Thread: Digit Check

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    25

    Exclamation Digit Check

    I need some help with checking whether or not the input from the user is 5 digits or not. It needs to be exactly 5 digits and for example 00000 should be a valid input so I can just check if it is between 10000 and 99999. My brain is just not working right now and I need help.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    25
    That helps but how do i use that to check if it is exactly 5 digits. my input has to be all at once and not one at a time.

    Thank you btw for the help it was a good help

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I would get the number as a string then convert to your number after checking length.

    If you are using std::string then check std::string.length(), for a cstring use strlen() to check the length.


    Jim

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    25
    Thank you so much

    I just have one more question. once i convert the string to an int will i lose my leading zeros and if i do how do i prevent that? for example if the user types in 00000, which should be a valid in put and i convert it to an int won't it display as just 0? I need it to display as 00000

    I read some where that there is a %9 method for checking but i did not understand what they were doing. It was just code that I was reading....would some thing along those lines work?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    It depends on what you are actually doing. Do you really need to convert to a number? Or can you stick with the string?

    A little code would probably help.

    Jim

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    25
    for the program to work i do not need to use an int i can stick with a string. the numbers represent a bank account number but the instructions say to use an int. in fact it even says the program should take it in as an int

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Ok, since the instructions say take it as in int then that's the way you should do it. If using an int it shouldn't matter if the number entered is 100 or 000100. The only thing you should worry about is if the number is too big.


    Jim

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    25
    how do i make it display as 5 digits and make sure that it is 5 digits if it is an int?

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    When you display it you can use a combination of std::setw() and std::setfill().

    Jim

  11. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    25
    ok that solves part of my problem but if the user types in 100 that is not valid but if her types in 00100 it is valid how do i check for that as an int? it seems like I;m checking for key strokes...

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You don't need to check the length of the int you need to check the value. Even if you enter 00001 to an int what gets stored is 1.

    the numbers represent a bank account number but the instructions say to use an int. in fact it even says the program should take it in as an int
    Your instructions do say to use an int?

    If you are using it as an int as your instructions state then don't worry about he length, unless your instructions say something different.

    Jim
    Last edited by jimblumberg; 10-20-2010 at 08:31 PM.

  13. #13
    Registered User
    Join Date
    Oct 2010
    Posts
    25
    Yeah my instructor said that he's checking for the length most of all like if he puts in 1234 he does not want it to be valid but if her puts in 01234 he wants it to be valid.

    he also wants us to tell him if the user didn't put enough digits or too many
    But to my understanding if those are both in an int they would be stored as the same thing....
    Last edited by SpockRox08; 10-20-2010 at 09:16 PM.

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Then you must use a string to get the data from the user.

    Jim

  15. #15
    Registered User
    Join Date
    Oct 2010
    Posts
    25
    Thank you. You have been really helpful

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. check digit
    By Jailan in forum C Programming
    Replies: 30
    Last Post: 09-20-2007, 05:52 PM
  3. Check application visibility
    By 3saul in forum Linux Programming
    Replies: 2
    Last Post: 02-13-2006, 05:13 PM
  4. newbie programmer - needs help bad.
    By hortonheat in forum C Programming
    Replies: 17
    Last Post: 10-20-2004, 05:31 PM
  5. Roman number converter
    By BalanusBob in forum C++ Programming
    Replies: 8
    Last Post: 04-23-2002, 06:29 AM

Tags for this Thread