The compiler tells you exactly what's wrong and where. But you've ignored all the detail you probably don't understand and only posted what you think is necessary.
Well, you really should post the exact message from the compiler and ask for help interpreting that.
total_thai= x *8.2; // this line int convert to float right ? int total_thai = float x
Consider this:
1 2 3 4
int a = 10;
float b = 20.1;
double c = a + b;
What is the type of c ? Are you proposing that it should change from it's original declaration? If so what type should it be: int or float ?
That is the reason implicit conversions are to the type of the left of the assignment, preserving the original declaration type.
In this code:
1 2
int total_thai = 0;
total_thai= x *8.2;
The expression is of type float because both x and the literal 8.2 are of type float. But then it is implicitly converted to int (loosing precision, which the compiler could warn about if that warning is turned on) because total_thai is of type int
Try to avoid float, unless you need it for some library function - it can loose precision quickly (it only 6 or 7 significant figures) prefer double it can do 15 or 16 sf
float thai( float x )
{
int total_thai = 0;
total_thai = x * 8.2;
return total_thai;
}
The x is a float.
The total_thai is an int.
The x * 8.2 is a float.
You assign a float (the result of x * 8.2) into total_thai.
The total_thai is an int and therefore value in it is int.
You store the integer part of a float value into integer variable.
The function returns float.
The function returns value of total_thai, an integer.
The integer is converted into float.
If you don't want int to mess up your floating point values, then which part(s) of your code you should change?
i already change return x is a float but i cout is Nothing changed
If you don't want int to mess up your floating point values, then which part(s) of your code you should change?
answer : if i dont want int change to float when a big amount come this float cant's use already right?
i want int total_thai convert to float and total_thai is still int