I created a while loop that finds out how students did on a test. The most that can be answered is let's say 20 questions. For the ones that answered more than 20 questions, how would I print an error message and then skip the rest of the rest of the processing(for that student) and then move on to the next one?
int main()
{
int id, ans, omitted, grade, numstu;
double pct, right, wrong;
constint total=20;
ans= right+ wrong;
cout<<"Please enter id number? \n";
cin>>id;
while(id!=0000||ans<=total)
{
cout<<"Please enter right answers \n";
cin>>right;
cout<<"Please enter wrong answers \n";
cin>>wrong;
cout<<"ID number "<<id<<"\n"<<(int)right<<" right "<<(int)wrong<<" wrong";
cout<<"\nThe total answered is "<<ans<<endl<<"The total omitted is "<<omitted<<endl;
pct=right/ans;
cout<<"The correct answer percentage is "<<pct<<endl;
grade=right*2;
cout<<"The grade is "<<grade;
cout<<endl<<endl;
cout<<"Please enter id number \n";
cin>>id;
}
return 0;
}
Edit: Paranoia. Clean up code from prying eyes. Tell me what's not clear.
Code from Kistal G.
Its all a bit vague, and we would need to know more information, i.e. see the block of code in question.
If I were doing this sort of thing I would create a class that hold a question and the correct answer (as long as it is a type of questioon that does have an exact right answer i.e. a math question). Then I would store loads of instances of this class in a vector, and iterate through the vector displayimng the question and reading the student response, then compare the student reponse to the correct answer. Loop round all the questions and then display the percentage of questions the student got right.
(4) Read the .txt (or .xml) file and create an instance of the above class for each question/answer pair (hint) and add it to your array you created.
You could add some functions to the class to accept the students answer and to return a Boolean to indicate whether they got the answer right or wrong,
It would appear you have some misconceptions about they way C++ works.
Line 17 sets the variable ans equal to the sum of the variables right and wrong. Both right and wrong have unspecified values, so the value of ans is unspecified as well. (In other words, this code is a bit of nonsense. It doesn't do anything useful.)
From the comment, it seems you think line 17 sets up some sort of invariant for ans such that it will always be equal to the sum of the other two variables. Likewise with line 19. It doesn't work this way. These statements are executed where they occur in the code. They have no effect on code occurring after them.
Line 21 compares ans to total and discards the result. (In effect, it does nothing.)
Line 21 compares ans to total and discards the result. (In effect, it does nothing.)
That's an accident. I was attempting do something I can't remember there, I'll delete it.
Line 17 was originally inside the loop but I put it there because I wanted to use the variable ans.
Thanks for your input though.
You could add some functions to the class to accept the students answer and to return a Boolean to indicate whether they got the answer right or wrong,
etc...
All what you're saying is fantastic but I can't use it. I'm going to be honest with you, we have not covered classes as yet. Perhaps, I should have put this in the beginner section though I kinda thought I did but I see now I didn't.