Logic Error: Simple Test Grading

So here's my problem. I've written a simple program to ask three questions, store the user's answers into variables, and then used conditional statements to see if they got the answer correct.

I assigned "cAns" the value of 3 (the total number of questions). If they get the answer wrong "cAns" should be reduced by one. I then calculate the score by using this formula: score = (cAns / 3) * 100;, but for some reason it isn't working. Here's my code:

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <iomanip>
#include <conio>
using namespace std;

int main(){
	char ans1, ans2, ans3;
	int cAns, wAns, score;
	
	cAns = 3;
	
	clrscr();	
	cout << "Question #1:" << endl;   
	cout << "Which of the following is NOT a relational operator in C++?" << endl;
	cout << "	A. ==" << endl;
	cout << "	B. <" << endl;
	cout << "	C. !=" << endl;
	cout << "	D. &&" << endl << endl; //correct answer
	cout << "Please select a letter and press enter:  "; 
	cin >> ans1;
	
	clrscr();
	cout << "Question #2:" << endl;   
	cout << "Which of the following is a valid logical expression in a conditional statement?" << endl;
	cout << "	A. age = 56" << endl;
	cout << "	B. age !== 56" << endl;
	cout << "	C. age <= 56" << endl; //correct answer
	cout << "	D. age => 56" << endl << endl;
	cout << "Please select a letter and press enter:  "; 
	cin >> ans2;
	
	clrscr();
	cout << "Question #3:" << endl;   
	cout << "Which of the following is the \"OR\" boolean operator?" << endl;
	cout << "	A. !" << endl;
	cout << "	B. ||" << endl;//correct answer
	cout << "	C. &&" << endl;
	cout << "	D. ~" << endl << endl;
	cout << "Please select a letter and press enter:  "; 
	cin >> ans3;
	
	//Check answers
	if (ans1 != 'D' || ans1 != 'd'){//check question 1
		cAns = cAns - 1;
	}
	
	if (ans2 != 'C' || ans2 != 'c'){//check question 2
		cAns = cAns - 1;
	}

	if (ans3 != 'B' || ans1 != 'b'){//check question 3
		cAns = cAns - 1;
	}
	
	//calculate score
	score = (cAns / 3) * 100;
	
	//Print results
	clrscr();
	cout << "Your Results:" << endl;
	cout << left << setw(20) << "Question #" << left << setw(22) << "Your Answer" << left << setw(14) << "Correct Answer";
	cout << endl << right << setw(5) << "1" << right << setw(21) << " " << left << setw(23) << ans1 << left << setw(7) << "D";
	cout << endl << right << setw(5) << "2" << right << setw(21) << " " << left << setw(23) << ans2 << left << setw(7) << "C";
	cout << endl << right << setw(5) << "3" << right << setw(21) << " " << left << setw(23) << ans3 << left << setw(7) << "B";
	
	cout << endl << endl << "You got " << cAns << " out of 3 questions correct." << endl;
	cout << "Your percentage score is: " << score << "%";
	
	return 0;
}


Here's my output, no matter what letters they enter for their answers they get "0 out of 3 questions correct" and score a 100%.
Your Results:
Question #          Your Answer           Correct Answer
    1                     D                      D
    2                     C                      C
    3                     B                      B

You got 0 out of 3 questions correct.
Your percentage score is: 100%


Last edited on
Hey,
Its a simple thing thats wrong, with this part:
if (ans2 != 'C' || ans2 != 'c'){//check question 2
cAns = cAns - 1;
}
Say I enter C which is the correct answer, it looks at the first part and its not equal to C, but then because you have used an OR statement it will check if its equal to c which its not because my answer was capitals and then deduct. The best thing you can do is to convert the answer to lower case and check for that by
ans2 = tolower(ans2);
if (ans2 != 'c'){//check question 2
cAns = cAns - 1;
}
You need to do that with all 3 answers. By the way for the 3rd question you check ans3 and then ans1
if (ans3 != 'B' || ans1 != 'b'){//check question 3
cAns = cAns - 1;
}
Also might I suggest you check if the answer is indeed one of the 4 possibilities and if not to give the user another go?
One final thing the clrscr() function seems to be exclusive to the borland compiler...it does not work with all compilers...certainly my g++ compiler did not like it! You may want to look up other methods! Good luck!
Perhaps a little bit simpler than using tolower() function is to change logical operators.

if (ans2 != 'C' && ans2 != 'c')
I actually thought of that too but for some reason the logic didn't seem right in my head last night!! I was thinking it would have to be equal to both!! A lack of sleep is dangerous! Anyway yeah that works!
Thank you very much! Your solutions worked beautifully! I knew the issue was something really simple with my logic, but sometimes all it takes is to have someone else who thinks differently take a look at it.

One final thing the clrscr() function seems to be exclusive to the borland compiler...it does not work with all compilers...certainly my g++ compiler did not like it! You may want to look up other methods!


Yes, it is exclusive to the Borland compiler. I'll replace it with something a little more compatible with others. Maybe system("cls") ?
system("cls"); works...for windows obviously...I think its system("clear"); for linux....dont forget #include<process.h>

Ew. system for the fail. There are better ways to clear the screen, if you're so silly as to want to try. I personally don't even see the point.
for the fail.


FYI, it's for the loss (FTL).

If you really REALLY MUST clear the screen, use n/pdcurses. But then you can't use cout and cin so...
Topic archived. No new replies allowed.