Unwanted conversion

Okay So this is a simple program I made to try to figure out how to print a decimal from a class. However I tried a lot of things and my getfucntion is converting the variable from double to int. how can I prevent that. I have three files.Feel free to tell me all my mistakes, give me advice and shout at me.

//My header file.

class Test
{

private:
double amount;

public:

void setamount(double Amount);
int getamount();

};



//my header cpp file

#include<iostream>
#include"wa.h"
using namespace std;

void Test::setamount(double Amount)
{

amount=Amount;

}


int Test::getamount(){
return amount;
}




//My main function file


#include<iostream>
#include<iomanip>
#include"wa.h"

using namespace std;

int main()
{

Test object;
cout<<setprecision(2)<<fixed;

object.setamount(623.2321);



cout<<"amount is"<<object.getamount()<<endl;

}




The output says amount is : 623
instead of saying amount is 623.2321.
Last edited on
1
2
3
int Test::getamount(){
return amount;
}


You have the function return type marked as in int so it will automatically convert the double to int for you. Change this to double and it will work (change it in header files etc too).
Great that worked thanks a lot, If I could hug you I would !
closed account (3qX21hU5)
Just wanted to give some more information on exactly what was happening and why it was converting it to a integer.

Now when you pass or return a variable from a function by value (Meaning that you don't use a reference or pointer) the function creates a copy of whatever you pass in.

So for example lets say we have this function which adds two numbers together and returns the sum.

1
2
3
4
int addTwo(int one, int two)
{
    return one + two;
}


And we call the function like this

int sum = addTwo(10, 5);

What our function is doing is first it is creating two temporary variable called one and two and assigning the values 10 and 5 to them respectively. So we aren't actually working with the actual values we pass in we are essentially working with copies of them. So remember when we assign a double to a int it implicitly converts that double to a integer and then stores it.

So with that said the same goes for return types. When we return by value from a function the function is creating a temporary variable and then assigns whatever we are returning to that variable (In this case it is the sum of two numbers). Then it it takes that temporary variable and assigns it to the variable int sum in our example.

So that is why it was returning a integer instead of a double. Even though you might have passed a double into the function it would still return as a integer because when the temporary return variable was created it converted the double into a integer so that it could return it.


This is always why pass by reference is so important sometimes, because as you can imagine if we had a very complex and large type we were passing to a function it would take away a good chuck of performance to make all them temp variables and copies. Especially when that function might be used recursively.

Hope that might help shed some light and was understandable ;p
zereo thank you a lot for your explanation. It was very clear and understandable. It is good to see how things work. This is my first computer science class and i am very excited. I also have another question about a switch statement if u care to help me. http://www.cplusplus.com/forum/beginner/104110/. Thank you a lot.
Topic archived. No new replies allowed.