Taking in a series of orders with a loop, and then outputting the final order total

Hi,

I just made an account here after searching on the web for a bit for an answer. I have a c++ programming assignment that takes in information from the user and then gives them an order total. I have the program while looped so that it will keep taking in orders until the user enters a sentinel. I also have to add up all of the order totals together once the user is finished inputting to output a final total. I'm not sure how to go about doing this.

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
#include <iostream>

using namespace std;

int orderedBooks(int enrollment, int copies)
{
enrollment - copies;
int bookstoOrder = enrollment - copies;

return bookstoOrder;
}

int totalOrderCost(int orderedBooks, int bookCost)
{
orderedBooks * bookCost;
int costofOrder = orderedBooks * bookCost;

return costofOrder;
}

int main()
{
char code[80];
char requiredBook;
char bookUsage;
double bookCost;
int copiesOnHand;
int prospectiveEnrollment;
char enterBook;

cout << "Would you like to enter a book to find the store profit and total cost (Y/N)?";
cin >> enterBook;

while (enterBook == 'y' || enterBook == 'Y')

{cout << "What is the code for the book?: ";
cin >> code;

cout << "What is the cost of the book?: $";
cin >> bookCost;

cout << "How many copies of this book are on hand?: ";
cin >> copiesOnHand;

cout << "What is the prospective class enrollment?: ";
cin >> prospectiveEnrollment;

cout << "Is the book required for the class (Y/N)?: ";
cin >> requiredBook;

cout << "Is the book new (Y/N)?: ";
cin >> bookUsage;

cout << endl;

cout << "Code: " << code << endl;

cout << "Book cost: $" << bookCost << endl;

cout << "Number of copies on hand: " << copiesOnHand << endl;

cout << "Prospective class enrollment: " << prospectiveEnrollment << " students" << endl;

cout << "Is the book required (Y/N)?: " << requiredBook << endl;

cout << "Is the book new (Y/N)?: " << bookUsage << endl;

if (bookUsage == 'Y' || bookUsage == 'y')
{
orderedBooks(prospectiveEnrollment, copiesOnHand);
cout << "Books to be ordered: " << orderedBooks(prospectiveEnrollment, copiesOnHand) << endl;

totalOrderCost(orderedBooks(prospectiveEnrollment, copiesOnHand), bookCost);
cout << "Order total: $" << totalOrderCost(orderedBooks(prospectiveEnrollment, copiesOnHand), bookCost) << endl;
}

if (bookUsage == 'N' || bookUsage == 'n')
{
cout << "Books to be ordered: 0" << endl;
}

cout << endl;

cout << "Would you like to enter a another book (Y/N)?: ";
cin >> enterBook;

}


return 0;
}
example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>

int main()
{
	// declare a sum var outside of your loop
	int sum(0);

	for (int i = 0; i < 50; ++i)
	{
		// i in this case is your book cost, i'm just using the loop variable
		// for convenience
		sum += i;
	}

	std::cout << "Sum: " << sum << std::endl;
	return 0;
}


note: you cannot calculate and display the full final total inside your loop, only a running total so far.
Last edited on
We were just introduced to counters this week, but we have not really used them. I'm trying to add up all of my totalOrderCost, obtained from my function. I tried to do this, but I don't think that this is what you meant.
1
2
3
4
5
6
double finalOrderCost = 0; //Declared outside of my loop

for (double finalOrderCost = 0; finalOrderCost < 50; ++finalOrderCost)
	{
		finalOrderCost += totalOrderCost(orderedBooks(prospectiveEnrollment, copiesOnHand), bookCost);
	}
Last edited on
Nearly. just replace your line 3 with my line 8. i.e. don't use the variable you've defined to hold your cost in the for condition.
Topic archived. No new replies allowed.