cin >> entero

Jan 17, 2013 at 4:47pm
Hi guys, I got stuck and I dont know what's wrong with this code. The isEven is a function created by me, but the thing is that the debbuger says the != opperator doesnt match.

cout << "Enter some integer to see if is even." << endl
<< "Enter the EOF character to end input." << endl;
int entero;
while((cin >> entero) != EOF)
{
cout << "\nThe number " << entero << " is " << isEven (entero);
}
Jan 17, 2013 at 4:52pm
Where is EOF decleard?
And if you want to compare a number to a string EOF you need to put it inside "EOF" like that and type cast from a int to a char\ string.
Jan 17, 2013 at 5:13pm
You could try
while((cin >> entero) && !cin.eof())
or simply
while (cin >> entero)
though these will end when a non-numeric character is entered.
Jan 17, 2013 at 9:06pm
Chrevil, the second option works for what I want, however, I was looking at a code of Dietel C++ and he has the next code in the definition of a memeber function of a userdefined class.

void GradeBook::inputGrades()
{
int grade;

cout << "Enter the letter grades." << endl
<< "\nEnter the EOF character to end input";

while ( ( grade = cin.get() ) != EOF )
{
DO SOMETHING WHICH IS NOT IMPORTANT IN THIS CASE..
}
}

Why that is supposed to work and not mine??
Last edited on Jan 17, 2013 at 9:07pm
Jan 17, 2013 at 9:27pm
> but the thing is that the debbuger compiler says the != opperator doesnt match.
So you don't understand the message, fine. ¿why are you rephrasing it?

cin >> entero is different than
grade = cin.get()
Jan 17, 2013 at 9:31pm
The return type is different in each case.

cin.get() has the return type of the member function get() In the example shown the returned value is of type int
Reference: http://www.cplusplus.com/reference/istream/istream/get/

cin >> entero returns the stream object itself. When used in a conditional test, it gives a boolean (true/false) result according to the state of the stream.
References:
http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/
http://www.cplusplus.com/reference/ios/ios/operatornot/
Jan 21, 2013 at 3:48pm
thank u sir
Topic archived. No new replies allowed.