
Kat H.
asked 06/28/21Write in C++ at the Linux command line a collection program that keeps track of a collection of information.
Introduction
Write in C++ at the Linux command line a collection program that keeps track of a collection of information. You can come up with your own idea of what information to store in the collection. Whatever idea you come up with, it must be complex enough to require an array of structs with at least three fields. See requirements for more details.
- movie collection
- book collection
- video game collection
- todo list
- DnD possessions (weapons, treasure, etc.) list.
I have an output example of a book collection manager below. This shows the level of complexity that your own program should have to get full credit. If are unsure on what to do, you can use this as your project's theme.
Whatever you do, try and keep it reasonably simple to do. Your work will be looked at on how well you follow the listed requirements.
Also make sure you keep the source code when you are done as you will need it for the future.
REQUIREMENTS
The following are in addition to the above general requirements
Tasks
Your program should have as a minimum the following tasks:
1. Allow the user to add entries to the collection. Check for valid data and let the user re-enter if they enter invalid data. Please let the user know what is valid data. For example, if the program needs a number between 1 and 100 let the user know in the prompt that they need to enter a number between 1 and 100.
So what is invalid data? I'm letting students define that but at a minimum whatever a user inputs should NOT cause your program to malfunction or crash (ie get a run-time error). So at least check for input that might cause your program not run correctly or crash. However, it doesn't hurt to catch obvious invalid data (e.g. negative years or prices) so the user can re-type them. if you want to you can ask the user if the data entered is okay and then allow them to re-enter.
2. Allow the user to print out the entire collection. Should not print out blank entries or invalid entries.
3. Allow the user to delete an entry from the collection. Allow users to choose which entry to delete. This can be by index (do print out the entries with their indices so the user can know what entry goes with what index). OR you can let them enter a search value and then search for that entry. If the entry is not found, do print out an error message, don't just do nothing. You can ask the user to re-enter or you can just return to the main function. Also, if you are using an index into the array, make sure this is a valid index to valid data.
Other tasks can be added but only if the above tasks are also present and working correctly. However, extra tasks will be graded for completeness and correctness so make sure they work too.
Code Requirements
The program also needs to have the following code structures:
1. An array of structs. The struct needs to be defined with at least 3 fields. At least one field should be numeric (integer or floating point) and at least one field should be a C string type (no C++ string type please). Please no parallel arrays (more than one array of simple types) for storing the collection.
2. A header file with your struct definition in it. Name the file after your struct name in all lower-case letters to prevent case-conflict issues.
3. Need to have at least one separate function for each task. Please try to break up your function tasks into smaller functions if they are getting long. Remember there is a 30 line requirement for all functions including the main function.
4. No global variables or goto's. See C++ Coding Guidelines under the Course Information module for styling and comment requirements.
1 Expert Answer

