Input validation predicament
Mar 14, 2013 at 8:54pm UTC
I'm currently working on a very simple program that takes in a students name, however many grades that student has, then averages the grades and outputs them. A condition that I want to meet, is that if the user inputs a "character" instead of a "int" for a grade, I want to be able to tell them it's wrong and ask them to re-enter the value.
Here's my code so far, its just missing the input verification bit:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
char studentName[15];
int gradeCount;
float finalGrade, gradeSum, gradeCheck;
bool gradeLoop = true ;
int _tmain(int argc, _TCHAR* argv[])
{
cout<<"Please enter student's name." <<endl;//student name-------
cin>>studentName;
cout<<studentName<<"'s grades." <<endl;//student name---------
cout<<"Enter student grade." <<endl;
cin>>gradeCheck;
while (gradeLoop)//grade loop---------
{
if (gradeCheck >= 0 && gradeCheck <= 150)//user grade input check for the correct value limit
{
gradeSum += gradeCheck;//increments gradeSum
cout<<"Current Grade Sum: " <<gradeSum<<endl;
gradeCount++;//tracks the number of grades entered by the user
cout<<"Number of Grades Entered: " <<gradeCount<<endl;
cout<<"Enter student's next grade." <<endl;
}
else //check for negetive grade value, average calculated, and average output-------
{
finalGrade = (gradeSum/gradeCount);
cout<<studentName<<"'s final grade:" <<setprecision(4)<<finalGrade<<endl;
gradeLoop = false ;
}
cin>>gradeCheck;
}//end grade while loop --------------------
system ("pause" );
return 0;
}
what's the simplest way to verify if the user accidentally inputs a "char" instead of a numerical value?
Last edited on Mar 18, 2013 at 8:59pm UTC
Topic archived. No new replies allowed.