Can anyone help me with this C++ exercise?

I am very confused as to how to go about this c++ assignment. I know I need to use fstream but am completely stuck.

"Modify your code to enter the favorite beverage of each person and
store it in a text file “survey.txt”, after finishing the data entry, close the file and then process the data from the file to produce the output similar to Lab #3."
Note:
1. When you run the program it will ask you if you want to enter the favorite
beverages or not.
2. If there is no file or no data in the file it will display a message to ask you if
you want to enter the data or quit the program

My original code is:

#include <iostream>
using namespace std;

int main()
{
int choice = 0, coffee = 0, tea = 0, coke = 0, oj = 0, counter = 0;
while (choice != -1)
{
cout << "1 - Coffee " << endl;
cout << "1 - Tea " << endl;
cout << "1 - Coke " << endl;
cout << "1 - Orange Juice " << endl;

cout << "Please input the favorite beverage of person #:" << counter + 1 << "\n";
cout << "Choose 1, 2, 3 or 4 from the above menu or -1 to exit the program: ";

if (choice == 1)
{
coffee++;
}
else if (choice == 2)
{
tea++;
}
else if (choice == 3)
{
coke++;
}
else if (choice == 4)
{
oj++;
}
else
{
cout << "invalid selection" << endl;
}
counter++;
}
cout << "The total number of people surveyed is " << counter << "The results are as follows: Beverage Number of Votes\n";

cout << "**********************\n";
cout << "Coffee= " << coffee << endl;
cout << "Tea= " << tea << endl;
cout << "Coke= " << coke << endl;
cout << "Orange Juice= " << oj << endl;

cin.get();
cin.get();
return 0;
}

Apparently, you're being asked to store those for each person in a file, rather than the screen.

Your code doesn't seem to have the notion of a person, so it's possible you've missed a step.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if (choice == 1)
{
coffee++;
}
else if (choice == 2)
{
tea++;
}
else if (choice == 3)
{
coke++;
}
else if (choice == 4)
{
oj++;
}
else
{
cout << "invalid selection" << endl;
}


i'm suggesting you to use switch

http://www.cplusplus.com/doc/tutorial/control/
Topic archived. No new replies allowed.