What is required is to create a submenu called surveyMenu that is called from option number one of the main menu.
The surveyMenu should have 4 options:
1- Enter survey Data
2- Display Survey Data
3- Search for the result of a Drink Survey
4- Return to the main menu
The code:
#include <iostream>
#include <fstream> //stream class needed to read and write to a file
#include <string>
#include <sstream>
using namespace std;
int main()
{
int coffee = 0, orangeJuice = 0, tea = 0, coke = 0, num = 0, total = 0, person = 0, currentPerson = 1; //initialized the variables and added current person
fstream outFile; //created ofstream for us to write in a file
outFile.open("survey.txt", fstream::app); //allows it to open in the survey text file
if (outFile.is_open()) //if the file is open then it will be put into the text file
{
while (num != -1) //sentinel value of -1 for the user to terminate when they are finished with the program
{
cout << "***********************" << endl;
cout << " 1 Coffee" << endl; //displayed all the beverages and the number required to pick them
cout << " 2 Orange Juice" << endl;
cout << " 3 Tea" << endl;
cout << " 4 Coke" << endl;
cout << endl;
cout << "***********************" << endl;
cout << "Please input the favorite beverage of person #" << currentPerson++ << " Choose 1,2,3,4, from the above menu or -1 to exit the program." << endl;
cin >> num; //displays instructions and also shows how many people have chose a beverage
if (num == 1 || num == 2 || num == 3 || num == 4) //allows the file to retrieve the numbers if conditions is met
{
outFile << num << "\n";
}
else if (num == -1)
{
cout << "End of input" << endl;
}
else
{
cout << "Invalid number.\nPlease re-enter a number between 1 and 4" << endl;
}
}
outFile.close();
}
outFile.open("survey.txt", fstream::in);
string line;
if (outFile.is_open())
{
while (getline(outFile, line)) //allows the file to retrieve information from condition
{
num = stoi(line);
switch (num)
{ //created a switch statement to allow us to tally the amount for each beverage
case 1:
coffee++;
person++;
break;
case 2:
orangeJuice++;
person++;
break;
case 3:
tea++;
person++;
break;
case 4:
coke++;
person++;
break;
}
}
outFile.close(); //closes the file
}
else
{
cout << "Error" << endl;
}
cout << "the total number of people surveyed is " << person << " The results are as follows: Beverage Number of Votes" << endl;
//Instead of using cout statement you use outFile statement because the outFile is open