-1
std::cout << "Goal Number "<<std::setprecision(1);
int Z;
cin >> Z;
std::cout << "Goal Number is "<< Z <<"\n";
start:
std::cout << "Input root "<<std::setprecision(8);
float X;
cin >> X;
float multiply;
multiply=X*X;

For example if I type 4.4, the value 4 is getting printed. But the remaining 0.4 is going on the next variable, which is float. Is there any way to stop this? Thanks in advance.

rlandster
  • 7,294
  • 14
  • 58
  • 96
  • what's `std::cout << "Enter Value 2 "< – Stack Danny Nov 20 '16 at 19:55
  • Maybe i should give more details on my code. the Z is the "Goal Number" and the next variable is the root which you have to input. That cout just indicates the user to enter the value X – Ford Junior Nov 20 '16 at 20:01

2 Answers2

0

when you are using

cin>>z;

then it is expected for you to input an integer and not a float. The compiler take the integer part and discards the other part for the second variable. for example if you input 4ab then output will be 4 and 0 as ab is not a float either but when you input 4.4 then first 4 goes into z being an integer and rest .4 being a float goes to X. so to avoid it just input an integer in z instead of a float and then it will ask you to input 2nd variable

Karan Nagpal
  • 341
  • 2
  • 10
0

By default cin will only read what it expects. If you specify an integer as the variable it is reading to, then it will only read numeric values.

In order to collect an entire number you will need to either read in a float and cast to whatever you might now intend to use it for, or read in by line and manually parse the information there with a stringstream. (Keep in mind that the string stream will still only read 4 from "4.4" if you tell it to output to an int so this is fairly redundant.)

Tyler Scott
  • 1,046
  • 17
  • 33