while(cin)

Nested if statements in while loops, do i need an else statement after every if?
If statement is just adding every number in file, instead of just the numbers associated with their respective categories,cant figure out why

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
  inData.open("ola6FinancialData.txt");

	inData.ignore(100,'c');
	inData >> category;

	
	while (inData)
	{	
		
		if (category == "Restaurant")
		{ 
		inData.ignore(500, '$');
		inData >> num1;
		totalR= totalR+num1;
		cout<<totalR;
	
		}
		
		else if (category =="Groceries")
		{	inData.ignore(500, '$');
			inData >> num1;
			totalGr=totalGr+num1;
		}
		else if (category == "Gas")
		{ inData.ignore(500, '$');
			inData >> num1;
			totalGa= totalGa+num1;
		}
		else if (category == "Entertainment")
		{ inData.ignore(500, '$');
			inData >> num1;
			totalEn= totalEn+num1;
		}
		
	}


its supposed to grab just the numbers associated with the category but it instead grabs them all and adds them for total of restaurant.
Last edited on
In this case, you don't need it as there is no way category could possibly be multiple options at the same time.
I cant figure out why its not executing the if correctly. its just adding every number in the damn file.

derp.

forgot the inData>> category needs to go in the while loop.


thank you for the help
Last edited on
Topic archived. No new replies allowed.