cin.fail() not working?

Hey,

i have a small problem, what i want to do is, have the user input an integer (Ie: 1,45,65) or a real number (Ie. 3.4, 6.5), if the input is an integer program in suppose to output, "you entered an integer", if its a real number, "you entered a real number".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;



int main()
{

    int i;



    cin >>i;

    if(cin.fail()==1)
    {
        cout <<"you entered a real number"<<endl;


    }
    else
    {
         cout <<"you entered a integer number"<<endl;

    }
cout<<cin.fail();


}


the cin>>i should fail if i enter 12.12, it should fail when it tries to read the ".", but its not happening. your help is appreciated.
The cin.fail() should only be true if the user tried to enter something other than an integer.

Given the input "12abc" then you have a valid integer input: "12", leaving "abc" in the stream. (Whitespace is not significant.)

The way to check, then, is to peek() at the next character in the stream. If it is a period, then you have a real number. If it is not, then you have an integer.

So: (1) read an int (2) peek() to see what is next (3a) if it was a period read a float (or double) and add your int to it.


A more robust method is to read the entire line of user input, and validate its form yourself. Then you can read things like "42e7" and complain about things like "-3abc".

Here is a link about it. As noted in the link, you can ignore the fancy template stuff. The important thing is checking eof().
http://www.cplusplus.com/forum/beginner/13044/page1.html#msg62827

Remember, once you have the entire line of user's input, you can try to convert it to as many different things as you like. A simple method would be to simply >> int and >> float and check to see if their values match. If they do, look to see if the input contains a period or an 'E' or 'e' to decide between integer or real.

Hope this helps.
well in the exercise i am not suppose to use peek(),
so cin>>i ,where i is an integer, it won't fail when it encounters a "."?
ok then i tried re-writing::

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

#include <iostream>
using namespace std;



int main()
{

    int i;
    double j;



    cin >>i;
    if(cin.eof())
    {

        cout <<"you entred a integer number\n";


    }
    else
    {
           cout <<"you entred a real number"<<endl;

    }


return 0;

}



i still don't get it, if i enter an integer, it will fully be read in and nothing will be left in the stream, thus end of file will be encountered.

if i inter a real number, after the integer takes in there will be something left in the stream, thus it won't be the end of file.

You have mixed the methods I gave you. The cin will not signal EOF if all you have done is pressed ENTER.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
  {
  double d;
  string s;

  getline( cin, s );
  istringstream iss( s );
  iss >> d;
  if (!iss.eof())  // this works because the end of the iss stream is the same as the end of the string s
    {
    cout << "Hey, that was not a number!\n";
    }
  else if (s.find_first_of( ".Ee" ) != string::npos)  // ok, so we got a valid number. Did it have anything that makes it a real number: -1.2e3 ?
    {
    cout << "That was a real number.\n";
    }
  else
    {
    cout << "That there was an integer.\n";
    }

  return 0;
  }

Good job for trying.
Last edited on
thanks, but is there anyway to check if something is still in the buffer?

ex, if i put 6.45,
.45 will be left in the buffer.
Topic archived. No new replies allowed.