determining integer/non-integer

This is a discussion on determining integer/non-integer within the C++ Programming forums, part of the General Programming Boards category; hi, how do i write a program which determins whether the number entered is integer or non-integer? any suggestions? thanks...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    3

    determining integer/non-integer

    hi,
    how do i write a program which determins whether the number entered is integer or non-integer?
    any suggestions?
    thanks

  2. #2
    In The Light
    Join Date
    Oct 2001
    Posts
    598
    howdy,
    this function was given to me by someone on this board. it seems to work pretty well for determining if user input is an int or not.


    Code:
    int IsIntt(char* input) //checks for input type 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;
     }
    M.R.

  3. #3
    Unregistered
    Guest
    using the fail(), good(), clear() memberfunctions of istreams is one way.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You guys keep overcomplicating things. If all you want to do is find out if the data entered is a number, just use cin inside an if statement. If the input is anything but a number then it will fail.
    Code:
    #include <iostream>
    
    int main( void )
    {
      int i;
      if ( std::cin>>i )
        std::cout<<"Valid\n";
      else
        std::cout<<"Invalid\n";
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Unregistered
    Guest
    It is my understanding that use of fail(), clear() allows detection of invalid input without crashing or the need to exit program by allowing you to clear the fail bit, thereby resetting cin for additional input.

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

    i tryed it

    howdy,
    i tryed it but when i enterd "4.d" it returned valid.
    the one i posted works pretty well with multiple chars.
    the down side is that to use the result as a number you have to convert it to an int.

    Code:
    cin>>guess;  
       if(!IsIntt (guess))
         {cout<<"Must be an integer"<<endl;}
         gues = atoi (guess);
         ....
    what do you think?

    M.R.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    3
    thank you all for the effort....

    prelude,
    your code seems to work fine, except that when i enter an integer, the system doesn't do anything.... when i enter non-integer it prints out 'invalid'.
    any suggestions?

    i can use only if,for & while statements & only include <iostream> so it has to be as simple as possible.

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

    oops - sorry

    howdy,
    sorry if i exceeded the bounds of your assignment. i'm teaching my self so i find the ones that more experienced users like Prelude post quit helpful.

    M.R>

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    3

    complete assignment

    maybe it'll make more sense if i explain the whole thing...

    i want to constantly ask the user whether he/she wants to enter a value(y/n)

    if 'y'...
    i want to test the value for integer/nonInteger & even/odd & add to the counter accordingly....
    if 'n'
    if dispalys
    1)no. of values entered
    2)odd values entered
    3)even values entered
    4)sum
    5)average
    6)integer values entered
    7)nonInteger values entered


    here's what i have so far

    #include <iostream.h>


    void main()
    {
    int value,sum=0,average=0;
    char response;
    int valueCount=0, intCount=0, nonIntCount=0;
    int evenCount=0, oddCount=0;

    cout <<"\nDo you want to enter a value? (y/n): ";
    cin >>response;

    while ((response|32)=='y'){
    cout<<"\nEnter your Value: ";
    cin >>value;
    sum=sum+value;

    valueCount=valueCount+1;

    if (value%2==0)
    evenCount=evenCount+1;

    else oddCount=oddCount+1;

    cout<<"\nDo you want to enter a value? (y/n): ";
    cin>>response;
    }

    average=sum/valueCount;

    cout<<"\nThe number of Values you entered is: "
    <<valueCount;
    cout<<"\nThe sum of all the values you entered is: "
    <<sum;
    cout<<"\nThe average of the values you entered is: "
    <<average;
    cout<<"\nThe number of interger values you entered is: "
    <<intCount;
    cout<<"\nThe number of non-integer values you entered is: "
    <<nonIntCount;
    cout<<"\nThe number of even values you entered is: "
    <<evenCount;
    cout<<"\nThe number of odd values you entered is: "
    <<oddCount<<endl<<endl;
    }

    p.s. i've been able to test the value for odd/even if it' s an integer value but changing it to float '%' doesn't seem to work


    thanks for all the help

  10. #10
    Unregistered
    Guest
    If I understand you correctly you want user to input either an integer or a decimal type number. Integers would logically be stored as type int and decimals as type float or type double. However if you are going to store user input in a single variable called value, then value will need to type double(or float) because you can store an int as a double but not visa versa. (You could store user input as a string, but let's not go there). To have a robust program you will need to make sure user input is valid, but to avoid clouding the issue and to just get the schematics down you can assume valid user input.

    Once you have user input stored in value you then need to determine if input was an integer or not. I think you can do that by casting value to an int, subtracting the casted value from value and if the difference is 0 then value is an integer, otherwise value is a double.

    The modulus operator can only be used on variable of type int.

    Decimals can never be odd or even unless the decimal portion is 0 which makes the decimal also an integer.

    Often averages are of type double or type float rather than type int, unless you are averaging very large numbers.

    Code:
    //declare and initialize variables as needed
    double value, intAvg, doubAvg;
    int intCount = 0, doubCount = 0, evenCount = 0, oddCount = 0;
    int iValue;
    char continue = 'y';
    
    //user controlled loop to be done at least once
    while(continue == 'y')
    {
      cout << "enter any decimal or integer value." << endl;
      cin >> value; //assume valid input
    
      iValue = (int)value;
      if(value - iValue == 0) //if value is an integer value
      {
        cout << "input was an integer" << endl;
        intCount++;
        intSum += iValue;
        intAvg = intSum/intCount;
        if(iValue % 2 == 0)
        {
          evenCount++;
        }
        else
        {
          oddCount++;
        }
      }
      else//vaule is not an integer
      {
        cout << "input was a decimal value." << endl;
        doubCount++;
        doubSum += value;
        doubAvg = doubSum/doubCount;
      }
      cout << "to continue enter y; to stop enter anything else" <<    
      endl;
      cin >> continue;
    }
    
    //display whatever summaries you wish to display.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  5. determining if an integer is a legal integer
    By LiquidBlaze33 in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2005, 07:06 PM

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