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:
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!
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") ?