While Statements Question

This is a discussion on While Statements Question within the C++ Programming forums, part of the General Programming Boards category; Hello, I am writing a C++ program that displays a multiple choice question, and then asks the user for the ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    21

    Question 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:
    Code:
    // name: quiz.cpp
    // author: Kautz
    // date: November 9, 2008
    // description: Multiple Choice Question
    
    #include <iostream>
    
    int main()
    {
            char Color;
    
    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 , B, C )
    {cout << "You must enter either A, B, or C" << endl;}
            if (Color == A )
    {cout << "Correct!" << endl;}
    
    return 0;
    }
    Here is a list of errors that appear:
    Code:
    quiz.cpp: In function `int main()':
    quiz.cpp:21: `B' undeclared (first use this function)
    quiz.cpp:21: (Each undeclared identifier is reported only once
    quiz.cpp:21: for each function it appears in.)
    quiz.cpp:22: `C' undeclared (first use this function)
    quiz.cpp:28: `A' undeclared (first use this function)
    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!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You don't have any variables called A, B, or C. Perhaps you want "B" and "C" instead.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    21
    Quote Originally Posted by tabstop View Post
    You don't have any variables called A, B, or C. Perhaps you want "B" and "C" instead.
    What do you mean?
    Where would I put "B" and "C" instead?
    I don't understand what you're saying.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    B is a variable, called, well, B. You don't declare any variables with that name, so you can't use it.

    "B" is a string that consists of the letter B. Since that's what you want, that's what you should use. (As in color == "B", etc.)

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Several bugs in there:
    - Have a look at how while loops work. This:
    Code:
    do {
    /*something1*/
    while(a);
    while(b);
    Is identical to:
    Code:
    do {
    /*something1*/
    while(a);
    
    while(b) {
    /* Nothing here, just loop indefinitely*/
    }
    Which you probably don't want.

    2. As stated, you can't just type A, B or C in the program. But they aren't strings either, they are supposed to be character constants, so they need to be in single quotes. Eg: 'A', 'B'.
    So:
    Code:
    char Color;
    if(color == A){}  /* Wrong, A is not defined here (unless we defined it earlier) */
    if(color == 'A'){} /* Correct */
    3. You can't do
    Code:
    if(something != value1,value2,value3)
    If you really think that, then pick up some really basic book or tutorial on programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-20-2009, 11:11 PM
  2. Multiple If Statements Question.
    By thekautz in forum C++ Programming
    Replies: 3
    Last Post: 11-07-2008, 02:06 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  5. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 12:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21