# Help: if(UserNumber != a number!)...

• 05-01-2005
mero24
Help: if(UserNumber != a number!)...
I am trying to make a simple validator to make sure that if the user enters anything besides a four digit number, (ANY four digit number) the program returns an error message. How can I do this without entering all the numbers (or all the non-numbers)?
-Mike
P.S. The number does not have commas.
• 05-01-2005
sean
Check if it's great than or equal to 1000 and less than 10000
• 05-01-2005
mero24
It could be equal to 1000, but the max is 9999.
Edit: It should be maxed at 9999.

Code:

```#include <iostream> #include <string> #include <time.h> #include <cstdlib> #include <ctime> using namespace std; int RandomNumber() { srand (time(NULL)); //Seeds the random number (random number src?) int rFourDigitNumber=rand()%9999+1; //Provides the range for the random number return rFourDigitNumber; if(rFourDigitNumber) {         //std::cout<<"Your four digit random number is..."<<rFourDigitNumber<<endl; } } int UserInput() {         int UserNumber;         std::cout<<"Please guess the four digit number"<<endl;         std::cin>>UserNumber;         return UserNumber;         if(UserNumber != ?????                 std::cout<<"Sorry, Invalid Decision"<<endl;                 Some kind of loop to keep it going until valid decision? } int main() { RandomNumber(); system("PAUSE"); return 0; }```
• 05-01-2005
sean
Code:

```while(UserNumber < 1000 || UserNumber > 9999) { ... }```
While we're talking, you should be aware that you're using obsolete C++ - headers are used differently now.

- If it's a standard header, you don't need the .h
- If it's a standard C header, and not C++ only, precede the name with a c.

For example stdlib.h, a C library, would be cstdlib.

You also need to use namespaces. The simplest way to do this is just to add "using namespace std;" after you do your #include's.

Also, the use of system("PAUSE") has several problems, since system executes the string given to it as though it was a system command - in this case, it's executing PAUSE.exe or .com or something like that. Here are some details:

http://www.cprogramming.com/tips/sh...count=30&page=1

Here are some alternatives:

http://faq.cprogramming.com/cgi-bin...5&id=1043284385

I'd also advise against the method that uses getch() from conio, as that is a nonstandard header.
• 05-02-2005
treenef
I can see a few problems with the random number generator. As you may have already noticed the numbers generated depends on the time you execute your program.

This means it is possible to have an exception in your program such that a four digit number will not always be generated. Moreover, the condition( <1000|| >9999) doesn't necessarily account for this exception.

Perhaps, you should begin to look into more sophisticated pseudo random number generators, such as the 'Mersenne Twister.'

Additionally, when using functions you have to pass the variable
'rFourDigitNumber' into 'UserInput' so that it checks if the password is the same. I realise that the program you posted was probably just a draft version, so you might have been planning to do this anyway.
However, I am just pointing it out for completion.

:confused: