Check for an integer, fail if anything else

This is a discussion on Check for an integer, fail if anything else within the C++ Programming forums, part of the General Programming Boards category; I'm working on an integration program, that works out the integation by approximating with the rectangle rule. I am trying ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    24

    Check for an integer, fail if anything else

    I'm working on an integration program, that works out the integation by approximating with the rectangle rule.

    I am trying to ask how many strips do you want? This MUST be an integer, yet if i enter a float, it will work. Is there a way of checking? i have looked but it seems that this integer check business is not as simple a check as it could be.

    just need some code that will say

    Code:
    ********* code*********
    
    cout << "Please enter the number of strips:  ";
    cin >> n; 
    
    is n an integer?
    
           no - please retry
    
    yes, carry on........
    
    *********code*********

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,720
    What might n be?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    24
    sorry, n is an integer.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    Code:
    #include<iostream>
    using namespace std;
    main()
    {
    float  c;
    int d;
    
    retry:
    cout<<"enter a number"; 
    cin>>c;
    cout<<"\n";
    d=c;
    if((d-c)!=0)goto retry;
    
    
    
    printf("%f",c);
    }

    this goes on and on until the user inputs and integer.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    What the hell? I already answered this question for you yesterday and you even thanked me.

    Code:
    #include <limits>
    
    /* *** CODE HERE *** */
    
    std::cout << "Please enter the value for a, the lower bound  ";
    while(!(std::cin >> a))     // If the stream fails from bad input
    {
       std::cin.clear();             // Clear the fail state
       std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');   // Clear the buffer
       std::cout << "Invalid statement, please re-enter: ";     // Reprompt
    }
    ...and please don't listen to qqqqxxxx. goto is not the best way to do anything.
    Last edited by SlyMaelstrom; 03-17-2006 at 05:05 AM.
    Sent from my iPadŽ

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,720
    Read the user input to a string. Check that each character in the string is valid, say by using std::isdigit() from <cctype> on each of them. If this checking passes, then use a stringstream to convert the string to an integer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    24
    slymaelstrom: yes, but for some reason it wouldn't work when i entered a float.

    no idea why. prob me!!

    but it worked for when i entered a character, just not a float, and it needs to be a float.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    There is no reason that code wouldn't work for a float. Post what you had.
    Sent from my iPadŽ

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,720
    There is no reason that code wouldn't work for a float. Post what you had.
    From what I understand, the OP needs an integer, but your example will still accept floating point input, although it will only read the integer portion.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    QUOTED BY SLYMAELSTORM
    >...and please don't listen to qqqqxxxx. goto is not the best way to do anything.

    hmmmm

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    24
    ok, this is what i have. apologies for incorect formatting, i'm new to this. i'm having to go out in a few minutes so i won't reply for a while either.

    Code:
    #include <iostream.h>
    #include <iomanip.h>
    #include <cmath.h>
    #include <fstream.h>
    #include <limits.h>
    #include <stdio.h>
    #include <conio.h>
    
    float funcj(float);
    
    int main()
    {
        int n, i, m;
        float a, b, h, x, s, integral;
                                
        
    
        clrscr();
        cout << "************************************************************************\n\n";
        textcolor (12);
        cprintf ("The function you are integrating is x^2 - 5x - 3");
    
        cout << "\n\nWould you like to input your values?\n";
        cout << "OR would you like to input the values from the file rectangle.dat?";
    
        cout << "\n\nOption 1: Input the values\n";
        cout << "\nOption 2: Read the values\n";
    
        cout << "\n\nPlease enter your choice, 1 or 2:  ";
        cin >> m;
    
            if (m == 1)
    
            {
    
        cout << "\n\nPlease input a, the lower bound:  ";
    
        
        while(!(cin >> a))
    
            {
    
            textcolor (2);
            cprintf ("You have inputted the data incorrectly.");
            cout << "\nPlease try again.  ";
            return 0;
            }
          
    
        cout << "\nPlease input b, the upper bound:  ";
    
        //cin >> b;
    
        while(!(cin >> b ))
    
            {
    
            textcolor (2);
            cprintf ("You have inputted the data incorrectly.");
            cout << "\nPlease try again. ";
            return 0;
            
    	}
        
        cout << "Please enter the number of strips:  ";
    	
        while(!(cin >> n))     // If the stream fails from bad input
    	
    	{
       	
    	cin.clear();             // Clear the fail state
       	cin.ignore(std::numeric_limits<streamsize>::max(), '\n');   // Clear the buffer
       	cout << "Invalid statement, please re-enter: ";     // Reprompt
    	
    	}
    
    
             }
    
          else if (m == 2)
    
             {
    
             ifstream read ("rectangle.dat");
    
             read >> a >> b >> n;
             
             }
    
    	else 
    
    	{ 
                    textcolor (2);
                    cprintf ("You have inputted data incorrectly.");
                    cout << "\nPlease try again.";
                    return 0;
    	}
    
        if (a >= b)
    
        {
           textcolor (2);
           cprintf ("\nYou have entered the data incorrectly.");
           cout << "\na must be lower than b.\n";
           cout << "Please Try Again.\n";
    
        }
    
       // else if ( n < 1)
    
       //    {
       //    textcolor (2);
       //    cprintf ("\nYou have entered the data incorrectly.");
       //    cout << "\nYou must have a positive (integer) number of strips.\n";
       //    cout << "Please try again.";
    
    
       //    }
    
         else
    
        {
            integral = 0;
    
                                                                          
                h = fabs(a - b) / n;
            
                for  (i = 0; i < n; i++ )
    
                {
                                                            
                    x = a + i * h;
            
                    s = h * funcj(x);
                                                            
                    integral = integral + s;
            
                           
    
                }
    
              textcolor (4);
    
              cprintf ("\nThe answer is: ");
              cout << integral << "\n";
         }
        
        return 0;
                                             
    }          
    
    float funcj(float x)
    {
        float f;
    
        f = (x*x) - (5*x) - 3 ;
    
        return f;
    }


    i'm aware i haven't used the std that you have, so i will alter it if you need me too when i get back.

    its not the finished article by any means. if you do run the program, if u use option 2, you'll need a file called rectangle.dat

    which just contains 3 numbers.

    Code:
    2 5 100
    // where a =2, b = 5, n = 100]

    this just shows how i can read data from a file. its not needed but i'm just showing my teacher i can read data from a file as well as from inputting the data in the prog.

    its by no means finished, but if i can just check for n being an integer, then i can then tweak it, so its more user friendly.

    thanks for the help. its much appreciated.

    sorry for the rushed messgae.

  12. #12
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by derek tims
    Code:
    #include <iostream.h>
    #include <iomanip.h>
    #include <cmath.h>
    #include <fstream.h>
    #include <limits.h>
    #include <stdio.h>
    #include <conio.h>
    i'm aware i haven't used the std that you have, so i will alter it if you need me too when i get back.
    You should understand a few things here, First of all, "std" is the namespace which contains the C++ standard library.
    (Note - Only in newer compilers. compilers older than about 1999 don't use namespaces)

    However, you aren't actually using any of the C++ standard library, most of the headers you've got there are outdated - iostream, iomanip, cmath, fstream, limits and cstdio must be used without an .h
    (Note conio.h is non-standard anyway, so that can be left alone, and in C++, all C headers are prefixed with "c", eg cstdio)

    There are dozens of FAQ links and tutorials which explain this in more detail, so I won't go into that, but I suggest you do go and read up on the std namespace.

    When using the C++ standard library, you can optionally add a line after your includes of
    Code:
    using namespace std;
    which removes the need for you to put the std:: prefix in front of names such as std::cout.
    Last edited by Bench82; 03-17-2006 at 06:20 AM.

  13. #13
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by qqqqxxxx
    Code:
    #include<iostream>
    using namespace std;
    main()
    {
    float  c;
    int d;
    
    retry:
    cout<<"enter a number"; 
    cin>>c;
    cout<<"\n";
    d=c;
    if((d-c)!=0)goto retry;
    
    
    
    printf("%f",c);
    }

    this goes on and on until the user inputs and integer.
    Please stop trying to screw with people.

  14. #14
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    double post

  15. #15
    Registered User
    Join Date
    Mar 2006
    Posts
    3
    HOMEYG
    cut the crap,come up with some code first.
    and the goto can be easily replaced by a while loop,but i suppose you r just lacking in talent.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check for integer in kernel module
    By NuNn in forum C Programming
    Replies: 1
    Last Post: 04-13-2009, 11:27 AM
  2. Check if a string is a positive integer, please help.
    By Phantom2303 in forum C Programming
    Replies: 1
    Last Post: 03-16-2009, 09:55 PM
  3. MFC check box question
    By MyglyMP2 in forum Windows Programming
    Replies: 2
    Last Post: 03-09-2009, 05:47 PM
  4. Access Violation
    By Graham Aker in forum C Programming
    Replies: 100
    Last Post: 01-26-2009, 07:31 PM
  5. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 07:38 AM

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