I've been severely troubled on putting a cout whereas will tell me "Error" if i didnt put an integer and put a symbol or alphabet instead

here's my code..

#include <iostream.h>
#include <conio.h>

int larger()
{ int x,y,z;
cout<<"enter 1st integer : ";
cin>>x;
cout<<"enter 2nd integer : ";
cin>>y;
cout<<"enter 3rd integer : ";
cin>>z;

if ((z>y)||(z>x))
cout<<z<<" is larger than "<<y<<" and "<<x<<"\n";
else if ((x>z)||(x>y))
cout<<x<<" is larger than "<<z<<" and "<<y<<"\n";
else ((y>x)||(y>z))
cout<<y<<" is larger than "<<x<<" and "<<z<<"\n";
else
cout<<"Error";
}
int main()
{ clrscr();
larger();
getch();
return 0;
}

been tinkering this for half an hour now.. i just gave up :'(

sorry but i dont know that iostream yet
and yes the program is working, i'm new at this and this is what my prof thought me
i just need a cout to tell me error if i put a symbol or alphabet rather than integers

Edited 6 Years Ago by RaoxFaello: n/a

i might just be readig this quickly but maybe a couple of IF statements to define the error
for example

if (x = y)
{
    cout<< "error both X and Y are equal"<endl;
}

cin's >> operator is type specific. If you give it an int, it will try to parse an integer, and fail if the conversion can't be made. So you would need to check for errors during the input request itself. A not uncommon pattern is an input loop that continues while cin fails, clears the stream state, and discards the buffer before prompting again:

while (prompt(), !(cin>> x)) {
  cerr<<"Input error\n";
  cin.clear();
  cin.ignore(1024, '\n'); // Arbitrary "large" value
}

There are more complex solutions that involve strings and manual tests, but those are only necessary if the above pattern doesn't fit your needs.

This article has been dead for over six months. Start a new discussion instead.