Calculating percentages question

I am stuck on how to calculate percentages for the total number contributed by each different size of pizza. My book doesn't have any examples of using division to solve. I tried to use sum but I keep getting errors. Here is my attempt at it using microsoft visual studio 2012. I also get an error LNK1168: cannot open everytime I try to debug again. Any help would be greatly appreciated!

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
37
38
39
 #include <iostream>
using namespace std;

int main()
{
    double small =				0.0;
	double medium =				0.0;
	double large =				0.0;
	double family =				0.0;
	double totalPizzasSold =	0.0;
	double smallContribution =	0.0;
	double mediumContribution = 0.0;
	double largeContribution =	0.0;
	double familyContribution = 0.0;

	//enter input
	cout << " Enter the small: ";
	cin >> small;
	cout << " Enter the medium: ";
	cin >> medium;
	cout << " Enter the large: ";
	cin >> large;
	cout << " Enter the family: ";
	cin >> family;


	// calculate total pizzas sold
	// calculate percentages of each
	smallContribution = small / totalPizzasSold;
	mediumContribution = medium / totalPizzasSold;
	largeContribution = large / totalPizzasSold;
	familyContribution = family / totalPizzasSold;
	totalPizzasSold = small + medium + large + family;
	cout << " Total pizzas sold: $" << totalPizzasSold <<  endl;

	system("pause");

	return 0;
}   //end of main function 
smallContribution = small / totalPizzasSold;
the denominator is still 0.0
but division by 0 just gives #inf whatever it is value when you print it and does not prevent running code unless you set some sort of flag to make it error out.

I agree that you want to compute the total first, that is a bug, but it should actually run, right?

thanks for responding, the program runs and collects the total but that is it. I don't get an #inf error in the debug. Is there any site that could help me with this?
The problem is that you're computing totalPizzasSold at line 33, but you're using it before that, at lines 29-32. At lines 29-32, totalPizzasSold still contains the value that you assigned at line 10.
Funny the program runs on cpp.sh as if totalPizzasSold initialized == 1:

http://cpp.sh/4axe

though it does warn about unused variables
ah, ok if it runs that makes sense. #inf isnt an error. Its a "special" value of floating point that print statements can handle that tell you when you goof up your math by doing things like dividing by zero. It literally prints some text with the letters inf (infinity) in it, or it should.

That aside, all you have to do is total before you divide. That is it. You are very close, move that one line of code before the divisions.

Last edited on
Topic archived. No new replies allowed.