-5

I write a small single program. Here is the code:

#include<iostream>
using namespace std;
int main()
{
  int x;
  float y;
  cout<<"Please input an int number:"<<endl;
  cin>>x;
  cout<<"The input number is x= "<<endl;
  cout<<"Please input a float number:"<<endl;
  cin>>y;
  cout<<"The float number is y= "<<endl;
  return 0;
}

But when I first input the number 69.8, the output is

Please input an int number:
69.8
The int number is x= 69
The float number is y= 0.8

I thought it will transfer 69.8 into 69,and then ask me input the next number y, but it input automatically,why?

L.Ying
  • 51
  • 1
  • 7
  • 8
    The subject line has no relationship to the question asked. – Peter Oct 05 '17 at 08:33
  • 3
    Possible duplicate of [Multiple inputs on one line](https://stackoverflow.com/questions/7425318/multiple-inputs-on-one-line) – msc Oct 05 '17 at 08:36

4 Answers4

2

It's because program expect a int and you give it a double. Then all characters that can be converted to a int will be converted, but those who cannot will stay in the buffer. At next call to std::cin, program will read what's left in buffer, that's why y is automatically set.

Read this question to know how to handle wrong type input in C++ and clean that buffer.

informaticienzero
  • 1,796
  • 1
  • 12
  • 22
2

C++ streams (like std::cin), when reading an int, stop at any character that is not part of an integral value. That includes a '.' character.

When reading to an int, there are no intermediate steps of reading a floating point value, or converting a floating point value to an int. The int is read directly, so if there is any non-digit after a digit, reading will stop - and the non-digit will be left in the stream to be read by the next operation.

The first cin>>x; therefore reads 69 from the stream, and leaves the '.' waiting to be read. The cin >> y encounters the '.', treats it as part of a floating point value, and continues. Hence y receives the value 0.8.

Peter
  • 35,646
  • 4
  • 32
  • 74
1

String you entered devided into two.

First, cin>>x; read number 69 and it all. It is correct integer number. Other characters stay in the buffer.

Second, cin>>y; try to read from stream and get .8 and it correct float number too.

Konstantin T.
  • 994
  • 8
  • 22
0

The main reason why, when an end user enters a floating point number where an integer is required, the program not only takes the integer into account, but also assigns the stored floating points of the previously entered number to the next variable, is the buffer - which is essentially a temporary storage of data that is moved to another memory or data storage.

To fix this, you need to use the cin.ignore() function, which is a great function used to ignore and/or clear one or more characters from the input buffer.

If, for example, you add a std::cin. ignore(100,'\n'), it would clear the buffer to allow the program to read and consider the next condition or statement. The number 100 in the syntax is just there to ignore a specific number of characters (100 characters) before the specified boundary, in this case the newline character '\n', but it can, of course, be any number - depending on the number of characters you have.

Also, note that in your outputs

(cout<<"The input number is x= "<<endl;)

and

(cout<<"The float number is y= "<<endl;)

you did not make reference to the x and y variables, hence you may not get the expected results, as the program will not output the values stored in these variables.

So, take a look at my approach, below:

#include<iostream>

using namespace std;

int main()

{
  int x;
  
  float y;
  
  cout<<"Please input an integer:"<<endl;
  
  cin>>x;
  
  cout<<"The entered integer is: "<<x<<endl;
    
  std::cin.ignore(100, '\n');
  
  cout<<"Please input a floating point number: "<<endl;
  
  cin>>y;
  
  cout<<"The entered float number is: "<<y<<endl;
  
  return 0;
  
}