C++ reading from .txt file

Hello,
I am having trouble with finding the running total in a .txt file.
I am asked to add up all the numbers for a specific category (like candy) and display total sale.


Text file contains:
Candy
6.59
Candy
6.59
Costumes
43.90
Decorations
10.98
Costumes
4.39


The part I am having trouble with in my code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
	else if (mainMenuChoice == 2)
	{
		ifstream file;
		cout << "You chose total sales for today.\n";
		if (file.fail())
			cout << "Error opening input file\n\n";
		else
		{
			file.open("sales.txt", ios::out);
			if (file)
			{
				getline(file, candy);
				file >> totalCandy;
				cout << candy << "\n" << totalCandy;
				
				getline(file, costumes);
				file >> totalCostumes;
				cout << costumes << "\n" << totalCostumes;

				getline(file, decorations);
				file >> totalDecorations;
				cout << decorations << "\n" << totalDecorations;
			
				file.close();
				file.clear();
			}


Thank you!
Can anyone help me with this? I am really lost.

I know I have to have a variable like 'sum' to calculate the running total in the file but I am not really sure how to do it, please help!
@cute

It doesn't look like your reading the file correctly. Plus, you're not increasing costs at all. The following worked in the program I tinkered with to get your code working.

Of course, you'll have to cout only the value of the item's total cost, that the user asks for.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 ifstream file("Sales.txt");
 cout << "You chose total sales for today.\n";
 double totalCandy = 0.0, totalCostumes = 0.0, totalDecorations = 0.0;
 char item[11];
 double cost; 
 if (!file)
	cout << "Error opening input file\n\n";
 else
 {

	cout << "File opened..." << endl;
	while (file >> item >> cost)
	{
	 cout << "Reading " << item << " costs $" << cost << endl;
	 if(strcmp("Candy", item) == 0)
		totalCandy += cost;

	 if(strcmp("Costumes", item) == 0)
		totalCostumes += cost;

	 if(strcmp("Decorations", item) == 0)
		totalDecorations += cost;
	}
	cout << "Amount Spent:" << endl;
	cout << "Candy       : $" << totalCandy << endl;
	cout << "Costumes    : $" << totalCostumes << endl;
	cout << "Decorations : $" << totalDecorations << endl;
 }
 file.close();
}
Last edited on
Why did you use a C-string for item? The OP appears to be using std::string so if you're going to give the whole solution you should have at least stuck with the std::strings. Then you wouldn't have needed things like strcmp().

Also since you did use a C-string for item you should never use the extraction operator>> to extract the string unless you limit the number of characters that will be retrieved by using setw() before the string extraction. Never ever use a function that doesn't limit the number of characters that will be retrieved when working with C-strings.

Lastly you don't need to call the file close() function in this program. You're using C++ streams and since the stream goes out of scope on the next line (the end of the function) the class destructor will properly close() the file. Plus the fact that that close(), if you insist on using it, should be within the else block, not after it because if that else doesn't execute there is no need in trying to close an unopened file.

Where is the file located? Is it inside your project or outside ?
Topic archived. No new replies allowed.