C++ data type question?

so i was just wondering.. Why when you do math calculations people sometimes do
int, float or double? wouldnt it be best just to use float or double every time just for the possibility the user my enter in a decimal number?

Thanks..
Ex:

int X;
double F;

cout << "Enter the first number!" << endl;
cin>> X;

cout << "Enter the second number!" << endl;
cin << F;

cout << "The total = " << X + F;



Last edited on
wouldnt it be best just to use float or double every time just for the possibility the user my enter in a decimal number?
no, the best would be to accept a string because that is what the user enters. Later you may convert it to the required data type e.g. with stringstream

http://cplusplus.com/reference/sstream/stringstream/

Then you're able to safely check whether the user input was right
Thanks you.. This was just something I was pondering while writing a payroll program.
Topic archived. No new replies allowed.