Functions

I'm trying to pass the total from another function to the main function but I'm having trouble. I keep getting 1.5 instead of 7. What am I doing wrong?

#include <iostream>

using namespace std;

double order();

int main(double total)
{

double salesTotal;
order();

salesTotal=total+1.50;
cout << salesTotal;

return 0;
}

double order()
{
double total;
total=5.50;

return total;
}
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/

HINT: you can edit your post and add code tags.

With that said, your main() parameter list is totally wrong. Is it supposed to be
double order(double total)?

https://stackoverflow.com/questions/4207134/what-is-the-proper-declaration-of-main

You are returning a value from your custom function and discarding it. You are doing nothing with the returned value in main().
from time to time, just redoing it feels like the best way to xplain it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

#include <iostream>

using namespace std;

double order();

int main() // fixed
{
   double total; 
   double salesTotal;
   total= order();  //functions that have a type other than void 
//are usually found on the right side of an assignment to capture the value returned. 

salesTotal=total+1.50;
cout << salesTotal;

return 0;
}

double order()
{
double total;
total=5.50;

return total;

return 5.5; //you don't need all that for a constant. 
}






Topic archived. No new replies allowed.