Hello. I am a programming student and I was working on a random number generator exercise when I decided that it might be cool to take input from the user and see how many loops it takes before the user's number comes up randomly.
The program I wrote "works", in that it compiles and runs fine, but the behavior is wrong. If I put in 25 as the input, it stops as soon as it finds any number starting with 25 (2501, 2594, 259984, etc.). The same if you put in a single digit or three or four. It stops as soon as that many digits (from left to right) of the generated number match the input number. I thought using "!=" as my condition would mean LITERALLY that it had to match perfectly, but it seems to match from left to right and that's good enough! :)
Why is it doing that? Did I use the wrong operator? Is my logic faulty?
#include "stdafx.h"
#include <iostream>
#include <ctime>
usingnamespace std;
int main ()
{
int attempt = 1;
int r = 0;
int userNumber;
int userStart =1;
while (userStart == 1)
{
//get the user to enter the random number to find
cout << "\n" << "Please enter a whole number to find: ";
cin >> userNumber;
cout<< "\n\n" << "You entered " << userNumber << "." << "\n" << endl;
cout<< "Searching for your number..." << endl;
cout << endl;
//use computer clock time to seed the random number generator
srand( (unsigned)time( NULL ) );
//generate random numbers until the user number is found
while (r != userNumber)
{
// generate the numbers and count attempts
r = rand(); cout << "Attempt: " << attempt;
cout << " Current Random Number is: " << r << "\r";
attempt = attempt + 1;
}
cout << endl;
cout << "\n" << "Found!" << "\n" << endl;
cout << "Enter ""1"" to try again, or any other key to exit. " << endl;
cin >> userStart;
}
return 0;
}
Okay, now I feel like an idiot! The carriage return I added to make "cout" print continually on the same line was leaving "junk" behind and causing the numbers to look wrong if they were less digits than the maximum number. For example, when I entered 25 as the input, it DID stop on 25, but the previous random number was a a five-digit number that ended with 949 (or whatever), and since it printed over the previous line (but not erasing the old one), the last attempt printed over only the first two digits, leaving the number "25949"...sorry for wasting your time with that...
You're running only standard C++ so it should work as expected regardless of OS so long as the compiler is good, and I'd hope MS's would do something this incorrect. I suppose worst case you've found a bug, but it's got to be something else...
Still, I'm at a loss as to what would be causing that. Perhaps you could try doing a rebuild (clean + build) and see if that helps.
I would also recommend using do..while rather than the while loop. I would also recommend initializing r within the first while loop.
There is nothing wrong with your understanding of operator!= but the C++ streams have always irritated me. If you don't validate inputs, and/or flush the streams when errors occur then all bets are off.