function int convert to float

#include<iostream>


using namespace std;
float thai(float x); //function declaration
float us(float x); //function declaration
float renmin(float x); //function declaration


int select;
float change_rm,total=0;

int main(){


cout<<"******* Ah Beng Money Changer ***********\n";
cout<<" 1. Thai Baht \n";
cout<<" 2. US Dollar \n";
cout<<" 3. Ren Min Bi \n\n";



cout<<"Insert your RM:";
cin>>change_rm;


cout<<"Select 1,2 or 3:";
cin>>select;

switch(select) {
case 1:
cout<<"\n Change to Thai Baht \n";
total=thai(change_rm) ;
cout<<"Total Thai Baht: "<<total;
break;

case 2:
cout<<"\nChange to US Dollar \n";
total=us(change_rm) ;
cout<<"Total US Dollar: "<<total;
break;

case 3:
cout<<"\nChange to Ren Min Bi \n";
total=renmin(change_rm) ;
cout<<"Total of multiply: "<<total;
break;


default:
cout<<"No such number. Please choose 1 to 3 only!";
break;

}
}


float thai(float x){
int total_thai=0; //here question when cout no float
total_thai= x *8.2;

return total_thai;


}

float us(float x){
int total_us=0; //here question when cout no float
total_us= x /4.1;

return total_us;

}

float renmin(float x){
int total_renmin=0; //here question when cout no float
total_renmin= x *1.68;

return total_renmin;

}
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.
Program no wrongWhen the program count answer the Thai baht is 8 no float I want thai baht 8.2
I just want how to Change integer convert to float
Last edited on
It is your code that converts float into int, and then converts that int back to float.

Why do you do that if you don't want to do that?
int total_thai=0; //here question when cout no float
total_thai= x *8.2;

total_thai is an int. if the result of x*8.2 is 64.9235, you get 64 as a result.

make total_thai a float if you want it to be a float.


make total_thai a float if you want it to be a float

if change all use float when a big amout float cant's use right?
To : keskiverto

float thai(float x){
int total_thai=0;
total_thai= x *8.2; // this line int convert to float right ? int total_thai = float x
}

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

To : TheIdeasMan

thanks u for suggestion

i want run thai bath is 8.2 not 8

can u help me to change this function?

This is what you have:
1
2
3
4
5
6
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?
To : keskiverto

thanks you so much !

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

but cout answer i want float number
Last edited on
We have a communication issue.

Why is total_thai an int?

It is you, who wrote int total_thai = 0;. Why did you do that?
Last edited on
this is my teacher do it
He wants to test me i just student only
I have no idea and then i come ask the question

how to int convert to float

u means the int total_thai only can change to float right?


are u have facebook ?
Last edited on
Let's save some pain for everyone and just write out what jonnin and the others said.
1
2
3
4
5
float thai(float x) {
    float total_thai = 0.0f;
    total_thai = x * 8.2f;
    return total_thai;
}

Edit: I just re-read your posts, it sounds like you don't like this. My bad.

Note that in real life, floats should NEVER be used to represent money.
In real life, there are specific rounding rules for dealing with currency exchange,
e.g. http://www.evro.si/en/info-for-consumers/rounding-rules/index.html

The real answer to your question is to make a new design. You should have a class that represents money exactly:
1
2
3
4
struct Money {
    int whole_part;
    int fractional_part;
};


Money thai(Money x)
{
// not testing
i
}
Last edited on
limchankhim. Do these changes to your code.
1
2
3
4
5
6
7
8
//cout << "Total Thai Baht: " << total;
cout << "Total Thai Baht: " << float(total);

//cout << "Total US Dollar: " << total;
cout << "Total US Dollar: " << float(total);

//cout << "Total of multiply: " << total;
cout << "Total of multiply: " << float(total);


Based on the discussion. I believe this is what you are looking for.
thanks you so much tibrodo!
Topic archived. No new replies allowed.