Patrick B. answered 06/29/21
Math and computer tutor/teacher
//stock.h
#ifndef _STOCK
#define _STOCK
#define STOCK_TICKER_LENGTH (12)
#define PORTFOLIO_FILENAME ((char*)"E:\\stocks.dat") //change this as needed
typedef struct _TStockRec
{
char stockTicker[STOCK_TICKER_LENGTH];
float stockPrice;
long numShares;
double subtotal; //calculated field: (stock price) * (# of shares)
} * TStockRec;
#define STOCK_REC_SIZE (sizeof(struct _TStockRec))
typedef struct _TPortfolio
{
TStockRec stockRecs;
TStockRec tempStockArray;
int count;
} * TPortfolio;
#define PORTFOLIO_SIZE (sizeof(struct _TPortfolio))
int Stock_Create(TStockRec,char *,float,long);
int Portfolio_PurchaseNewStock(TPortfolio,TStockRec stockRec);
int Portfolio_UpdateStock(TPortfolio,TStockRec stockRec);
int Portfolio_SellStock(TPortfolio,char * ticker);
int Portfolio_LinearSearchFind(TPortfolio, char * ticker);
int Portfolio_Report(TPortfolio,char * strMsg=(char*)0, char * filename=(char*)0);
int Portfolio_Read(TPortfolio,char*filename);
int Portfolio_Write(TPortfolio,char*filename);
#endif
//stock.cpp
#ifndef _STOCK
#include "stock.h"
#endif
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
Stock_Create(TStockRec stockRec,char * ticker,float price,long n)
{
strncpy(stockRec->stockTicker,ticker,STOCK_TICKER_LENGTH);
stockRec->stockPrice = price;
stockRec->numShares = n;
stockRec->subtotal = (price*n);
}
//adds the new stock record to the portfolio array
//ISSUE: (1) caller must guarantee the stock record is not already in the array via LinearSearchFind();
// (2) perhaps should return -1 in the RARE event that malloc fails
Portfolio_PurchaseNewStock(TPortfolio portfolio,TStockRec stockRecNew)
{
//realloc() is also an option
//allocates temp array that is one stock recorder LARGER
int N = portfolio->count;
portfolio->tempStockArray = (TStockRec) malloc(( N+1) * STOCK_REC_SIZE);
//copies the current data to temp array
memcpy(portfolio->tempStockArray,portfolio->stockRecs,N*STOCK_REC_SIZE);
free(portfolio->stockRecs); //frees the old array
//copies the new record into the temp array
memcpy(&portfolio->tempStockArray[N],stockRecNew,STOCK_REC_SIZE);
//points the stockRecs pointer at the new array
portfolio->stockRecs = portfolio->tempStockArray;
portfolio->tempStockArray = NULL;
//updates the counter
portfolio->count++;
Portfolio_Write(portfolio,PORTFOLIO_FILENAME);
}
//updates stock record in the portfolio array
Portfolio_UpdateStock(TPortfolio portfolio,TStockRec stockRec)
{
int iReturn=0;
int iIndexPosReturn = Portfolio_LinearSearchFind(portfolio,stockRec->stockTicker);
if (iIndexPosReturn>-1)
{
memcpy(&portfolio->stockRecs[iIndexPosReturn],stockRec,STOCK_REC_SIZE);
Portfolio_Write(portfolio,PORTFOLIO_FILENAME);
}
else
{
iReturn=-1;
printf(" ERROR: stock record not found \n");
}
return(iReturn);
}
//deletes the stock record from the portfolio array
Portfolio_SellStock(TPortfolio portfolio,char * ticker)
{
int iReturn=0;
int iIndexPosReturn = Portfolio_LinearSearchFind(portfolio,ticker);
if (iIndexPosReturn>-1)
{
//allocates temporary array with one LESS stock record
int N = portfolio->count;
portfolio->tempStockArray = (TStockRec)malloc((N-1)*STOCK_REC_SIZE);
//copies all of the stock records EXCEPT the one to be deleted
int curIndexPos=0;
for (int iLoop=0; iLoop<portfolio->count; iLoop++)
{
if (iLoop!=iIndexPosReturn)
{
memcpy(&portfolio->tempStockArray[curIndexPos],&portfolio->stockRecs[iLoop],STOCK_REC_SIZE);
curIndexPos++;
}
else
{
continue;
}
}
//updates the counter, frees the old array, updates the array pointers, and writes to file
portfolio->count--;
free(portfolio->stockRecs);
portfolio->stockRecs = portfolio->tempStockArray;
portfolio->tempStockArray=NULL;
Portfolio_Write(portfolio,PORTFOLIO_FILENAME);
}
else
{
printf("Stock record not found. Portfolio does not contain stock record with ticker %s \n",ticker);
iReturn=-1;
}
return(iReturn);
}
//locates the stock record in the portfolio array; returns the array index or -1 if not found
int Portfolio_LinearSearchFind(TPortfolio portfolio, char * targetTicker)
{
int iIndexPosReturn=-1;
for (int iLoop=0; iLoop<portfolio->count; iLoop++)
{
if (stricmp(targetTicker,portfolio->stockRecs[iLoop].stockTicker)==0) //found it!!!
{
iIndexPosReturn = iLoop;
break;
}
}
return(iIndexPosReturn);
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
Please email for all of the source code... there are 3 files stock.h, stock.cpp and of course main. THey will not all fit here. I was only able to post the header, and half the source code for stock.cpp file. Missing is the report writing routine and the main/driver. click on my profile picture and there is a link to email. I will save the source code for you.06/29/21