How do you know if it returns 7 or 7.00?
Do you print a double or an int?

When you convert the MyPrice return to an int, it'll become 7.

Post some more code?

It is as Hiddepolen is saying, right now you have the function:

double MyPrice(){

return cost;
}

so it returns a double value.
If it is however that the variable that it's being returned to is an int,
when you print that variable you will only print the 7;

eg. int x = MyPrice ();
printf ("%d", x);

that will only print the 7, whereas if you had:

eg. double x = MyPrice ();
printf ("%f", x); // OR printf ("%lf", x);

that would print: 7.000000

NOTE: to reduce the zeros ( printf ("%.2f", x); )

Hi ya, no I don't convert it into an int. If I replace that 7.00 with say 7.01 then when the function returns it will return 7.01. So for some reasons when it is 7.00 it chops off the .00.

The variable cost has been declared as double. I have another function that sets the cost, say something like

void setCost( double Cost ) 

			{
				
				cost = Cost; //cost is a double here

			}

I pass a value of 7.00 to this function and then somewhere else in main I call the double MyPrice function so it should return 7.00 and not 7. As I said above if I change that 7.00 to 7.01 or any other value as long as the last digit is not 0 will return fine. So say 7.02 etc.

Strange. Could you post your entire code? Or at least all the relevant parts, everything to do with those two functions and the printing of the double.

Cheers

Void functions don't return anything, so setting:

cost = Cost;
won't change anything outside of that function unless
cost is a globally defined variable.

Again agreeing with Hidde sending us some more of the code that you're using
will enable us to help you better.

hi there, sure here is an example:

#include<iostream>
using namespace std;
double cost;
double getCost( )

			{
				
				return cost ;

			}

		void setCost( double Cost ) 

			{
				
				cost = Cost ;

			}

int main()

{
	double myCost = 0.0 ;

	cout << "What's the cost? ";

	cin >> myCost ;

	setCost(myCost) ;

	cout <<"the cost is"<< getCost() ;



}

if I input 7.99 it returns 7.99, if I input 7.00 it returns 7 and not 7.00 as I would like. Why is that and how do I get around that...really don't have a clue sorry and can't think of anything...
thanks

jonsca,
thanks for that. I had a read at the tutorial, not sure if I understand it correctly though especially when it comes down to distinguish between fixed and default...
this is what I came up with, and it works:

#include<iostream>
#include <iomanip>
using namespace std;
double cost;
double getCost( )
 
			{
 
				return cost ;
 
			}
 
		void setCost( double Cost ) 
 
			{
 
				cost = Cost ;
 
			}
 
int main()
 
{
	double myCost = 0.0 ;
 
	cout << "What's the cost? ";
 
	cin >> myCost ;
 
	setCost(myCost) ;
 
	cout <<"the cost is";
	cout<< fixed;
	cout<< setprecision (2)<< getCost() ;
 
 
 
}

Just so I understand this properly:

cout<< fixed;//not sure what it does
cout<< setprecision (2)<< getCost() ;

This instead effectively says that I want the floating number to display 2 digits after the period..is it correct?

I think what you're doing is correct, but:

Why dont you use 'printf ()'. This function is much handier, and lets you input everything you want (and print every way you want it).
Like this:

int main () {

int foo = 5;
double bar = 7.22, bar2 = 3.93;
char ret = 'x';

printf ("This is my int 'foo': %d, these are my doubles 'bar' and 'bar2': %.0f and %.2f, and this is my 'ret' charachter : %c", foo, bar, bar2, ret);


}

Try it!

Cheers

And, in the above, look at the doubles. First I print them with 0 precision, and the next one with 2 presicion.

(Output:

This is my int 'foo': 5, these are my doubles 'bar' and 'bar2': 7 and 3.93, and this is my 'ret' charachter : x

)

Cheers

jonsca,
thanks for that. I had a read at the tutorial, not sure if I understand it correctly though especially when it comes down to distinguish between fixed and default...

From that same site fixed means that: "the value is represented with exactly as many digits in the fraction part as specified by the precision field and with no exponent part." (presumably meaning that the implementation will find the closest floating point number that carries that value to a certain number of digits, and will not use scientific notation)
vs.
Floating point is a way to increase precision at the expense of the size of the number and vice versa. That is the default for cout.

