While Statements Question

Hello,
I am writing a C++ program that displays a multiple choice question, and then asks the user for the answer. I am posting this because I have a couple questions regarding errors I receive. Below is the code.

- I will use a "do-while" loop, to keep the program running until the user gets the answer correct
- I will use "if" statements to give user clues when they get the answer wrong.
- I use an "if" statement when the user types in an answer not on the list of answers for the question, to let them know that they did not enter a valid answer.

Here is the Code:
------------------
#include <iostream>

int main()
{
char Color;
char A
char B
char C
do
{
cout << "What Color is the Sky?" << endl;
cout << "A. Blue" << endl;
cout << "B. Green" << endl;
cout << "C. Red" << endl;
cout << "A, B, or C?" << endl;
cin >> Color;
}
while (Color == 'B' );
while (Color == 'C' );

if (Color == 'B' )
{cout << "Hint: Starts with the letter B" << endl;}
if (Color == 'C' )
{cout << "Hint: Starts with the letter B" << endl;}
if (Color != 'A')
{cout << "You must enter either A, B, or C" << endl;}
if (Color != 'B')
{cout << "You must enter either A, B, or C" << endl;}
if (Color != 'C')
{cout << "You must enter either A, B, or C" << endl;}
if (Color == 'A' )
{cout << "Correct!" << endl;}

return 0;
}
------------------------------
------------------------------

Here is my errors:

quiz.cpp: In function `int main()':
quiz.cpp:12: syntax error before `char'
quiz.cpp: At top level:
quiz.cpp:23: syntax error before `while'

------------------------------
------------------------------

I hope that someone can help me out with why there are errors and what I can do to fix them.
It would be greatly appreciated!
Thank you!
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
#include <iostream>


int main()
{
char Color;
char A;
char B; // missing ';' at the end
char C;
do
{
cout << "What Color is the Sky?" << endl;
cout << "A. Blue" << endl;
cout << "B. Green" << endl;
cout << "C. Red" << endl;
cout << "A, B, or C?" << endl;
cin >> Color;
}while (Color != A ); // cant have to while () for one do i think
                     // also you can just do what i have

if (Color == 'B' )
{cout << "Hint: Starts with the letter B" << endl;}
if (Color == 'C' )
{cout << "Hint: Starts with the letter B" << endl;} // same thing with while
                                                   //can just do != A
// but that wont ever show up because you have to get the question right
//to exit the loop. suggeting puting it inside the loop
if (Color != 'A')
{cout << "You must enter either A, B, or C" << endl;}
if (Color != 'B')
{cout << "You must enter either A, B, or C" << endl;}
if (Color != 'C')                                   // can use or(||) 
{cout << "You must enter either A, B, or C" << endl;}//since your saying 
                                                     //the same thing

if (Color == 'A' ) 
{cout << "Correct!" << endl;}

return 0;
}


hope that helps :D
and added some suggesions
Last edited on
use code tags to paste code to make it easier to read

first of all, lets think about what you want to do.

you want to output a question and 3 answers to the user, if the user gets it wrong you want to reiterate the question ans ask for input again.

in order to test multiple cases you're going to need 'or'

1
2
do{
}while(color =='B' || color=='C');


but you can be clever and just check if the answer is right instead of two checks

1
2
3
4
5
6
7
do{
// output question
//output valid answers
//get input
//use if statements to determine what to output to user
// if color ==A while will terminate
}while(color != 'A');


in addition your going to need to do your checks inside your loop after you get the input.

output correct after the loop because you know if you exit the loop they will be correct
Last edited on
Hi hope this helps as I think this is what you're trying to do....
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
#include <iostream>
//include the string class!!!!!
#include <string>

//you must use a namespace!!!! since you're using cout and cin!!!!!
//That was a cause of some of your errors... 
using namespace std;

int main() {
	char Color;
	//Why do you need these, since you never assign them a value
	//suggest you dont use them at all....
	//char A
	//char B
	//char C

	//You could instead create a string to hold all this if you
        //intend to use it at more than one location in your program
	/*
	*string user_info =  "What Color is the Sky? \n A. Blue\n";
	*user_info += "B. Green\nC. Red\nA, B, or C?"
        */

	//I seems like you should only ask this once if so then I
	//would do this before the while, otherwise you could include
	//   cout << user_info << endl;  here once and  inside the do 
       // while as well, which ever you think is best...

	cout << "What Color is the Sky?" << endl;
	cout << "A. Blue" << endl;
	cout << "B. Green" << endl;
	cout << "C. Red" << endl;
	cout << "A, B, or C?" << endl;

	//It seems to me this is what you want to do inside the loop
	do
	{
		cin >> Color;
		//If you get the correct answer no need to carry on
		if (Color == 'A') {
			//Inform user he was correct
			cout << "Correct!" << endl;
			//And Break out of loop and finish program
			break;
		}
                //Note inside this condition you could also write
                // ( Color == 'B' || Color == 'b') to consider lowercase
		if (Color == 'B') {
                	cout << "Hint: Starts with the letter B" << endl;
		}
		if (Color == 'C') {
			cout << "Hint: Starts with the letter B" << endl;
		}
	      //You should combine the 'B' and 'C' if's statements
             //And to do this you need to use && 'and' not || or,
             // to make sure that neither B nor C was giving as 
             //an answer. And you off course dont need to consider
	     //the option where Color != 'A'
		if (Color != 'B' && Color != 'C') {
			cout << "You must enter either A, B, or C" << endl;
		}
	}
	while (Color != 'A');

	return 0;
}
Last edited on
Topic archived. No new replies allowed.