Help with 2 small issues I'm having in a store program!

Hey guys, I have two issues.

First one: My validating while statements aren't working exactly how I want them to work. I am validating that the input is in fact an integer. It works if I put in a wrong integer value, or a character, or a string that includes only text. If I put in a string that consists of both integer numbers and letters, it sends it off crazy. Either it will validate the while loop or in some cases it sends it into an infinite loop.

Second one: My total at checkout isn't quite right. This can be demonstrated with an example of purchasing the item for $21. The tax rate is 7.5%. Which should make the sales tax amount: $1.58. However upon the final total the sum comes out to $22.57. Which is 1 cent short of the actual amount it should be. What's causing this?

I would greatly appreciate any help!

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
//Children's Store Program Practice

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

	double dollars; //global variables
	double totaldollars = 0.00; //initializing a counter 
	string item, response;
	double totalcost = 0.00; //initializing a counter
	double cost;
		
	void checkout(double, double); //function prototypes
	double store();
	double piggybank();

int main()
{
	ifstream inFile;
	double subtotal = 0.00; 
	double BankBalance, BalanceLeft;

	BankBalance = piggybank();
	subtotal = store();
	checkout(subtotal, BankBalance);
	
	system("pause");
	return 0;
}

void checkout(double PurchaseSubTotal, double inBankBalance)
{
	srand(time(NULL)); //NULL is defined as 0 (zero)
	ofstream outFile; 
	ifstream inFile;
	int min = 1000;
	int max = 9999;
	int ReceiptID = rand()%1000 +1;
	int BankDebitID = (rand()%(max-min)+min);
	double taxTotal = 0.0750 * PurchaseSubTotal;
	double finalTotal = PurchaseSubTotal + taxTotal;
	double MoneyLeft = inBankBalance - finalTotal;
	double MoneyRemaining = (MoneyLeft * (-1));

	cout << "\nWelcome to checkout!" << endl;
	
	if (inBankBalance < finalTotal)
	{
		cout << "\nEFT Debit: **** **** **** " << BankDebitID << endl;
		cout << "US Debit: Declined" << endl;
		cout << "Unfortunately, you do not have enough funding in your piggy bank to complete this purchase." << endl;
		cout << "You are short by exactly: $" << MoneyRemaining << endl;
		cout << "\nThank you! Please visit the Toy Store another time!" << endl;
	}

	else
	{
		outFile.open("ToyStoreReceipt.txt");   // Saving receipt from purchase to a text file on computer
		outFile << fixed << setprecision(2);
		outFile << "Subtotal: $" << PurchaseSubTotal<< endl; 
		outFile << "Sales tax: 7.5%" << endl;
		outFile << "Sales tax applied: $" << taxTotal << endl;
		outFile << "Total price: $" << finalTotal << endl;
		outFile << "EFT Debit: **** **** **** " << BankDebitID << endl;
		outFile << "US Debit: Approved" << endl;
		outFile << "Receipt #: " << ReceiptID << endl;
		outFile << "\nThank you for your purchase! Please give our store a visit another time!" << endl;
		outFile.close();

		cout << "Here is your receipt." << endl;
		cout << "__________________________________" << endl;
		inFile.open("ToyStoreReceipt.txt");

	for(string ToyReceipt; getline(inFile, ToyReceipt);) // To get you all the lines
	{
		cout << ToyReceipt << "\n"; // "\n" or endl; is used to display the text as it was originally typed in multiple lines instread of one big unnecessary line
	}
		inFile.close();
	
		cout << "__________________________________" << endl;
		cout << "\nBalance Remaining in Piggy Bank: $" << MoneyLeft << endl;
	}
}

double piggybank()
{
	
	cout << "Parent," << endl;
	cout << "\nPlease add money to Bank America's Piggy Bank Plan for 3 days for your child." << endl;
	cout << "After the 3 day period, the child can immediately buy toys at our online store." << endl;

	for (int i = 1; i < 4; i++)
	{
			cout << "\nHow much money would you like to add to the piggy bank today? :$";
			cin >> dollars;

			while (dollars < 0)
			{
			cout << "Invalid Request. Try again. Make sure to enter $0.00 or more!" << endl;
			cin.clear();
			cin.ignore();
			cin >> dollars;
			}

			cout << "\nDay: " << i << " money = $";
			cout << fixed << setprecision(2) << dollars << endl;

			totaldollars += dollars; //counter for the total money invested in the piggy bank. It accumulates each daily deposit for the 3 days
			cout << "End of Day " << i << ": total in piggy bank as of right now is...$";
			cout << fixed << setprecision(2) << totaldollars << endl;
	}
	
	cout << "\nFinal total amount in piggy bank is...$" << totaldollars << endl;
	cout << "Now that you have money you can buy items from the toy store!\n" << endl;
	return totaldollars;
}

double store()
{	

do {
	cout << "TAMS' TOY STORE" << endl;
	cout << "Items #      Description       Price" << endl;
	cout << "____________________________________" << endl;
	cout << "142          Action Figure     $9" << endl;
	cout << "123          Toy Boat          $7" << endl;
	cout << "189          R/C Car           $21" << endl;
	cout << "____________________________________" << endl;
	
	cout << "\nEnter item #";
	cin >> item;

	while (item != "142" && item != "123" && item != "189")
	{
		cout << "Error. Item not found or you entered an invalid search criteria..." << endl;
		cout << "Please try again." << endl;
		cin >> item;
	}

	if (item == "142")
	{
		cout << "Action Figure added to cart for $9" << endl;
		cost = 9.00;
		totalcost += cost;
		cout << "Your current subtotal: $" << totalcost << endl;
		cout << "Press '1' to continue shopping or '0' to go to checkout!" << endl;
		cin >> response;
	}
	else if (item == "123")
	{
		cout << "Toy Boat added to cart for $7" << endl;
		cost = 7.00;
		totalcost += cost;
		cout << "Your current subtotal: $" << totalcost << endl;
		cout << "Press '1' to continue shopping or '0' to go to checkout!" << endl;
		cin >> response;
	}
	else if (item == "189")
	{
		cout << "R/C Car added to cart for $21" << endl;
		cost = 21.00;
		totalcost += cost;
		cout << "Your current subtotal: $" << totalcost << endl;
		cout << "Press '1' to continue shopping or '0' to go to checkout!" << endl;
		cin >> response;
	}

	while (response != "1" && response != "0")
	{
		cout << "Try again. Enter 1 to continue shopping or 0 for checkout." << endl;
		cin >> response;
	}

	if (response == "0")
	{
		return totalcost;
	}

	}
	while (response == "1");

}
Last edited on
MisterTams,
I recommend using a 'char response' not string. A string will take multiple chars. Which is why your are getting an error.
Topic archived. No new replies allowed.