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.
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.)
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.
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/