Thread: need help with integers

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    8

    need help with integers

    Hey

    How do i ensure a user only enters 4 values into an integer.

    entered values will range from 0000-9999

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by tomisme View Post
    Hey

    How do i ensure a user only enters 4 values into an integer.

    entered values will range from 0000-9999
    Code:
    do   {
       //print your message here, asking them to enter a number between 0 and 10000
       //get your number in here from the user
    
    }while(number < 0 || number > 10000);
    Note that "0000" is not an integer. Integers are never proceeded by a zero.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    8
    thats works

    thanks

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Read the user input as a string and then parse using strtol(). Search the forum for strtol(). Cast to an integer to convert from long to int. Use printf() formatting to add leading zeros when you report the value.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Remember that kind of loop, please.

    It's used a great deal in all kinds of programs.

    You're welcome.

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    8
    on second thoughts what stops the user from entering 1,2 or 3 values insted of the required 4?

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    87
    Are you saying the number has to consist of exactly four digits? Or that at most it consists of four digits? If you want the former, you just have to change the condition at the bottom of the do...while loop posted above.

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    8
    yes sorry i should have mentioned that the value entered by the user will be 4 values.

    its a uni project part of the criteria is that i have to make sure that the ammount of digits entered is 4 and nothing else.

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Then your first loop condition will be to ensure the user enters 4 characters. Therefore, you have to accept it as a string, not as a number.
    Mainframe assembler programmer by trade. C coder when I can.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Elysia View Post
    Not necessarily true.
    Just set the loop condition to:
    number < 1000 || number > 9999
    Did you forget something?:
    entered values will range from 0000-9999
    Todd Burch was right.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It seems I placed some text in a reply it shouldn't have been :|
    Yes, it seems Todd is right...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  2. Integers into array.
    By livestrng in forum C Programming
    Replies: 10
    Last Post: 10-29-2008, 11:35 PM
  3. Replies: 8
    Last Post: 10-24-2005, 11:26 AM
  4. Scanning integers from a file:
    By xarmenx in forum C Programming
    Replies: 6
    Last Post: 11-29-2004, 04:57 PM
  5. Replies: 6
    Last Post: 08-04-2003, 10:57 AM