C++ project help needed

I have to write a program for a class in C++ and when I run the program, the calculations for the subtotal do not calculate correctly. Also the city does not display when I run the program. Please help! Below are the instructions and the written code.
1.Calculate the cost of the doflingies and the sales tax. Then calculate the shipping cost and total cost.



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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <string>
#include <iomanip> /* Necessary for setw */
using namespace std;
int main ()
{
	string name, address, city, state, zipCode;
	int doflingies;

/* Prompt user to enter shipping information */
	cout << "Enter full name of the purchaser: ";
	getline(cin, name);				   
	cout << "Enter the street address: ";
	getline(cin, address);
	cout << "Enter city: ";
	cin >> city;
	getline(cin, city);
	cout << "Enter state abbreviation: ";
	cin >> state;
	cout << "Enter zip code: ";
	cin >> zipCode;
	cout << "Enter the amount of doflingies ordered: ";
	cin >> doflingies;

/* Display shipping information */
	cout << "Ship to: " << " " << name << endl;
	cout << right << setw(20) << address << endl;
	cout << " " " " " " << " " << city << ",";
	cout << state << " " << zipCode << endl;
	cout << endl;
	cout << "Number of Doflingies ordered: " << doflingies << endl;
	cout << endl;
	
	int huge, large, medium, small, remainder;
	huge = 50;
	large = 20;
	medium = 5;
	small = 1;
	remainder = 0;
	
/*Mathematical calculations */
	huge = doflingies/50;
	doflingies %= 50;
	large = doflingies/20;
	doflingies %= 20;
	medium = doflingies/5;
	doflingies %= 5;
	small = doflingies/1;
	doflingies %=1;
	
	
	
		/*Display the output */
	cout << left << setw(9) << "Countainer Size " << right << setw(20) << "Number required" << endl;
	cout << "-------------" << " " << setw(20) << " -----------" << endl;
	cout << left << setw(10) << "Huge" 
		 << right << setw(20) << huge << endl;
	cout << left << setw(10) << "Large" 
		 << right << setw(20) << large << endl;
	cout << left << setw(10) << "Medium" 
		 << right << setw(20) << medium << endl;
	cout << left <<setw(10) << "Small" 
		 << right << setw(20) << small << endl;
	cout << endl;
	


/*Assign doubles for cost */
	const double huge_cost = 4.00;
	const double large_cost = 2.00;
	const double medium_cost = 0.75;
	const double small_cost = 0.20;
 
	double shippingSubTotal;
	shippingSubTotal = (huge * huge_cost) + (large * large_cost) + (medium * medium_cost) + (small * small_cost);
 	cout << fixed << showpoint << setprecision(2) << endl;
	
	/*Assign constant doubles for doflingy prices and quantity discounts.*/
	const double priceOver500 = 15.95;
	const double price250_499 = 17.50;
	const double price100_249 = 18.95;
	const double price1_99 = 19.95;
	double doflingySubTotal = 0.0; 
	/*Calculations of Doflingies subtotal. */ 
 if (doflingies >= 500)
		doflingySubTotal = doflingies * priceOver500;    //the subtotal always ends up
	else if 
		(doflingies >= 250)                                          //being 0, no matter what
		doflingySubTotal = doflingies * price250_499;
	else if
		(doflingies >= 100)
		doflingySubTotal = doflingies * price100_249;
	else
		doflingySubTotal = doflingies * price1_99;
	
/*Assign a const double sales_tax at a value of 7.75% (0.0775) */
	const double sales_tax = 0.0775;
	double totalTax;
	totalTax = (doflingySubTotal + shippingSubTotal) * sales_tax;
 	double totalcost;
 	totalcost = doflingySubTotal + shippingSubTotal + totalTax;

/*Display receipt information */	
	cout << "---------------------------------" << endl;
	cout << "Doflingy total cost: " << setw(10) << "$" << doflingySubTotal << endl;
	cout << "Sales Tax: " << setw(15) << "$" << totalTax << endl;
	cout << "Shipping Cost: " << setw(10) << "$" << shippingSubTotal << endl;
	cout << endl;
	cout << "---------------------------------" << endl;
	cout << "Total: " << setw(10) << "$" << totalcost << endl;	
	
	
	
	

	return 0;
	
}
Last edited on
First of all, use [code][/code] tags to preserve formatting.

1
2
cin >> city;
getline(cin, city);

Remove the first line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*Mathematical calculations */
huge = doflingies/50;
doflingies %= 50;
large = doflingies/20;
doflingies %= 20;
medium = doflingies/5;
doflingies %= 5;
small = doflingies/1;
doflingies %=1;

//by now, doflingies < 5
//what happens in the calculations below?

/*Calculations of Doflingies subtotal. */ 
if (doflingies >= 500)
doflingySubTotal = doflingies * priceOver500;
else if 
(doflingies >= 250)
doflingySubTotal = doflingies * price250_499;
else if
(doflingies >= 100)
doflingySubTotal = doflingies * price100_249;
else
doflingySubTotal = doflingies * price1_99;
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <string>
#include <iomanip> /* Necessary for setw */

using namespace std;

