So here's my dilemma. I have this homework assignment due today which involves reading data in from a file, separating the lines into various variables, and displaying the lines based on certain parameters. The issue I (as well as my classmates) are experiencing is that our professor is very bad at teaching and with our projector currently broken, she's not able to teach from the pre-given slides and instead relies 100% on examples she comes up with and explains very little about. Not only this but I've been working away at this for hours, it is currently 4:30 in the morning and I am very bad at programming regardless of professor. I've never been very good and it's actually going to make me change majors because I can't get the hang of it. Basically I just need to get an idea of the steps to take in the right direction because otherwise I'm going to be up all night and will have a miserable rest of the day because of it.
Our assignment involves taking data from a list of farms which also include a number of items, the description of said item, the price per item, and the total cost of said item multiplied by the cost per item all on one line per "complete" listing. If the farm itself has been mentioned previously in the file (duplicates are conveniently placed next to each other) then add the number of items as well as the total price into one single line. So for example, between the 3 listings of "Big Top Farm" would be displayed as one line containing 10,625 total items with a total cost of $5,622.30. At the very end, the code is intended to print out a specific number of "unique" farms that contributed (ones that had repeat entries are only included once). I understand that I could go about this with a simple counter integer with a quick ++ sequence after it reads in a specific set, but that's about the only thing I know I'm doing correctly.
Here's my desperate attempt at code (which yes, I know is unfinished and doesn't build)
#include <fstream>
#include <cstdlib>
#include <string.h>
using std::cin;
using std::cout;
using std::endl;
using std::ifstream;
using std::ofstream;
using std::ios;
using std::string;
//prototypes
void readIn();
int farmDisplay(int, string, double, double);
int main()
{
string farmName, itemType;
int itemCount, farms;
double itemPrice, totalPrice;
cout << "==================================================" << endl;
cout << "= FARMER'S MARKET INVENTORY =" << endl;
cout << "==================================================" << endl;
farms = farmDisplay(itemCount, itemType, itemPrice, totalPrice);
cout << endl;
cout << "There were " << farms << " unique farms contributing to this week's event." << endl;
return 0;
}
//precondition:
//postcondition:
int farmDisplay(int itemCount, string itemType, double itemPrice, double totalPrice)
{
int counter = 0, result, prevItemCount, currentItemCount;
string farmName, prevFarm, currentFarm;
ifstream inFile;
inFile.open("ASSGN6-B.txt");
//Check for Error
if(inFile.fail())
{
cout << "Error opening file..." << endl;
exit(1);
}
while(!inFile.eof())
{
cin.ignore();
getline(inFile, currentFarm, ',');
if(prevFarm.compare(currentFarm) == 0)
{
prevFarm = currentFarm;
prevItemCount == currentItemCount;
counter--;
}
else
{
prevFarm = currentFarm;
prevItemCount == currentItemCount;
}
inFile >> itemCount >> itemType >> itemPrice >> totalPrice;
cout << farmName << " " << itemCount << " items contributed totaling $" << totalPrice << endl;
counter++;
}
inFile.close();
return counter;
}
Here's what the file that we are given looks like:
Collins Farm, 43900 tomatoes 0.67 29413
Bart Smith Farms, 34910 cassavas 0.99 34560.9
Allen Farms, 117 coconuts 0.54 63.18
River Run Farm, 103 taros 0.65 66.95
Big Top Farm, 109 artichokes 2.23 243.07
Big Top Farm, 777 crosns 0.28 217.56
Big Top Farm, 9739 cucumbers 0.53 5161.67
Marble Farm, 108 crosns 0.33 35.64
Food For Life Inc., 106 carrots 0.87 92.22
Food For Life Inc., 86 coconuts 0.84 72.24
Johnson Farms, 121 parsnips 0.22 26.62
A1 Farm, 111 beets 0.12 13.32
A1 Farm, 5591 taros 0.72 4025.52
Looney Tunes Farm, 102 onions 0.49 49.98
Wolfe Creek Farm, 103 rhubarbs 1.21 124.63
Wolfe Creek Farm, 199 radishes 0.71 141.29
James Farm, 47 pickles 0.68 31.96
Weaver Farms, 75 walnuts 2.5 187.5
Weaver Farms, 500 pickles 0.59 295
Pore Boy Farms, 670000 peanuts 0.79 529300
Rutherford Farms Inc., 809 apples 0.9 728.1
Rutherford Farms Inc., 659 pickles 0.7 461.3
Javens Farm, 129000 figs 0.44 56760
Harrison Farms, 8001 yams 1.09 8721.09
Setzer Farms Inc., 701 potatoes 0.89 623.89
Setzer Farms Inc., 651 tomatoes 0.69 449.19
Pikes Peak Farm, 1045 turnips 0.79 825.55
Holland Area Farms, 10001 radishes 0.69 6900.69
Any advice would be greatly appreciated as I feel like I'm going to go insane working on this project any longer