I think what you're doing is correct, but:

Why dont you use 'printf ()'. This function is much handier, and lets you input everything you want (and print every way you want it).

This is probably one that could be argued either way. If the OP's class is using iostream, it's probably a good idea to stick with that.

Not to take this OT, but there are some interesting arguments about that here: http://stackoverflow.com/questions/2872543/printf-vs-cout-in-c

Hi thanks, well I don't use 'printf ()' because to be honest I have never come across it before...do you have any link where I can read a bit more about it?

thanks guys. Jonsca sorry to labour the point but, to go back to the floating point issue, I had a look again at http://www.cplusplus.com/reference/iostream/manipulators/setprecision/ and the example there is:

// setprecision example
#include <iostream>
#include <iomanip>
using namespace std;

int main () {
  double f =3.14159;
  cout << setprecision (5) << f << endl;
  cout << setprecision (9) << f << endl;
  cout << fixed;
  cout << setprecision (5) << f << endl;
  cout << setprecision (9) << f << endl;
  return 0;
}

which prints
3.1416
3.14159
3.14159
3.141590000

Now, the first cout is set to 5, so shouldn't the first result have 5 digits and be 3.14159? why is it rounding it? Same story for the second cout which is set to 9, so shouldn't the second result have 9 digits rather than 5? the "fixed" I think is clear enough.

I am finding myself in this situation at the moment. I have a program which takes 5 floating numbers as input from a file say 3.78, 6.658, 4.4569, 5.76549, 3.665439. I set

cout<<"numbers to be processed are: "
<<fixed
<<setprecision ( 6)
<<numbers;// to indicate the numbers to be output

and with a for loop it prints all the above numbers. Obviously it displays:
3.780000
6.658000
4.456900
5.765490
3.665439

Any way to get the rid of the zeros but at the same time making sure that the last number prints all the digits after the period? I tried without using the setprecision function but what happens is that the last number is printed as 3.66544 and not 3.665439...
thanks

As far as the second 3.14159 (with setprecision at 9), the mode is still the default one, so the extra zeros are not displayed (from the cplusplus.com site
"On the default floating-point notation, the precision field specifies the maximum number of meaningful digits to display in total counting both those before and those after the decimal point. Notice that it is not a minimum and therefore it does not pad the displayed number with trailing zeros if the number can be displayed with less digits than the precision." (http://www.cplusplus.com/reference/iostream/manipulators/setprecision/)

In terms of your request, what is your objective? (it seems to be changing from your original request) You could selectively format the number based on the digits after the decimal point, but that would get tedious. You are asking to have the default setting on some and the fixed setting on others. Perhaps if we knew what you were trying to accomplish...

Edited 6 Years Ago by jonsca: n/a

sorry didn't mean to be confusing. My original request and this one are in fact the same pretty much, it's just the examples I used that are different. I fully understand now my own example (the one with the cost function) and the next example was just to confirm that I understand how to deal with this precision business.

I have few numbers the program takes from a file, and they are:
3.78
6.658
4.4569
5.76549
3.665439

The problem I was having is when I cout them. With no precision set, the screen outputs this:

3.78
6.658
4.4569
5.76549
3.66544

rounding the 6th digit after the period in the last number.

With the fixed precision set to 6

cout<<fixed<<setprecision (6)...;

it displays

3.780000
6.658000
4.456900
5.765490
3.665439

which is what i expected but the trailing zeroe are a bit annoying so I was trying to find a way to get the rid of them. So reading the tutorial and your post I now know that the one to use is the default setprecision, so

cout<<setprecision (7)...;

because the default one takes into consideration numbers before and after the period and since 3.665439 is 7 digit I need 7. With this my output is sorted:
3.78
6.658
4.4569
5.76549
3.665439

What's annoying though, is that you need to know how many digits your numbers are before setting the precision up. So say that I take the input from a user and not from a file and the user inputs a number like 3.5645342314569 there will be no way that the number input can be displayed in full unless I set the (default) precision to be 14 in my program that attempts to display that..

This question has already been answered. Start a new discussion instead.