[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]
Re: Problem with cin
On Tue, 23 Jan 2001 11:49:17 +0530 (IST)
shubhendu <97co266@xxxxxxxxxxxxxx> wrote:
> hi friend !
>
> what i am writing is not a solution but discussion on y'r problem
>
> yesterday i worked with your problem and found some things
> like
> ones we give irrelevant value to cin ( wrong data type )
> its value goes to (nil) and
> it does not let execute any cin coming
> after thin cin insted of cin.clear()
> making it back to 0xffffff...
>
> what could be the reason ?
>
> enlighten me ...
This is a standard problem with cin, scanf etc inputs.
You should read a string and then process it if you want your prog should not crash.
You can handle the string conversions by seperating tokens and then converting it to desired number ( int, float, double etc.) using atoi(), atof() etc.
If you want I can send my routines for number input. It was writen for dos but should work fine by changing getch().
>
> On Mon, 22 Jan 2001, Ramkumar Iyer wrote:
>
> > Hi ,
> > Have a look at the code.
> > If I give some invalid value for one integer (like
> > characters) , no values are read into both . Ideally , if
> > reading int1 fails then it should print some message and try
> > reading int2.Am I missing something?
> >
> > #include <iostream.h>
> > main()
> > {
> > int int1=0;
> > int int2=0;
> > cout << " Enter the int1 val : " << flush;
> > cin >> int1;
> > if(!cin)
> > {
> > cout << endl << "Error reading int1" << endl << flush;
> > cin.clear();
> > }
> >
> > cout << " Enter the int2 val : " << flush;
> > cin >> int2;
> >
> > if(!cin)
> > {
> > cout << endl << "Error reading int2" << endl << flush;
> > cin.clear();
> > }
> >
> > cout << endl << " " << int1 << " " << int2 << flush;
> > }
> >
> >
> >
> > ---------------------------------------------
> > LIP is all for free speech. But it was created
> > for a purpose. Violations of the rules of
> > this list will result in stern action.
> >
>
>
> ---------------------------------------------
> LIP is all for free speech. But it was created
> for a purpose. Violations of the rules of
> this list will result in stern action.
>
--
Deepak Joglekar
joglekar@xxxxxxx
----------------
/\^/\
(o.o)
--oOo-(_)-oOo--