So, given a task for a class we are told to gather user inputs for 4 test scores and a final exam score. I built in if/else statements that should check if the value entered is a positive number. It works fine until the user enters a character. Then instead of going to the second input statement and asking for a value and waiting until all values are entered it seems to skip into the if statements, and returns all the input is not valid statements, assigns every variable the value of 0 and completes the program after only one input. Initially I had all five inputs separated with their individual if/else statements, I tried grouping them all at the top thinking it would at least allow you to get all five inputs entered, but it still breaks down the program as soon as the first character is entered. It worked fine for valid inputs, so I turned it in, though I was given points off for failed data validation. I'm not sure what I could have done differently as we weren't allowed to use use loops or anything that wasn't previously covered in class, including a line such as this one I found while browsing the forums.
here is the program I wrote, if someone could explain to me why it breaks down as soon as the first character is entered maybe I could come up with a better solution. (I tried the program with both int and double variable types and both broke down the same way.)
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
int test1 = 0, test2 = 0, test3 = 0, test4 = 0,
final_exam = 0,
temp1, temp2, temp3, temp4, temp5;
//first test score
cout<< "Please enter the numerical grade of your first test: ";cin>>temp1;cout<<endl;
cout<< "Please enter the numerical grade of your second test: ";cin>>temp2;cout<<endl;
cout<< "Please enter the numerical grade of your third test: ";cin>>temp3;cout<<endl;
cout<< "Please enter the numerical grade of your first test: ";cin>>temp4;cout<<endl;
cout<< "Please enter the numerical grade of your first test: ";cin>>temp5;cout<<endl;
if (temp1 >= 0)
{
test1 = temp1;
}
elseif (temp1 < 0)
{
cout<< "Numerical value entered for first test score is invalid.\n";
}
//second test
if (temp2 >= 0)
{
test2 = temp2;
}
elseif (temp2 < 0)
{
cout<< "Numerical value entered for second test score is invalid.\n";
}
//third test
if (temp3 >= 0)
{
test3 = temp3;
}
elseif (temp3 < 0)
{
cout<< "Numerical value entered for third test score is invalid.\n";
}
//fourth test
if (temp4 >= 0)
{
test4 = temp4;
}
elseif (temp4 < 0)
{
cout<< "Numerical value entered for fourth test score is invalid.\n";
}
//final exam
if (temp5 >= 0)
{
final_exam = temp5;
}
elseif (temp5 < 0)
{
cout<< "Numerical value entered for final exam score is invalid.\n";
}
//data verification finished
//average calculations
double test_average = ((test1 + test2 + test3 + test4) / 4);
double final_grade = (((test_average * 7) + (final_exam * 3)) /10);
//output statements
cout<< "Your first test score was: "<<fixed<<setprecision(1)<<test1<<endl;
cout<< "Your second test score was: "<<fixed<<setprecision(1)<<test2<<endl;
cout<< "Your third test score was: "<<fixed<<setprecision(1)<<test3<<endl;
cout<< "Your fourth test score was: "<<fixed<<setprecision(1)<<test4<<endl;
cout<< "Your final exam score was: "<<fixed<<setprecision(1)<<final_exam<<endl;
cout<< "Your overall grade in the class is: "<<fixed<<setprecision(1)<<final_grade<<endl;
//letter grade assignment
if (final_grade >= 89.5)
{
cout<< "Congratulations, you made an A!\n";
}
elseif ((final_grade >= 79.5) && (final_grade <= 89.4))
{
cout<< "Nice work, you made a B.\n";
}
elseif ((final_grade >= 69.5) && (final_grade <=79.4))
{
cout<< "You passed the class with a C.\n";
}
elseif ((final_grade >= 59.5) && (final_grade <=69.4))
{
cout<< "Your grade is a D.\n";
}
elseif (final_grade < 59.4)
{
cout<< "You made an F.\n";
}
return 0;
}
> as we weren't allowed to use use loops or anything that wasn't previously covered in class
¿did you actually asked?
> I was given points off for failed data validation
You say that the input was invalid, but you keep processing.
> why it breaks down as soon as the first character is entered
because the streams remains in an invalid state, so almost any operation would fail.
So you could test the stream to know if the operation was succesful
If you want to recover, then you need to reset the stream std::cin.clear(); and discard the input buffer std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Another alternative would be to read as a string (that will not fail), and then check if that can be converted to a number
I appreciate your reply. As for asking the instructor about material not covered in class yes, I asked about it after recieving negative feedback for using fixed, showpoint, and setprecision to display monetary values in the proper format before we covered it in class.
While I do appreciate your attempt to help me I'm afraid I don't understand much of anything you said. I am only a few weeks into my first programming class ever and only into chapter 4 in the text book. After reading the entries on this website concerning those functions I believe I could get them to work but wouldn't be allowed to use them.
However, my main frustration with why the entire program would just collapse was fixable by following your example on using cerr, and placing return 1; in the else part of my if statements causing the program to return the error line and shut down. I was previously unaware of being able to use a return value in this way. Thanks again for replying and trying to help me out of my dilemma.