Passing values

How to print the total purchase cost from a cout comand in the main function? My output is: The total purchase price is: $121.663 but i want that to be the output printing in main. How do I pass total cost back to main and then print total in main?


#include <iostream>
using namespace std;

// Function prototype
void calculateTotalPurchasePrice(float,float,float,float,float);

int main()
{
float item1 = 45.00, item2 = 95.48, item3 = 5.75, sales_tax = 0.832, total;

calculateTotalPurchasePrice(item1, item2, item3, sales_tax, total);




return 0;
}

void calculateTotalPurchasePrice(float item1, float item2, float item3, float sales_tax, float total)
{

total = (item1 + item2 + item3)*sales_tax;
cout << "The total purchase price: $" << total << endl;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
double calculateTotalPurchasePrice(double item1, double item2, double item3, double sales_tax)
{ return (item1 + item2 + item3) * (1.0 + sales_tax); }

int main()
{
  double apple = 10.00, pear = 5.00, orange = 7.00; 
  double sales_tax = 0.11; // 11 percent sales tax

  std::cout << calculateTotalPurchaseProce(apple, pear, orange, sales_tax) << '\n';
}
Last edited on
Hello dannyboy89,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

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

Hint: You can edit your post, highlight your code and press the <> formatting button.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


"float"s may work here, but you are better off using "double"s for your floating point numbers as "double"s have greater precision and likely to give you a better answer in a calculation.

mbozzi has made a good point. Working off your code and with what he said I came up with this:
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
#include <iostream>
#include <iomanip>  // <--- Added. For std::fixed, std::showpoint, std::setprecision.

using namespace std;

// Function prototype
double calculateTotalPurchasePrice(double item1, double item2, double item3, const double SALES_TAX);

int main()
{
	constexpr double SALES_TAX{ 0.832 };  // <--- This is a value that should not be changed by the program.

	double item1 = 45.00, item2 = 95.48, item3 = 5.75,  total{};

	total = calculateTotalPurchasePrice(item1, item2, item3, SALES_TAX);
	
	std::cout << std::fixed << std::showpoint << std::setprecision(2);  // <--- Only needs done once.

	cout << "The total purchase price: $" << total << endl;
	
	return 0;
}

double calculateTotalPurchasePrice(double item1, double item2, double item3, const double SALES_TAX)
{
	return (item1 + item2 + item3) * SALES_TAX;
}

For line 7 the only requirement for the prototype is to list the type of variables being sent to the function. I find that adding the name of the variables can be very helpful. Most times I just copy the function definition and paste it at the top of the program and add the ";" at the end. This cuts down on misspelled variable names.

If you are not familiar with the "<iomanip" include file now is a good time to learn. This header file will allow you to manipulate the output and print only two decimal places, (setprecision). The "std::fixed" says to use numbers not scientific notation, the default, and "std::showpoint" says to print (.00) if it comes up.

I made "SALES_TAX" a constant because this if a variable that should not be changed by the program. If you try the compiler should complain.

When I first compiled the program it complained the "total" was an uninitialized variable. It may not always be necessary to initialize all the variables, but it is a good idea. If just for the peace of mind that the variable does not contain garbage.

Your IDE/compiler should be using at least the C++11 standards which gives you the uniform initializer of the {}s. Empty this will set the variable to (0) zero, "char"s to "\0", and doubles to (0.0). Or you could put a number in between to give it an initial value.

After that the comments in the program should explain it.

Another alternative is to use the "cout" as mbozzi did in his example.

Andy
Topic archived. No new replies allowed.