"Not Equal" seems to be "Equal"

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?


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
40
41
42
43
44
45
46
47
48
#include "stdafx.h"
#include <iostream> 
#include <ctime> 

using namespace 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;
}

[/code]
I copied and pasted your program in VS2012 and it behaves as expected (i.e., requires an exact match).
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...
Last edited on
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 recommend that you add checking to validate the user input.
http://www.parashift.com/c++-faq/istream-and-ignore.html

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.
Topic archived. No new replies allowed.