Write a gradebook application that offers the user the following menu: 1. Creat
ID: 3547246 • Letter: W
Question
Write a gradebook application that offers the user the following menu:
1. Creat a grade file for a class
2. Display gradebook for a class
3. Claculate the averages for a class
4. Exit
The program should continue to execute until the user terminates by selecting choice 4. If choice 1 is selected, the user should be prompted for each students name and four test grades, then create a file composed of this information with a filename selected by the user. If the user selects choice 2, the program should prompt for the name of the grade file, read each student and his/he grades, and print the information on the screen. If the user selects choice 3, the program shhould prompt for the name of the grade file, then read each student and his/her grades, calculate the student's average, and print the student's name and average.
I have a couple problems. No matter what number I input, the program will always select choice 1. When I'm in choice 1 the file that is created has nothing in it. Below is my program and I hope someone can correct it.
#include<iostream>
#include<fstream>
using namespace std;
int menu(void)
{
int user_input=0;
cout << "Select one of the following by selecting the corresponding number." << endl;
cout << "1. Create a grade file for a class." << endl;
cout << "2. Display the gradebook for a class." << endl;
cout << "3. Calculate averages for a class." << endl;
cout << "4. Exit" << endl;
cin >> user_input;
}
int main ()
{
string first, last, filename, input_file, average_file, temp;
int g1, g2, g3, g4, i, students, user_input;
char x;
while(user_input=menu()!=4)
{
if (user_input == 1)
{
ofstream gradeFile;
cout << "What would you like to call this file?" << endl;
cin >> filename;
getline(cin,filename);
gradeFile.open(filename.c_str());
cout << "Select how many students you would like to enter." << endl;
cin >> students;
for(i=1; i<=students; i++)
{
cout << "Enter student " << i << "'s name (first name, last name) :" << endl;
cin >> first >> last;
gradeFile << "Student " << i << "'s name: " << first << " " << last << endl;
cout << "Enter student " << i << "'s grades :" << endl;
cin >> g1 >> g2 >> g3 >> g4;
gradeFile << "The grades are: " << g1 << ", " << g2 << ", " << g3 << ", " << g4 << endl << endl;
}
gradeFile.close();
}
else if (user_input == 2)
{
ifstream gradeData;
cout << "Enter the name of the grade file you are trying to access." << endl;
cin >> input_file;
getline(cin,input_file);
gradeData.open(input_file.c_str());
while(gradeData >> first >> last >> g1 >> g2 >> g3 >> g4)
{
cout << "Name: " << first << " " << last << endl;
cout << "Grades: " << g1 << ", " << g2 << ", " << g3 << "," << g4 << endl;
}
gradeData.close();
}
else if (user_input == 3)
{
ifstream averageData;
cout << "Enter the name of the grade file you are trying to access." << endl;
cin >> average_file;
getline(cin,average_file);
averageData.open(average_file.c_str());
while(averageData >> temp >> temp >> temp >> first >> last >> temp >> temp >> temp >> g1 >> x >> g2 >> x >> g3 >> x >> g4)
{
float sum = g1 + g2 + g3 + g4;
cout << "Name: " << first << " " << last << ", Average: " << sum / 4 << endl;
}
averageData.close();
}
else if (user_input == 4)
{
return 0;
}
else
{
cout << "Invalid entry, please try again." << endl;
}
}
}
Explanation / Answer
You don't need to use getline because you are already using cin>>filename.
It worked if you remove getline function
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.