Thread: random numbers and responses

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

    Angry random numbers and responses

    the program asks questions in the same order, how can i fix that?
    for some questions any answer is seen as correct...?

    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 (strcmp(answer, "terps") || strcmp(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 (strcmp(answer, "bluedevils") || strcmp(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 (strcmp(answer, "yellowjackets") || strcmp(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 (strcmp(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 (strcmp(answer, "tar heels") || strcmp(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 (strcmp(answer, "wolfpack") || strcmp(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 (strcmp(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 (strcmp(answer, "tigers") || strcmp(answer, "tiger"))
    {
    cout <<"Correct!" << endl << endl;
    c++;
    }
    else
    {
    cout <<"Wrong" << endl << endl;
    w++;
    }
    }

    if (choice == 9)
    {
    cout <<"What is the nickname of Wake Forest University?" << endl;
    cin.getline(answer, 20, '\n');
    if (strcmp(answer, "deamon deacons") || strcmp(answer, "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;
    }

    //creates a random number between a low number and high number
    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
    You'll have to seed the random number generator -

    srand((unsigned)time(0));

    Include <ctime> to use the time function.

    strcmp() returns zero if the strings are equal.
    zen

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

    i dont understand

    i dont understand after u compare strings how to say if they are equal to do something and if not do something else.
    you said it returns 0 if they are true so can u do an example for one of the parts.

    thanks,
    you're a big help

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    if (strcmp(answer, "terps")==0 || strcmp(answer, "terrapins")==0)
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Big (and small) numbers...
    By swif in forum C Programming
    Replies: 6
    Last Post: 04-22-2005, 12:21 PM
  2. random messages generator?
    By pancho in forum C Programming
    Replies: 3
    Last Post: 03-14-2002, 03:57 PM