Thread: how do you check to see if input is an int?

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    25

    how do you check to see if input is an int?

    if i want to take the input of a user and check to see an int, how do i do that?
    I Love Jesus

  2. #2
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160

    Lightbulb

    yea i have a similar problem that when you input a char(when you need an Int) or a bunch of chars it loops and loops and loops... i need a way to fix that...

    someone said something like
    Code:
    while(cin.fail())
    {
            cout<<"Enter an integer not a char: ";
            cin>>num;
    }
    but i dunno i havent tried it...
    Paro

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    25
    those aren't working. the second one doesn't let me input a new value for my variable
    I Love Jesus

  4. #4
    In The Light
    Join Date
    Oct 2001
    Posts
    598
    howdy,

    this function was given to me by another friend on this board. i dont remember who at this moment.
    it works just fine for what i need.


    Code:
    int IsIntt(char *input) //checks for input int
    {
     int bad = 0;
     int i;
    
     for(i = 0; i<strlen(input); i++)
        { 
         if(!isdigit(input[i]) && input[i] != '.')
           return 0;
         if(input[i] == '.')
           bad++;
        }
    
     if(bad)
     return 0;
    
     return 1;
    }
    all of the credit goes to someone else on this one.

    M.R.

  5. #5
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160

    Exclamation

    whoa! thats some high-tech shiz, and i dont understand half of it...isn't there a more nooblar way of doing it?

    gosh, im starting to find out how nooblar i really am
    Paro

  6. #6
    In The Light
    Join Date
    Oct 2001
    Posts
    598

    check it outhowdy,

    howdy,
    i,m going out of town right now, i will explain it to you tonight.
    notice how the input is read into the function.

    on hint:

    if(input[i] == '.')
    bad++;

    looks for decimal point and increments bad so 5.4 will return bad.

    M.R.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Fiddle around with this and see what is good and what is bad.
    Code:
    #include <iostream>
    using namespace std;
    
    int main ( void )
    {
      int i;
      if ( cin>>i )
        cout<< i <<" good"<<endl;
      else 
        cout<<"bad"<<endl;
      return EXIT_SUCCESS;
    }
    Generally anything that can't be truncated into an integer will cause the if to fail. So an alphabetic character or special symbol will fail but a floating point will be truncated and lose everything past the decimal.

    This is the easiest way, but the problem of not reading floating point numbers might not work for what you need it to do. But I don't see how it would unless the user entered 1.6 and wanted it to be rounded to 2.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    FULL WORKIN VERSION OF IS INTEGER

    No offense to the fine example, here is a full example of how to test for an integer entry with c++.

    Code:
    #include<iostream.h>
    #include<string.h>
    #include<stdlib.h>
    #include<ctype.h>
    
    
    int isinteger( char* sznumber );
    
    void main( void )
    {
      char buffer[100] = {0};
      int num = 0;
    
      while( num != -1 )
      {
           cout << "\nEnter a number(-1 to quit):  " << flush;
           cin.getline(buffer, 80);
    
            if( isinteger( buffer ) == 1)
            {
                 // convert to integer after verification
                 num = atoi( buffer );
    
                  cout << "You entered: " << num << endl;
            }
            else 
            {
                  cout << buffer << " is not a valid integer." << endl;
            }
        }
    }
     
    int isinteger( char* sznumber )
    {
       int iLength = strlen(sznumber);
    
       for( int i = 0; i < iLength; i++)
       {	
             //accept '-' only as first char
             if( sznumber[i] == '-' && i == 0 )
    	continue;
    
             if( !isdigit( sznumber[i] ) )
                  return 0;
      } 
    
          return 1;
    }
    zMan

  9. #9
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    ..or the concise version, using functionality already built into the istream classes -

    Code:
        int i;
        cin>>i;
    
        while(cin.fail())
        {
            cin.clear();
            cin.ignore();
            cout << "not integer\n";
            cin>>i;
        }

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    yeah but the functionality of that is cheesy

    the function automatically loops through your entry say for instance you enter w123 which is not an integer it will fail at first and when it encounters 123 it will pass. Also you must remember the cin is tied to cout and the behavior is screwy when trying to flush the input. I prefer getting all input as a line evaluating it and if it is screwed up you must re-enter it.
    zMan

  11. #11
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >the function automatically loops through your entry say for instance you enter w123 which is not an integer it will fail at first and when it encounters 123 it will pass. Also you must remember the cin is tied to cout and the behavior is screwy when trying to flush the input.

    Ok, then you'd clear the buffer -

    Code:
    int i;
        cin>>i;
    
        while(cin.fail())
        {
            cin.clear();
            cin.ignore(80,'\n');
            cout << "not integer\n";
            cin>>i;
        }
    What would happen in your case if you were to enter different data types on the same line?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  3. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  4. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  5. Passing a double array to a function as an argument
    By Glirk Dient in forum C++ Programming
    Replies: 20
    Last Post: 09-10-2003, 02:54 PM