ashish sarode wrote:
> Hello,
> 
> i have following code in C++
> 
> #include <iostream>
> using namespace std;
> int main()
> {
>  int i;
>  while(1)
>  {
>   cout<<"Enter a number";
>   cin>>i;
>   if(i==1)
>    break;
>  }
>  return 0;
> }
> 
> After running this code, if i enter a floting precision value - E.g. 2.3 for 
> i then the code goes into infinite loop.
> 
> Interestingly if i enter 1.1 or 1.36 for i then the infinite loop doesnt 
> encountered - since the break statement is reached.
> 
> I have tried it on Visual Studio 6.0. Can anybody tell me the reason?
> 
> 
> I did also tried the same code using printf scanf but the same problem occurs 
> for this code also - 
> 
> Note- this is .c file
> 
> #include <stdio.h>
> int main()
> {
>  int i;
>  while(1)
>  {
>   printf("Enter a number");
>   scanf ("%d", &i);
>   if(i==1)
>    break;
>  }
>  return 0;
> }

Both are reading the integer portion just fine (most likely) and, if it 
is not a '1', it loops back.  On the second loop, the decimal portion is 
still in the buffer and both are either failing (most likely) or 
returning 0.

This is why I only input strings - usually one line at a time.  Once the 
raw data the user input is in memory, you can do whatever you want with 
it - including ignore the decimal portion in an integer conversion...and 
the input stream will start on the next line.

Data processing and data input should always be separate functions, IMO. 
  It rarely makes sense to combine them.

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* MyTaskFocus 1.1
Get on task.  Stay on task.

http://www.CubicleSoft.com/MyTaskFocus/

Reply via email to