Thread: Help with random numbers and loops.

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    7

    Help with random numbers and loops.

    Hello everyone. I just got an assignment for my c++ class. I am almost done with it but im having hard time creating different random numbers. The assignment is "The program you will write is a History quiz. The following is a list of the first 10 US presidents in the order in which they were in office, along with the start and end years of their service. Write a program that will keep displaying the name and years in office for a president randomly selected from this list and ask the user to guess his chronological order. For example, if the program randomly selects and displays Thomas Jefferson (1801-1809), the right answer is 3 since he was the 3rd president. If the user enters the correct number, the program prints "Right!" and increments the correct answers count; otherwise, it prints "Wrong!" The program stops and displays the number of correct answers once 10 questions have been asked.If the president selected by the program is the same as the last one, have the program continue to randomly choose another until it's different form the last one.
    We want to be lenient on the student, so for each question, allow 3 trials. If any of the 3 trials produces a correct answer, it should be counted as correct. Only if all 3 answers were wrong, show the right answer and don't give credit for the question. So, for each wrong answer, tell the user "Wrong!' and have him or her try again, for a total of 3 times. You must not repeat code, including for the 3 trials. "
    but the program i wrote print the same president twice sometimes. I cant figure out a way to fix the bug. The code i wrote is below.

    Code:
    #include <iostream>
    #include <conio.h> 
    #include <iomanip> 
    using namespace std;
    int main()
    {
        int number, last = 0, correct = 0, answer;
    
    
        for (int i = 0; i < 10; i++)
        {
            do
            {
                srand((unsigned)time(0));
                number = 1 + rand() % 10;
                if (number == last)
                    number = 1 + rand() % 10;
    
    
                else
                {
                    if (number == 1)
                        cout << "George Washington (1789-1797) :";
                    else if (number == 2)
                        cout << "John Adams (1797-1801) :";
                    else if (number == 3)
                        cout << "Thomas Jefferson (1801-1809) :";
                    else if (number == 4)
                        cout << "James Madison (1808-1817) :";
                    else if (number == 5)
                        cout << "James Monroe 1817-1825) :";
                    else if (number == 6)
                        cout << "John Quincy Adams (1825-1829) :";
                    else if (number == 7)
                        cout << "Andrew Jackson (1829-1837) :";
                    else if (number == 8)
                        cout << "Martin Van Buren (1837-1841) :";
                    else if (number == 9)
                        cout << "William H. Harrison (1841) :";
                    else if (number == 10)
                        cout << "John Tyler (1841-1845) :";
    
    
                    break;
                }
                } while (last = number);
                number = number + 1;
            
    
    
            cout << "\n\nEnter the order number for each of the following presidents: ";
            cin >> answer;
    
    
            for (int j = 0; j < 4; j++)
            {
                if (answer == number)
                {
                    cout << "\nRight!\n\n ";
                    correct++;
                    break;
                }
                else if (j == 3)
                {
                    cout << "\nWrong! The correct answer is " << number << "\n\n";
                    break;
                }
                else
                {
                    cout << "\nWrong! Try again :";
                    cin >> answer;
                }
            }
        }
        cout << "You answered " << correct << " of 10 correctly.\n";
    
    
        cout << "\nPress any key to continue.";
        _getch();
        return 0;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    You should call srand() only once, preferably at the start of your program.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with finding only prime numbers with for loops
    By AlmondSeason in forum C++ Programming
    Replies: 11
    Last Post: 02-19-2013, 12:51 AM
  2. Replies: 4
    Last Post: 11-16-2004, 07:29 AM
  3. Obtaining a random object with loops
    By Gnoober in forum C++ Programming
    Replies: 2
    Last Post: 10-12-2002, 02:31 PM
  4. For/Next Loops...adding 10 numbers...
    By IanelarAzure in forum C++ Programming
    Replies: 5
    Last Post: 09-12-2002, 12:02 PM

Tags for this Thread