C++ String Question

This is a discussion on C++ String Question within the C++ Programming forums, part of the General Programming Boards category; Ok, basically I have a user inputted string named ssn, for entering social security numbers. I have the user input ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    2

    C++ String Question

    Ok, basically I have a user inputted string named ssn, for entering social security numbers. I have the user input it with a getline function, but after that I need to test this string to make sure a 9 digit number was entered into it. How would I check this string to make sure all of the characters are numbers (0-9)?

    None of the string functions I've used seem to work with this, and tend to have problems because the string values are characters.

    Any help would be much appreciated, I honestly can't find help for this in either of my books.

    Silverwolf

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    In the <cctype> library there exists a function isdigit() that accepts a character and returns true if the character is a digit 0-9 and false otherwise.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    2
    Thank you very much! I knew I was overlooking something. I really appreciate it.

    Silverwolf

  4. #4
    Registered User Mr_Jack's Avatar
    Join Date
    Oct 2003
    Posts
    63
    I would have tried something like
    Code:
    for(int a = 0; a < 7; a++)
    {
    if(number[a] != '0' || '1' || '2' ...
    but that didn't work. I don't know what to tell you. You might have to say
    Code:
    if(number[a] == 'a')
    {
    //error
    }
    for every character. Something like
    Code:
    if(number[a] == 'a' || 'b' || 'c')
    {
    //error
    }
    doesn't work either on my compiler. Good luck.

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Originally posted by Mr_Jack
    I would have tried something like
    Code:
    for(int a = 0; a < 7; a++)
    {
    if(number[a] != '0' || '1' || '2' ...
    but that didn't work. I don't know what to tell you.
    Mr_Jack - I think joshdick was able to give a good solution to wolff76. Just for your knowledge, to do it the way you were trying to do it, you must explicitly type out "number[a] != " for each comparison. It doesn't work like in english where you can say "x does not equal 1 or 2 or 3". The correct statement is, "x does not equal 1 and x does not equal 2 and x does not equal 3."
    Code:
    for(int a = 0; a < number.length(); a++)
    {
        if(number[a] != '0' && number[a] != '1' && number[a] != '2' ...)

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    See the custom validation section (OK, that example is mainly in C, but the validation function is generic).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Reusing a string pointer question
    By chiefmonkey in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2009, 04:53 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 05:44 PM

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