Broken code: srand and cin.get

Hi. I am just starting out learning C++ as a hobby. I am writing programs from exercises out of a book. The "Math Tutor" exercise I am working on right now asks me to:

1. Display 2 random numbers to be added.
2. Have the program pause while the student works out the answer.
3. When the student is ready a key can be pressed for the program to display the correct solution.

I did this fine with just rand and cin.get, but it annoyed me that I always got the same random numbers. Even though the program worked, I changed it to use srand, but now it is broken. I can't figure out why.

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
// This is a math tutor program.
// It displays a math problem using two random numbers.

#include <iostream>
#include <iomanip>
#include <stdlib.h>

using namespace std;

int main(){
    unsigned seed;
    int number1, number2, number3;
    char ch;

    cout << "Enter a seed value." << endl;
    cin >> seed;
    srand(seed);

    number1 = 1 + rand() % 999;
    number2 = 1 + rand() % 999;

    number3 = number1 + number2;

    cout << endl << "  " << setw(3) << number1 << endl;
    cout << "+ " << setw(3) << number2 << endl;
    cout << "_____" << endl << endl;
    cout << "Press enter for answer." << endl;

    cin.get(ch);

    cout << "  " << setw(3) << number1 << endl;
    cout << "+ " << setw(3) << number2 << endl;
    cout << "_____" << endl << endl;
    cout << "  " << setw(3) << number3 << endl;

    return 0;


}


The problem is that it skips the "press enter to continue." After the seed is entered the whole thing runs to the end.
Last edited on
Put cin.ignore(); after the cin >> seed;. When you get something from cin in that manner it tends to leave a new line (\n) in the buffer, and when you call cin.get() afterwards, it just immediately finds and grabs that \n that's already there. cin.ignore() will get rid of that new line that's left over and leave you with an empty buffer, so cin.get() is forced to wait for the user to enter a new line, as you want it to.
In addition, if you'd prefer, an easier way to do srand() is to use the <ctime> header, and call srand with time(NULL) as the value. (the current Unix timestamp, changes every second.)
Last edited on
I tried your first suggestion and it worked. Thanks for that. Then I tried your second suggestion, but it isn't giving me different numbers. This is what I have changed it to.

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
// This is a math tutor program.
// It displays a math problem using two random numbers.

#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <ctime>

using namespace std;

int main(){
    unsigned seed;
    int number1, number2, number3;
    char ch;

    srand(NULL);

    number1 = 1 + rand() % 999;
    number2 = 1 + rand() % 999;

    number3 = number1 + number2;

    cout << endl << "  " << setw(3) << number1 << endl;
    cout << "+ " << setw(3) << number2 << endl;
    cout << "_____" << endl << endl;
    cout << "Press enter for answer." << endl << endl;

    cin.get(ch);

    cout << "  " << setw(3) << number1 << endl;
    cout << "+ " << setw(3) << number2 << endl;
    cout << "_____" << endl;
    cout << "  " << setw(3) << number3 << endl;

    return 0;


}
He meant srand(time(NULL)); not srand(NULL);.

Edit:
Anyway what what compiler are you using? If it's recent enough it may implement C++11's random library (which is supposed to produce better quality numbers than rand()).
http://cplusplus.com/reference/std/random/

Last edited on
Awesome. :)

Thanks. It's perfect now. :)
Topic archived. No new replies allowed.