I have everything working correctly besides when a person enters both first AND last name, it throws the program into a loop, and I cant get the save to work correctly, it builds just fine, but when it is ran the program shuts down.
The program is first supposed to ask if you want to load info from a file or start fresh. Then the user can choose to enter names and grades to a list, view the list, or exit. I can not get the program to save the list.
Code:
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cctype>
#include <cmath>
#include <istream>
using namespace std;
double exam[100][4];
string students[100];
int numberofstudents=0;
char assign(double average);
void selection();
void print()
{
cout<<setw(10)<<left<<"Student"<<setw(10)<<left<<"Exam 1"<<setw(10)<<left<<"Exam 2"<<setw(10)<<left<<"Exam 3"<<setw(10)<<left<<"Final"<<setw(10)<<left<<"Average"<<setw(10)<<left<<"Letter"<<endl;
for (int i=0; i<numberofstudents;i++)
{
double average=((exam[i][0]+exam[i][1]+exam[i][2]+exam[i][3])/4);
cout<<setw(10)<<left<<students[i]<<setw(10)<<left<<exam[i][0]<<setw(10)<<left<<exam[i][1]<<setw(10)<<left<<exam[i][2]<<setw(10)<<left<<exam[i][3]<<setw(10)<<left<<average<<setw(10)<<left<<assign(average)<<endl;
}
}
void add()
{
cout<< " Please enter student name "<<endl;
cin>>students[numberofstudents]; //When a person's first AND last name are entered, it throws the program into a continual loop, what is used so it gets first and last names without the loop.
cout<< " Please enter the exam grades in this order: Exam 1, Exam 2, Exam 3, Final "<<endl;
cin>> exam[numberofstudents][0]>>exam[numberofstudents][1]>>exam[numberofstudents][2]>>exam[numberofstudents][3];
numberofstudents++;
ofstream save;
save.open("grades.txt");
save<<students[0]<<exam[0][0]<<exam[0][1]<<exam[0][2]<<exam[0][3];
while (save)
{
numberofstudents++;
save<<students[numberofstudents]<<exam[numberofstudents][0]<<exam[numberofstudents][1]<<exam[numberofstudents][2]<<exam[numberofstudents][3];
}
save.close();
selection();
}
char assign(double average)
{
char grade;
if (100>=average&&average>89)
grade= 'A';
else if (90>average&&average>79)
grade='B';
else if (80>average&&average>69)
grade='C';
else if (70>average&&average>59)
grade='D';
else grade='F';
return grade;
}
void selection()
{
bool flag=true;
while (flag)
{
int t;
cout<<"Please enter activity selection: "<<endl;
cout<<"0 - Print Student Grades "<<endl;
cout<<"1 - Add Student "<<endl;
cout<<"9 - Exit "<<endl;
cin>>t;
if (t==0)
{
print() ;
}
else if (t==1)
{
add() ;
}
else if (t==9)
{
flag=false;
}
else
{
cout<< "INVALID ENTRY"<<endl;
flag=true;
}
}
}
int main()
{
char y;
cout<<"Would you like to enter data from an existing file(Y/N)?"<<endl;
cin>>y;
if ('Y'==y||y=='y')
{
ifstream grades;
grades.open("grades.txt");
grades>>students[0]>>exam[0][0]>>exam[0][1]>>exam[0][2]>>exam[0][3];
while (grades)
{ numberofstudents++;
grades>>students[numberofstudents]>>exam[numberofstudents][0]>>exam[numberofstudents][1]>>exam[numberofstudents][2]>>exam[numberofstudents][3];
}
grades.close();
selection();
}
else if(y=='N'||y=='n')
{
selection();
}
else
{
cout<<" INVALID SELECTION";
}
return 0;
}