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; ok, well what i have done is when i enter n, i store that value as a string. now then, ...

  1. #16
    Registered User
    Join Date
    Mar 2006
    Posts
    24
    ok, well what i have done is when i enter n, i store that value as a string.

    now then, i read from that file, an integer q, and read the same value, a float p.

    then i set up a while loop saying that when q-p is not equal to zero, then it brings about an error.

    thanks for all the advice.

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    In your posted code, you use while(!(cin >> a)), but you didn't put the cin.clear() and cin.ignore() inside that while loop. It won't work without them.

    If you want to make sure n is an int, you can change your loop to the following. It checks to see if there is anything typed after the integer is read in, so if the user types 3.6, the 3 will be read in and the . will cause it to give the error:
    Code:
        while(!(cin >> n) || cin.get() != '\n')     // If the stream fails from bad input
    	{
     	cin.clear();             // Clear the fail state
       	cin.ignore(1000, '\n');   // Clear the buffer
       	cout << "Invalid statement, please re-enter: ";     // Reprompt
    	}

  3. #18
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    You want to make a cast...

    Code:
    float x = p - (float)q;
    or put your intenger on a float

    Code:
    float aux = q;
    result = p - aux;
    that is...

    so bb..

    ------------------------------------------

    Sorry for my poor english...

  4. #19
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Or for another simple way of doing it, you could read in a string, and check for the presence of a '.' indicating a floating point number or any other non-numeric character, and reject it as you see it.

    Really though, I've not seen a valid use of goto's that come to mind, I am sure in some rare case they could be useful, but in every instance I've been tempted to use them, I find a loop much easier to read in any event.

  5. #20
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    aside to qqqqxxxx: What you really wanted to do is use a do-while loop.

    Code:
    do {
    cout<<"enter a number"; 
    cin>>c;
    cout<<"\n";
    d=c;
    } while ((d-c)!=0);
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

Page 2 of 2 FirstFirst 12
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, 12:27 PM
  2. Check if a string is a positive integer, please help.
    By Phantom2303 in forum C Programming
    Replies: 1
    Last Post: 03-16-2009, 10:55 PM
  3. MFC check box question
    By MyglyMP2 in forum Windows Programming
    Replies: 2
    Last Post: 03-09-2009, 06:47 PM
  4. Access Violation
    By Graham Aker in forum C Programming
    Replies: 100
    Last Post: 01-26-2009, 08:31 PM
  5. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM

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