Simple Quiz

This time I try to make a quiz with three questions that should be appearing with order. Somehow, I got an "invalid conversion from 'const char' to 'char" error. I wonder why? This is the code.

 ``123456789101112131415161718192021222324252627282930313233343536`` `````` #include using namespace std; int main() { int points = 0; char decision, firstquestionlogic, secondquestionlogic, thirdquestionlogic; cout << "Answer these five questions below!" << endl; cout << "Ready? Y/N " << endl; cin >> decision; if(decision = "Y") { cout << "The head of this country is Mr.Abe. Y/N ?" << endl; cin >> firstquestionlogic; if(firstquestionlogic = "Y") {points = points + 100;}; cout << "Whale is not a mammal. Y/N ?" << endl; cin >> secondquestionlogic; if(secondquestionlogic = "N") {points = points + 100;}; cout << "The richest person in the world is Steve Jobs. Y/N ?" << endl; cin >> thirdquestionlogic; if(thirdquestionlogic = "N") {points = points + 100;}; } cout << "Your accumlated points is " << points <
const char* is a string(word/sentence)
char is a letter.

And you compare both(and you can't!). Hence, error.

Just change all your conditionals, like in line 14:

 `` `` ``if( decision == 'y') // mind the single ' ``

And when I wrote it, I noticed that you made second mistake - you assigned "Y" to decision :) Remember,
= is assignment
== is comparision

If you want to be safe from this kind of mistakes, put constants first, like this
 `` `` ``if('Y' == decision)``

Now, if you made a mistake and instead of == used =, compiler would tell you that you are trying to assign something to constant. Otherwise, if you do assignment in if, you can have a bug that is hard to find.
Last edited on
ah, thanks. I should have realized that :(
closed account (iAk3T05o)
This is exactly what i'm doing and thanks to a forum member, i've added more functionality using loops.
You don't have any else if for when the user inputs 'N'.
Have you learnt string, stringstream and getline ?
Last edited on
Topic archived. No new replies allowed.