While Quitting Out Early

It it supposed to ask for the month, year, and total amount 5 times. After I enter two months it closes. It has something to do with too many characters, if I just spam 1 1 1 over I can enter the correct amount of times. This isn't entirely finished but I cannot add anything until this is cleared.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
 
//This program will ask for the month, year, and total amount five times.
//After it will display the given information in a chart, along with
//the different taxes and the total tax amount.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
	ofstream outfile;
	outfile.open("out.txt");
	
	int count;
	count = 1; //(count <= 5);

	//String used to input month and year
	

	outfile << "class info\n";

	while (count <= 5)
	{
		
		string month;
		string year;
		double totamount;
		double sales;
		double citytax;
		double statetax;
		double tottax;

		getline(cin, month);
		getline(cin, year);
		cin >> totamount;
		
		//Tax calculations
		citytax = totamount * .02;
		statetax = totamount * .0625;
		tottax = totamount * .0825;
		sales = totamount / 1.0825;

		

		//Fixed and setprecision keep decimals tidy
		outfile << "Month: " << month << setw(5) << year << "\n";
		outfile << "----------------------------------\n";
		outfile << "Total Collected: " << setw(5) << "$"
			<< fixed << setprecision(2)
			<< setw(10) << totamount << endl;
		//Setw changes to adjust for shorter titles
		outfile << "Sales: " << setw(15) << "$"
			<< fixed << setprecision(2)
			<< setw(10) << sales << endl;

		outfile << "City Sales Tax: " << setw(6) << "$"
			<< fixed << setprecision(2)
			<< setw(10) << citytax << endl;

		outfile << "State Sales Tax: " << setw(5) << "$" 
			<< fixed << setprecision(2)
			<< setw(10) << statetax << endl;

		outfile << "Total Sales Tax: " << setw(5) << "$"
			<< fixed << setprecision(2)
			<< setw(10) << tottax << endl;
		
		count = count + 1;
	}

		outfile.close();
		system ("pause");
		return 0;

}
Last edited on
when you read a number you leave a '\n' in the input buffer
then a geltine operation will read that line-break giving you an empty string, so later input operations are out of sync

you may do cin >> totamount; cin.ignore(); to discard the line break
or as an alternative getline(cin>>ws, month); to ignore any whitespace at the front.
Last edited on
That worked perfectly! Thank you so much. I had already cleaned up the endl, just forgot to remove it beforehand
Last edited on
Topic archived. No new replies allowed.