Program using data file

My program is not showing what i want it to.
In the data file the numbers printed were
1 1
2 2
3 3
4 4
5 5
1 5
1 4
1 3
4 5
4 2
5 1

I saved both files in the same folder and i am confused...
the numbers on top show the food and service rating. Each line counts as one customer. For example if i pick option 9, it tells me 0 people participated in the survey??



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
#include <iostream>
#include <fstream>					//for fstream

using namespace std;

int main(){

	ifstream infile("data.txt");	//read file into stream
	int f,s;
	int food[5];				//to store food rating
	int service[5];				//to store service rating
	int total=0,both_unsat=0,both_sat=0,both_one=0,both_5=0;
	for(int i=0;i<5;i++){		//initialize all to zero
		food[i]=0;
		service[i]=0;
	}
	while(infile >> f >> s){	//read while there are entries in the file
		food[f-1]++;			//increment food rating
		service[s-1]++;			//increment service rating
		total++;				//total number of entries
		if(f==1 && s==1){
			both_one++;			//both one
			both_unsat++;		//both unsatisfied
		}
		else if(f==5 && s==5){
			both_5++;			//both five
			both_sat++;			//both satisfied
		}
		else if(f<3 && s<3){
			both_unsat++;		//both unsatisfied
		}
		else if(f>3 && s>3){
			both_sat++;			//both satisfied
		}
	}
	int ch=0;
	while(ch!=10){			//menu for display

		cout<<"\nPlease choose a menu item."<<endl;
		cout<<"1. How many customers ranked the food as satisfactory."<<endl;
		cout<<"2. How many customers ranked the food as unsatisfactory."<<endl;
		cout<<"3. How many customers ranked the service as satisfactory."<<endl;
		cout<<"4. How many customers ranked the service as unsatisfactory."<<endl;
		cout<<"5. How many customers ranked both the food and service as satisfactory."<<endl;
		cout<<"6. How many customers ranked both the food and service as unsatisfactory."<<endl;
		cout<<"7. How many customers ranked the food and service both as 5."<<endl;
		cout<<"8. How many customers ranked the food and service both as 1."<<endl;
		cout<<"9. How many customers participated in the survey."<<endl;
		cout<<"10. Quit the program."<<endl;
		cin>>ch;
		cout<<endl;
		switch(ch){
			case 1: cout<<food[3]+food[4]<<" customers ranked the food as satisfactory."<<endl;
					break;
			case 2: cout<<food[0]+food[1]<<" customers ranked the food as unsatisfactory."<<endl;
					break;
			case 3: cout<<service[3]+service[4]<<" customers ranked the service as satisfactory."<<endl;
					break;
			case 4: cout<<service[0]+service[1]<<" customers ranked the service as unsatisfactory."<<endl;
					break;
			case 5: cout<<both_sat<<" customers ranked both the food and service as satisfactory."<<endl;
					break;
			case 6: cout<<both_unsat<<" customers ranked both the food and service as unsatisfactory."<<endl;
					break;
			case 7: cout<<both_5<<" customers ranked the food and service both as 5."<<endl;
					break;
			case 8: cout<<both_one<<" customers ranked the food and service both as 1."<<endl;
					break;
			case 9: cout<<total<<" customers participated in the survey."<<endl;
					break;
		}
	}

	return 0;
}
Last edited on
I'd check that the file has opened successfully. You can use is_open().

http://www.cplusplus.com/reference/fstream/ifstream/is_open/

I ran the code with a text file with the data above and got 11 as the # participated.
i used is open and it said there was an error...( am even more confused...if the same EXACT code works for you)
Did you create your own data file on the same computer and did you double check it's called "data.txt" and in the same directory as the executable?
yes. i created on notepad, named it data.txt and put it in the same visual studio folder i saved my hw in...
I tested the code on a Linux system and I don't have access to Visual Studio right now.

There's an answer to a similar problem here that might help.
https://stackoverflow.com/questions/32643393/how-to-open-input-file-c-on-visual-studio-community-2015

If that doesn't help, I might just try having the code temporarily try to create a new file for output, and see where it puts that file.
Topic archived. No new replies allowed.