int main ()
{
	string name, address, city, state, zipCode;
	int doflingies;

	/* Prompt user to enter shipping information */
	cout << "Enter full name of the purchaser: ";
	getline(cin, name);	/* Necessary for correct output */
	cout << "Enter the street address: ";
	getline(cin, address);
	cout << "Enter city: ";
	//cin>>city;
	getline(cin, city);
	cout << "Enter state abbreviation: ";
	cin >> state;
	cout << "Enter zip code: ";
	cin >> zipCode;
	cout << "Enter the amount of doflingies ordered: ";
	cin >> doflingies;


	/* Display shipping information */
	cout << "Ship to: " << " " << name << endl;
	cout << right << setw(20) << address << endl;
	cout << " " " " " " << " " << city << ",";
	cout << state << " " << zipCode << endl;
	cout << endl;

	cout << "Number of Doflingies ordered: " << doflingies << endl;
	cout << endl;

	int huge, large, medium, small, remainder;
	huge = 50;
	large = 20;
	medium = 5;
	small = 1;
	remainder = 0;

	/*Mathematical calculations */
	huge = doflingies/50;	//why did you initialize "huge" with 50 when it was going to be overriden?
	doflingies %= 50;
	large = doflingies/20;
	doflingies %= 20;
	medium = doflingies/5;
	doflingies %= 5;
	small = doflingies/1;
	doflingies %=1;

	/*Display the output */
	cout << left << setw(9) << "Countainer Size " << right << setw(20) << "Number required" << endl;
	cout << "-------------" << " " << setw(20) << " -----------" << endl;
	cout << left << setw(10) << "Huge" 
	<< right << setw(20) << huge << endl;
	cout << left << setw(10) << "Large" 
	<< right << setw(20) << large << endl;
	cout << left << setw(10) << "Medium" 
	<< right << setw(20) << medium << endl;
	cout << left <<setw(10) << "Small" 
	<< right << setw(20) << small << endl;
	cout << endl;

	/*Assign doubles for cost */
	const double huge_cost = 4.00;
	const double large_cost = 2.00;
	const double medium_cost = 0.75;
	const double small_cost = 0.20;

	double shippingSubTotal;
	shippingSubTotal = (huge * huge_cost) + (large * large_cost) + (medium * medium_cost) + (small * small_cost);
	cout << fixed << showpoint << setprecision(2) << endl;

	/*Assign constant doubles for doflingy prices and quantity discounts.*/
	const double priceOver500 = 15.95;
	const double price250_499 = 17.50;
	const double price100_249 = 18.95;
	const double price1_99 = 19.95;
	double doflingySubTotal = 0.0; 

	/*Calculations of Doflingies subtotal. */ 
	if (doflingies >= 500)
		doflingySubTotal = doflingies * priceOver500;
	else if (doflingies >= 250)
		doflingySubTotal = doflingies * price250_499;
	else if(doflingies >= 100)
		doflingySubTotal = doflingies * price100_249;
	else
		doflingySubTotal = doflingies * price1_99;

	/*Assign a const double sales_tax at a value of 7.75% (0.0775) */
	const double sales_tax = 0.0775;
	double totalTax;
	totalTax = (doflingySubTotal + shippingSubTotal) * sales_tax;
	
	double totalcost;
	totalcost = doflingySubTotal + shippingSubTotal + totalTax;

	/*Display receipt information */	
	cout << "---------------------------------" << endl;
	cout << "Doflingy total cost: " << setw(10) << "$" << doflingySubTotal << endl;
	cout << "Sales Tax: " << setw(15) << "$" << totalTax << endl;
	cout << "Shipping Cost: " << setw(10) << "$" << shippingSubTotal << endl;
	cout << endl;
	cout << "---------------------------------" << endl;
	cout << "Total: " << setw(10) << "$" << totalcost << endl;	
	
	return 0;
}


The problem with the city has been commented out.

The output i received is the expected.
Number of Doflingies ordered: 100

Countainer Size      Number required
-------------          -----------
Huge                         2
Large                        0
Medium                       0
Small                        0


---------------------------------
Doflingy total cost:          $0.00
Sales Tax:               $0.62
Shipping Cost:          $8.00

---------------------------------
Total:          $8.62


In line 46, huge gets to 2.
In line 47, doflingies gets to 0. 49,51,53 no longer make sense.

choose the right data types to suit your needs.

Thank you for your help, I have solved the city problem, but when I calculate the subtotal, I receive an answer of 0. As I understand it, depending on how many doflingies a person orders, the subtotal is calculated through if/else statement, but the calculations are always 0.

if (doflingies >= 500)
doflingySubTotal = doflingies * priceOver500;
else if (doflingies >= 250)
doflingySubTotal = doflingies * price250_499;
else if(doflingies >= 100)
doflingySubTotal = doflingies * price100_249;
else
doflingySubTotal = doflingies * price1_99;
CODEEEEE TAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS -
http://www.cplusplus.com/articles/jEywvCM9/
@Infinity223

Maybe you didn't see it in my post, and didn't understand the second part of shadowCODE's post either. In my post I said doflingies is less than 5. My mistake, it's actually 0.

Do you know what this part of your code does? doflingies %=1;

It is the same as saying doflingies = doflingies % 1;

In plain English, it means to set doflingies equal to the remainder if you divide doflingies by 1.

We all know that 1 divides evenly into any other integer, right? So what is the remainder? It is always 0.

Therefore, doflingies %=1; is the same as doflingies = 0;

Now what happens in the following calculations?

1
2
3
4
5
6
7
8
9
10
 if (doflingies >= 500)
		doflingySubTotal = doflingies * priceOver500;    //the subtotal always ends up
	else if 
		(doflingies >= 250)                                          //being 0, no matter what
		doflingySubTotal = doflingies * price250_499;
	else if
		(doflingies >= 100)
		doflingySubTotal = doflingies * price100_249;
	else
		doflingySubTotal = doflingies * price1_99;

We all know that anything multiplied by 0 is always 0. So doflingySubTotal will always be 0.
Topic archived. No new replies allowed.