Hello I am new to the boards, and would like to say hello. Also thank you in advance for any help you can offer.
I am slightly confused at this point in my program for two reasons
1. When I type in Y to continue and start the program over it does not allow me to input a new name a second time. I was not having this problem earlier which is why I am so confused by why it's now acting up.
2. I cannot seem to understand how to flush the input statements, when I choose to start the program again it keeps the previous grades in the stream.
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<string>
usingnamespace std;
int main()
{
int inGrade,
lowGrade=999,
highGrade=0,
GradeCtr=0,
i=0,
Grades[5]={0};
double Average,
Total=0;
string name;
string word = "hellohellohellohellohellohello";
cout <<setprecision(1)
<<setiosflags(ios::fixed)
<<setiosflags(ios::showpoint);
char response='Y';
while (response=='Y')
{
cout <<" Please enter the student's name:"; cout <<endl;
getline(cin,name,'\n'); cout<<endl;
cout<<"Please enter a list of exam Grades using integers from 0 to 100. End the list\n";
cout<<"by entering -1.\n";
cin >>inGrade;
while (inGrade != -1)
{
if (inGrade>highGrade) highGrade=inGrade;
if(inGrade<lowGrade) lowGrade=inGrade;
if (inGrade>89) Grades[0]++;
elseif (inGrade>79) Grades[1]++;
elseif (inGrade>69) Grades[2]++;
elseif (inGrade>59) Grades[3]++;
else Grades[4]++;
GradeCtr++;
Total += inGrade;
cin>>inGrade;
}
Average = Total/GradeCtr;
cout<<"\nThere were " << GradeCtr << " grades entered for " <<name<< " in the following grade\n";
cout<<"categories:"; cout << endl; cout << endl;
cout<< "A B C D F"; cout << endl;
for (i=0;i<5;i++)
cout<<Grades[i]<<" ";
cout<<endl;
cout<<endl;
cout<<"\nThe scores ranged from " <<lowGrade<< " to " <<highGrade<< " and the overall average was " <<Average;
cout<< endl; cout<< endl;
cout<<"Would you like to continue with another student's grades?(Y/N)" <<endl;
cin >> response;
cout<<endl;
cin.ignore(1,'\n');
}
}
1. When I type in Y to continue and start the program over it does not allow me to input a new name a second time. I was not having this problem earlier which is why I am so confused by why it's now acting up.
First of all, let take a look at this :
while (response=='Y')
This code only does check the 'Y' uppercase. If you type 'y' lowercase, your program will end anyway.
In order to check both uppercase & lowercase, you can try this :
while (response== 'Y' || response== 'y')
Or : while (toupper(response) == 'Y')
Edit :
2. I cannot seem to understand how to flush the input statements, when I choose to start the program again it keeps the previous grades in the stream.
You should clear your data. This can be done easily :
@Kikiman, I think you may have misunderstood the OP's question. He is asking about how to flush the input buffer for his program.
My way of doing this is to use the following little code snippet to clear the input buffer:
1 2 3 4 5 6
// Sync the input buffer with the program
std::cin.sync();
// Ignore every character in the input buffer up and including
// the first newline character
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Thank you both for the help with this, I still made the change so that the loop can either be primed with 'y' or 'Y'.
And NT3 I am happy to say I can input different names again so that has been a help, but I am still getting the same occurrence of the input stream not leaving.
I posted what the output is doing, hopefully this makes what I am saying a little more understandable.
Please enter the student's name:
Joseph Fakemen
Please enter a list of exam Grades using integers from 0 to 100. End the list
by entering -1.
88
99
-1
There were 2 grades entered for Joseph Fakemen in the fallowing grade categories:
A B C D F
2 2 0 0 0
The scores ranged from 88 to 99 and the overall average was 93.5
Would you like to continue with another student's grades?(Y/N)
Y
Please enter the students's name:
Louie Notreal
Please enter a list of exam Grades using integers from 0 to 100. End the list
by entering -1.
66
77
-1
There were 4 grades entered for Louie Notreal in the following grade categories
A B C D F
1 1 1 1 0
The scores ranged from 66 to 99 and the over all average was 82.5
Would you like to continue with another student's grades?(Y/N)