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

This is a discussion on Help: if(UserNumber != a number!)... within the C++ Programming forums, part of the General Programming Boards category; I am trying to make a simple validator to make sure that if the user enters anything besides a four ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    18

    Question 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.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Check if it's great than or equal to 1000 and less than 10000

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    18
    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;
    }
    Last edited by mero24; 05-01-2005 at 09:41 PM. Reason: Code add-in

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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.

  5. #5
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    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.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 09:28 AM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 10:27 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 05:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 01:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21