Thread: input of long character answers

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    9

    Angry input of long character answers

    the program always asks the questions in the same order and will not respond with correct when the answer is correct
    can u help me, thanks
    int main()
    {
    char answer [20];
    char ans = 'y';
    cout <<"Do you know the nicknames of these colleges . . . \n";
    cout <<"All the answers are plural (if possible), lowercase \n";
    cout <<"and do not include the word 'the' \n\n.";
    while(ans == 'y' || ans == 'Y')
    {
    int c=0, w=0;
    while(w + c < 5)
    {
    int choice = randMid(1, 9);

    if (choice == 1)
    {
    cout <<"What is the nickname of The University of Maryland?" << endl;
    cin.getline(answer, 20, '\n');
    if (answer == "terps" || answer == "terrapins")
    {
    cout <<"Correct!" << endl << endl;
    c++;
    }
    else
    {
    cout <<"Wrong" << endl << endl;
    w++;
    }
    }

    if (choice == 2)
    {
    cout <<"What is the nickname of Duke University?" << endl;
    cin.getline(answer, 20, '\n');
    if (answer == "bluedevils" || answer == "blue devils")
    {
    cout <<"Correct!" << endl << endl;
    c++;
    }
    else
    {
    cout <<"Wrong" << endl << endl;
    w++;
    }
    }

    if (choice == 3)
    {
    cout <<"What is the nickname of Georgia Tech University?" << endl;
    cin.getline(answer, 20, '\n');
    if (answer == "yellowjackets" || answer == "yellow jackets")
    {
    cout <<"Correct!" << endl << endl;
    c++;
    }
    else
    {
    cout <<"Wrong" << endl << endl;
    w++;
    }
    }

    if (choice == 4)
    {
    cout <<"What is the nickname of The University of Virginia?" << endl;
    cin.getline(answer, 20, '\n');
    if (answer == "cavaliers")
    {
    cout <<"Correct!" << endl << endl;
    c++;
    }
    else
    {
    cout <<"Wrong" << endl << endl;
    w++;
    }
    }

    if (choice == 5)
    {
    cout <<"What is the nickname of North Carolina?" << endl;
    cin.getline(answer, 20, '\n');
    if (answer == "tar heels" || answer == "tarheels")
    {
    cout <<"Correct!" << endl << endl;
    c++;
    }
    else
    {
    cout <<"Wrong" << endl << endl;
    w++;
    }
    }

    if (choice == 6)
    {
    cout <<"What is the nickname of North Carolina State?" << endl;
    cin.getline(answer, 20, '\n');
    if (answer == "wolfpack" || answer == "wolf pack")
    {
    cout <<"Correct!" << endl << endl;
    c++;
    }
    else
    {
    cout <<"Wrong" << endl << endl;
    w++;
    }
    }

    if (choice == 7)
    {
    cout <<"What is the nickname of Florida State University?" << endl;
    cin.getline(answer, 20, '\n');
    if (answer == "seminoles")
    {
    cout <<"Correct!" << endl << endl;
    c++;
    }
    else
    {
    cout <<"Wrong" << endl << endl;
    w++;
    }
    }

    if (choice == 8)
    {
    cout <<"What is the nickname of Clemson University?" << endl;
    cin.getline(answer, 20, '\n');
    if (answer == "tigers")
    {
    cout <<"Correct!" << endl << endl;
    c++;
    }
    if (answer != "tigers")
    {
    cout <<"Wrong" << endl << endl;
    w++;
    }
    }

    if (choice == 9)
    {
    cout <<"What is the nickname of Wake Forest University?" << endl;
    cin.getline(answer, 20, '\n');
    if (answer == "deamon deacons" || "deamondeacons")
    {
    cout <<"Correct!" << endl << endl;
    c++;
    }
    else
    {
    cout <<"Wrong" << endl << endl;
    w++;
    }

    }
    }
    cout <<"You got " << c <<" correct and " << w <<" wrong!" << endl << endl;
    cout << "Do you want to run this program again? (Y or N) --> ";
    cin >> ans;
    }
    return 0;
    }

    int randMid(int low, int high)
    {
    return low+rand()%(high-low+1);
    }

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If you want to compare char arrays with string literals you should use strcmp() , not the == operator. Otherwise you're comparing memory addresses and not strings.
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting using pointer to pointer not working
    By eager2no in forum C Programming
    Replies: 17
    Last Post: 09-21-2008, 12:52 AM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Problems with character input
    By OmnipotentCow in forum C Programming
    Replies: 19
    Last Post: 06-20-2003, 03:39 PM
  4. cin.getline question (user's input too long)
    By JerryL_MB in forum C++ Programming
    Replies: 1
    Last Post: 12-09-2002, 12:17 PM