Thread: Check??Newbie..HELP!!

  1. #1
    Unregistered
    Guest

    Check??Newbie..HELP!!

    Hi,

    Its a newbie Q!!please help........
    I want to check if the integers entered are only numbers ,not strings/characters.

    I know isdigit function but i suppose its in character-handled library,but I am using class string.So is there any function for checking that input values are only integers not character/strings/symbols etc;


    thanks

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    scanf("%d", varname);

  3. #3
    Unregistered
    Guest

    Unhappy thanks...but

    Hi,

    thanks for replying, but i assume, u gave me a soln in c and not vc++,
    if,iam not wrong??I am unsure though.........
    thankx

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    yes it is C but it works just as well in C++. the header file is conio.h

    or at least it works on MY VC++, 4.0 that is

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    305
    you could do it manually... personally, i dont use the string data tye cuz it takes away all your power (muwhahaha). anywayz, use a char array and check the contents:
    Code:
        int i;
        char s[100];
        cin.getline( s, '\n' );
    
        for( i = -1; i != strlen( s ) + 1; i++ ){          
    //strlen checks the length of the string
            if( s[i] != '1' && s[i] != '2' && s[i] != '3' ){
                //your string has a symbol or whatever
                //this just check 1, 2, and 3, but you get the point (i hope)
                .
                .
                .
                break;     //breaks out of the for loop
            }
         }
    remember to include string.h
    im probably gonna receive some criticism for this post because some ppl might think the code "inefficient", but computers now are like 2ghz. welcome to reality folks!

    also, scanf is stdio.h not conio.h
    Last edited by Flikm; 04-06-2002 at 11:45 PM.

  6. #6
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Yeah.. instead of using the string class, you post O(n^2) code for something that could trivially be O(N) .
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  7. #7
    Unregistered
    Guest

    ????

    Hi,
    I dont understand what SilentStrike is tryin to say????
    Btw,thanks flikm and denethor2000
    But as this an assignment, its expected to use a string and not char array.......also only c++ will be allowed for obvious reasons...
    So can anyone please tell me how to check if the value entered is an integer and not strings/char/symbols???
    thanks..........

  8. #8
    Unregistered
    Guest

    Unhappy Please help!

    32 reads!!!still waiting!!!!!

  9. #9
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    >>32 reads!!!still waiting!!!!!

    ...meanwhile do a search on the board for integer input check... it might help
    -

  10. #10
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210
    If you absoloutely have to have a string you can always typecast a character array into a string.

    Code:
    (string) char;
    "The mind, like a parachute, only functions when open."

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    94
    if I haven't misunderstood your request, here is a nifty little while loop that clears and ignores the information that was in the buffer and does exactly what you want...the only thing it will accept is numbers

    Code:
    int num;
    
     cout <<"pls enter num";
     cin >> num;
    
     while (cin.fail())
     {
        cin.clear();
        cin.ignore (BUFFER, '\n');
        cout << "\n pls re-enter number";
        cin >> num;
     }
    NB: dont forget to create a BUFFER constant, that is :
    const int BUFFER=128; before main.

    sophie
    simple is always an understatement.....

  12. #12
    Unregistered
    Guest

    Smile thanks a bunch

    Hi sophie,
    it worked liked a charm......thanks a lot
    but can u enlighten me about what buffer=128 ,cin.clear(),cin.fail(),cin.ignore() funcns do??????
    thanks once again.

  13. #13
    Registered User
    Join Date
    Apr 2002
    Posts
    94
    firstly, Im glad you found what you were after as for what they do.

    the buffer constant you declared can be any size...I usually use 128...just like the sound of it the cin.fail() condition checks and returns a value, that is if the input was anything other than a number cin.fail() would return true (which is the condition for entering a loop).

    the cin.clear() and cin.ignore() does exactly as they say...they ignore and clear the buffer thus cin >> num will have a clean and empty buffer to get info from.

    sophie
    simple is always an understatement.....

  14. #14
    Unregistered
    Guest

    Thumbs up ok!got it!!

    thanks,
    I got it!!!!!!!!!

  15. #15
    Unregistered
    Guest

    Question another dumb Q

    Hi,
    is there any function like cin.fail() to check for the input value is only string and not numbers??????/
    thanks..........

Popular pages Recent additions subscribe to a feed