Thread: More input problems

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    More input problems

    What I need to do is check the format of some data entered by the user. I ask them to enter their age, and I need to check they don't enter a letter, for example.

    Basically, I have no clue how to do the above.

    Also, I need to read a name into a std::string buffer, but I need to account for spaces entered. I've tried various approches (too many to post really) and nothing works; when the user enters a space in their name, it trips an infinite loop.

    I've had to rush out so I can't provide any example right now :|
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    One possibility would be to use getline for input. getline will not stop readin when it encounters a space.
    You could use isdigit to validate integer input and atoi or a stringstream to convert to int.
    Kurt

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    to get a line of input including spaces check out getline()

    For verification there are a bunch of different methods. One is to read in the input into a string and then parse the string. Another method is to check the state of the stream:
    Code:
    int age;
    bool g2g = false;  // g2g = good to go
    
    do
    {
      cin >> age;
      if ( cin.fail() )
      {
        cout << "I said age so give me an integer dang it!"<<endl;
        cin.clear();  // clear the failed state
        cin.ignore();  // remove the offending character and try again
      }
      else
        g2g = true;
    }while ( !g2g );
    However this has its on unique set of problems. To test it try an input of:
    abc123def

  4. #4
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    To overcome this:

    Code:
    abc123def
    You could write your program to handle every possible outcome that would be considered invalid.

    The following prog would overcome your problem regarding whitespace and invalid input, however, the program would be considered cumbersome. Moreover, it doesn't use strings which would probably be frowned upon in c++.

    Code:
    #include <iostream>
    #include <fstream>
    #include <ctype.h>
    
    
    using namespace std;
    int main()
    {
        char array[100];
        cout<<"Please enter your age>>";
        cin.getline(array,100);
        
        int size=strlen(array);
        int cond=0;
        
        for (int i=0; i<size; i++)
        {
            if(isdigit(array[i])==0)
            {
                cond=1;
                
            }
        }
        if (cond==0)
        {
            cout<<"Valid input";
            //do rest of program
        }
        else
        {
            cout<<"Invalid";
            // or loop back to original question
        }        
        cin.get();        
        
    }

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Except that your program would validate it if there was at least one number.

    Code:
        for (int i=0; i<size; i++)
        {
            if(isdigit(array[i])==0)
            {
                cond=1;
                
            }
        }
    Lets take a look at my test case of "abc123def"

    So for "abc" cond will still be 0. For each of "123" it will then set cond to 1. Then finally for "def" it leaves cond alone. So at the end cond is 1, yet you still don't have a valid input.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    @Thantos: You mixed up sth. isdigit is tested against 0;
    that effectively means
    if ( ! isdigit() ) cond = 1;
    1 means not valid;
    Kurt

  7. #7
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Yes the notation for isalpha is slightly confusing. But it should work.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Bah yeah, skipped over that == 0 part

    Of course the next question is if "123def" should be read as 123 and then "def" or rejected as invalid.

  9. #9
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Thanks for all your help guys, I'll have a fiddle
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 09:12 PM
  2. Problems with input and fflush
    By edugarcia in forum Linux Programming
    Replies: 1
    Last Post: 11-24-2004, 01:52 PM
  3. Custom Made Safe Input Function
    By Beast() in forum C Programming
    Replies: 6
    Last Post: 08-21-2004, 10:19 PM
  4. Input Problems and C# (CSharp) Tutorials
    By Grayson_Peddie in forum C# Programming
    Replies: 4
    Last Post: 02-27-2003, 10:45 PM
  5. Problems with commas as input
    By Yojimbo III in forum C++ Programming
    Replies: 3
    Last Post: 02-05-2003, 09:18 PM