c++ code output problem

***I have a problem with my output. When I input these values:

10
11
y
0
999999.9

This my output when I input those values, but it is wrong because the numbers are wrong:
Spools to be ordered
Spools in stock
Special shipping and handling (y or n)
Shipping and handling amount
Spools ready to ship: 11
Spools on back-order: -1
Subtotal ready to ship: $   1100.00
Shipping and handling:  $      0.00
Total shipping charges: $   1100.00


**This what the output is suppose to look like below:
Spools to be ordered
Spools in stock
Special shipping and handling (y or n)
Shipping and handling amount
Spools ready to ship: 10
Spools on back-order: 0
Subtotal ready to ship: $   1000.00
Shipping and handling:  $      0.00
Total shipping charges: $   1000.00


**Here is my coding below. If you can help fix this. Thank you.

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
#include <iostream>
#include <iomanip> // std::setprecision
using namespace std;
// Function Declarations
void readInputData(int& spoolsOrdered, int& spoolsInStock, double& shippingCharges);
void display(int spoolsOrdered, int spoolsInStock, double shippingCharges);
int main()
{
	// Declaring variables
	int spoolsOrdered, spoolsInStock;
	double shippingCharges;
	// Calling the functions
	readInputData(spoolsOrdered, spoolsInStock, shippingCharges);
	display(spoolsOrdered, spoolsInStock, shippingCharges);
	return 0;
}
// This function will read the inputs entered by the user
void readInputData(int& spoolsOrdered, int& spoolsInStock, double& shippingCharges)
{
	/* This while loop continues to execute
	* until the user enters a valid number
	*/
	while (true)
	{
		cout << "Spools to be ordered";
		cin >> spoolsOrdered;
		if (spoolsOrdered < 1)
		{
			cout << "\nSpools order must be 1 or more" << endl;
			continue;
		}
		else
			break;
	}
	/* This while loop continues to execute
	* until the user enters a valid number
	*/
	while (true)
	{
		cout << "\nSpools in stock";
		cin >> spoolsInStock;
		if (spoolsInStock < 1)
		{
			cout << "\nSpools in stock must be 0 or more" << endl;
			continue;
		}
		else
			break;
	}

	char ch;
	cout << "\nSpecial shipping and handling (y or n)";
	cin >> ch;
	if (ch == 'y')
	{
		/* This while loop continues to execute
		* until the user enters a valid number
		*/
		while (true)
		{
			cout << "\nShipping and handling amount";
			cin >> shippingCharges;
			if (shippingCharges < 0)
			{
				cout << "\nThe spool shipping and handling charge must be 0.0 or more" << endl;
				continue;
			}
			else
				break;
		}
	}
	else
	{
		shippingCharges = 11.88;
	}
}
// This function will display the output
void display(int spoolsOrdered, int spoolsInStock, double shippingCharges)
{
	// Setting the precision to two decimal places
	std::cout << std::setprecision(2) << std::fixed;
	cout << "\nSpools ready to ship: " << spoolsInStock << endl;
	cout << "Spools on back-order: " << spoolsOrdered - spoolsInStock << endl;
	double subtotal = spoolsInStock * 100;
	cout << "Subtotal ready to ship: $" << setw(10) << subtotal << endl;
	cout << "Shipping and handling:  $" << setw(10) << spoolsInStock * shippingCharges << endl;
	double total = (spoolsInStock * 100) + (spoolsInStock * shippingCharges);
	cout << "Total shipping charges: $" << setw(10) << total << endl;
}




Last edited on
Here’s the current output of your program:
Spools to be ordered10

Spools in stock11

Special shipping and handling (y or n)y

Shipping and handling amount0

Spools ready to ship: 11
Spools on back-order: -1
Subtotal ready to ship: $   1100.00
Shipping and handling:  $      0.00
Total shipping charges: $   1100.00

Why do you say it’s wrong?
Let’s do the math:

spoolsOrdered = 10
spoolsInStock = 11
shippingCharges = 0.0

"Spools ready to ship: " --> spoolsInStock --> 11
"Spools on back-order: " --> spoolsOrdered - spoolsInStock --> 10 - 11 --> -1
"Subtotal ready to ship: " --> spoolsInStock * 100 --> 11 * 100 --> 1100
"Shipping and handling: " --> spoolsInStock * shippingCharges --> 11 * 0 --> 0
"Total shipping charges: "
--> (spoolsInStock * 100) + (spoolsInStock * shippingCharges)
--> 11 * 100 + 11 * 0 --> 1100 + 0 --> 1100


Are you mixing "spoolsOrdered" and "spoolsInStock" up perhaps?
Topic archived. No new replies allowed.