How to add a loop, user-defined function and an array/structure to the program

Hello, guys.

I have to do this assignment until tomorrow. We had to write a "selling program for computers, laptops and tablets", which I did but for the extra credit, we have to have those three points in the application and I have tried the entire weekend to do that but didn't know how to do the "extra credit" part, which I really need.

The application is pretty basic and easy but I have to also have these three things and I was wondering if you guys could help me with these:

1.) A loop to prompt the user if they would like to place another order

2.) At least one user-defined function

3.) An enumerated data type, array or struct (structure)


I did one of these three, it's a "DO WHILE" loop asking users if they want to make another order, it's right at the beginning of the code.

Anyway, this is the source code and I will be really, very grateful if someone helps me out here.



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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

double desktop();		//function prototype
double laptop();
double tablet();


int main()
{

	char order, makeAnotherOrder;
	double amountDue;

	do
	{

	cout << "Please select your model (d, l, t)\n";

	cin >> order;

	if (order == 'd')
		amountDue = desktop();

	if (order == 'l')
		amountDue = laptop();

	if (order == 't')
	amountDue = tablet();

		cout << "The total amount with a discount (if you were eligible for one) is : $" << amountDue << endl;

		cout << "Would you like to place another order? Press 'y' to confirm.\n";
		cin >> makeAnotherOrder;

		} //closing brace of the loop statement

		while (makeAnotherOrder == 'y' || makeAnotherOrder == 'Y');

		return 0;

}

double desktop()
{

	int quantity;
	double desktopAmount;
	double discount = .1;
	const double price = 999;
	double subtotal, tax, totalPrice;

	cout << "Please enter how many desktop computers you want to buy: \n";

	cin >> quantity;


	if (quantity > 3)
	{
		desktopAmount = quantity * price;
		tax = price * .07;
		totalPrice = desktopAmount + tax;
		subtotal = totalPrice - discount;
		cout << "You are entitled to a discount!\n";
	}

	else

	desktopAmount = quantity * price;
	tax = price * .07;
	totalPrice = desktopAmount + tax;
	cout << "The total price you have to pay is: \n"
	<< totalPrice << endl;


	cout << setfill('.') << left << setw(35) << "Quantity: " << right << " " << quantity << endl;

	cout << left << setfill ('.') << setw(35) << "Price: " << setfill(' ') << right << " $" << setw(1) << price << endl;

	cout << setfill('.') << left << setw(28) << "Tax: " << setfill('.') << right << setw(9) << "$" << tax << endl;

	cout << left << setfill ('.') << setw(35) << "Total Price: " << setfill(' ') << right << " $" << setw(7) << totalPrice << endl;


	return desktopAmount;

}


double laptop()
{

	int quantity;
	double laptopAmount;
	double discount = .1;
	const double price = 699;
	double subtotal, tax, totalPrice;

	cout << "Please enter how many laptops you want to buy: \n";

	cin >> quantity;


	if (quantity > 5)
	{
		laptopAmount = quantity * price;
		tax = price * .07;
		totalPrice = laptopAmount + tax;
		subtotal = totalPrice - discount;
		cout << "You are entitled to a discount!\n";
	}

	else

	laptopAmount = quantity * price;
	tax = price * .07;
	totalPrice = laptopAmount + tax;
	cout << "The total price you have to pay is: \n"
	<< totalPrice << endl;


	cout << setfill('.') << left << setw(35) << "Quantity: " << right << " " << quantity << endl;

	cout << left << setfill ('.') << setw(35) << "Price: " << setfill(' ') << right << " $" << setw(1) << price << endl;

	cout << setfill('.') << left << setw(28) << "Tax: " << setfill('.') << right << setw(9) << "$" << tax << endl;

	cout << left << setfill ('.') << setw(35) << "Total Price: " << setfill(' ') << right << " $" << setw(7) << totalPrice << endl;


	return laptopAmount;

}


double tablet()
{

	int quantity;
	double tabletAmount;
	double discount = .1;
	const double price = 399;
	double subtotal, tax, totalPrice;

	cout << "Please enter how many tablets you want to buy: \n";

	cin >> quantity;


	if (quantity > 10)
	{
		tabletAmount = quantity * price;
		tax = price * .07;
		totalPrice = tabletAmount + tax;
		subtotal = totalPrice - discount;
		cout << "You are entitled to a discount!\n";
	}

	else

	tabletAmount = quantity * price;
	tax = price * .07;
	totalPrice = tabletAmount + tax;
	cout << "The total price you have to pay is: \n"
	<< totalPrice << endl;



	cout << setfill('.') << left << setw(35) << "Quantity: " << right << " " << quantity << endl;

	cout << left << setfill ('.') << setw(35) << "Price: " << setfill(' ') << right << " $" << setw(1) << price << endl;

	cout << setfill('.') << left << setw(28) << "Tax: " << setfill('.') << right << setw(9) << "$" << tax << endl;

	cout << left << setfill ('.') << setw(35) << "Total Price: " << setfill(' ') << right << " $" << setw(7) << totalPrice << endl;





	return tabletAmount;

}
Last edited on
No one is gonna do your homework, buddy. Read those tutorials, and if you have questions, we'd be glad to help.

1) http://www.cplusplus.com/doc/tutorial/control/

2) http://www.cplusplus.com/doc/tutorial/functions/

3) http://www.cplusplus.com/doc/tutorial/structures/
Just put ur menu in a while loop, And try not to use "do". Its a proper C-function but not really needed in c++.
Topic archived. No new replies allowed.