Print floating point--Solved

Solved-thanks!

I am very new.. About one month in, and I'm having trouble with this one for some reason. And yes, I have to use floating-point numbers.

Here's what I had, but obviously it's wrong because I don't believe you can use cout inside the first function.

"using namespace std;

double currency(float number) {

cout << "Please enter a number: ";
cin >> number;
return number;
}

int main ()
{

cout << "That number in currency is: $" << currency(number) << fixed << setprecision(2) << endl;

return 0;
}

Thanks for any help. I know my code is messed up, but just throwing something out there.
Last edited on
Would this be easier with just a main function?
You have to call the function "currency" in main, and set the return value to a variable.

The code will look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <iomanip>
using namespace std;

float currency() { //You do not need to have any arguments in this function
float number;
cout << "Please enter a number: ";
cin >> number;
return number;
}

int main ()
{
float amount;
amount=currency(); //Runs currency, and assigns returned value to variable "amount"
cout << "That number in currency is: $" << amount << fixed << setprecision(2) << endl;

return 0;
}
Last edited on
You also need to put the << fixed << setprecision(2) in front of << amount.
Topic archived. No new replies allowed.