Error message

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?


Edit: Clean up from prying eyes.
Last edited on
How do you have your code structured? There isn't enough information to help you from just that one line.
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
int main()

{
    int id, ans, omitted, grade, numstu;    
    double pct, right, wrong;

    const int 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.
Last edited on
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.
Last edited on
I can't use vector in this because i'm not allowed to.
1) Create a .txt document (xml would be better) holding question followed by a delimiter and then the answer

2) Open file and count number of questions

3) Create dynamic array of the number obtained from (2) above of a specific class say:

1
2
3
4
5
6
7
8
9
10
11
12
13
class CQuestionAnswer
{
public:
	CQuestionAnswer(const string question, const string answer)
		: m_question(question), m_answer(answer)
	{

	}

private:
	const string m_question;
	const string m_answer;
};



(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,

etc...

HTH
Last edited on
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.)
Last edited on
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.
Oh and could you guys give me a clue instead of an answer. I don't want to get in trouble in case my prof decide to look this up. :-)
It's such an obvious answer but yet I overlooked it. I got it! I used an If else compound statement and it worked like a charm!
Topic archived. No new replies allowed.