c++ code help

I have a problem with my output when I input these numbers :

0
200
-1
12
y
-0.01
5.99

This is my output which is incorrect because it has those spacing's:

Spools to be ordered
Spools order must be 1 or more
Spools to be ordered
Spools in stock
Spools in stock must be 0 or more

Spools in stock
Special shipping and handling (y or n)
Shipping and handling amount
The spool shipping and handling charge must be 0.0 or more

Shipping and handling amount
Spools ready to ship: 12
Spools on back-order: 188
Subtotal ready to ship: $   1200.00
Shipping and handling:  $     71.88
Total shipping charges: $   1271.88


**Below is what the output should look like:

Spools to be ordered
Spools order must be 1 or more
Spools to be ordered
Spools in stock
Spools in stock must be 0 or more
Spools in stock
Special shipping and handling (y or n)
Shipping and handling amount
The spool shipping and handling charge must be 0.0 or more
Shipping and handling amount
Spools ready to ship: 12
Spools on back-order: 188
Subtotal ready to ship: $   1200.00
Shipping and handling:  $     71.88
Total shipping charges: $   1271.88



**Here is my coding:
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;
}


***If you could help me fix this problem. Thanks!***
Last edited on
Hello ,

Best to stick to one post or the other.

http://www.cplusplus.com/forum/beginner/225116/

Andy
Topic archived. No new replies allowed.