User inputs incorrect display error message and ask to re-enter correct input

I'm almost finished with my program. Just left with the error checking which I have no idea to do. I tried using do while loops but it's not working for me. I'm really new to programming so any help would be appreciated. I bolded the parts where I need help displaying error and re-enter input.

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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

void StartScreen(int five, int ten, int twenty, int fifty, double total){
	
	cout << "\n \n \n      ..: JAQK POT :..      " << endl;
    cout << " ****************************" << endl;
	cout << " Jack    Ace    Queen    King" << endl;
	cout << " 5c      10c    20c      50c " << endl;
	cout << " " << five << "       " << ten  << "      "<< twenty << "        " << fifty << endl;
	
	cout << "\n Total = RM" << total << endl;
}

void clScreen(){
	for(int i = 0; i < 100; ++i){
		cout << "\n";
	}
}

int main()
{
	char start;
	
	cout << "+----------------------+" << endl;
	cout << "|  ------------------  |" << endl;
	cout << "|                      |" << endl;
	cout << "|     ------------     |" << endl;
	cout << "+----------------------+" << endl;
	
	cout << "\n Press any key to continue . . .";
	cin >> start;
	clScreen();
	string x, exit = "p";
	srand (time(NULL));
	int max = 10;
	int five = rand() % max;
	max -= five;
	int ten = rand() % max;
	max -= ten;
	int twenty = rand() % max;
	max -= twenty;
	int fifty = max;
	int j, a, k, q;
	double total = five*0.05 + ten*0.1 + twenty*0.2 + fifty*0.5, amount;
	double c05 = 0, c10 = 0, c20 = 0, c50 = 0;
	
	
	while (exit != "N" && exit != "n"){
		clScreen();
		
		StartScreen(five, ten, twenty, fifty, total);

		cout << "\n Press D or d if you want to deposit." << endl;
		cout << " Press W or w if you want to withdraw." << endl;
		cout << " Press R or r if you want to reset." << endl;
		cout << "==>";
		cin >> x; 
		if(x == "D" || x == "d"){
			cout << "How many 5c coins? ==> ";
			cin >> j;
			five += j;
			cout << "How many 10c coins? ==> ";
			cin >> a;
			ten += a;
			cout << "How many 20c coins? ==> ";
			cin >> q;
			twenty += q;
			cout << "How many 50c coins? ==> ";
			cin >> k;
			fifty += k;
		} else if (x == "W" || x == "w"){
			cout << "Please enter the amount (in RM) => ";
			cin >> amount;
			int tmp = amount*100;
			if(tmp%5 != 0 || amount > total){
				cout << "INVALID";
			} else {
				while(amount > 0){
					if(fifty > 0 && amount >= 0.5){
						fifty -= 1;
						c50 += 1;
						amount -= 0.5;
					} else if (twenty > 0 && amount >= 0.2){
						twenty -= 1;
						c20 += 1;
						amount -= 0.2;
					} else if (ten > 0 && amount >= 0.1){
						ten -= 1;
						c10 += 1;
						amount -= 0.1;
					} else if (five > 0 && amount >= 0.05){
						five -= 1;
						c05 += 1;
						amount -= 0.05;
					}
				}
				cout << "Yes, Please collect your coins: "; 
				if (c50 > 0){
					cout << "\n 50c x " << c50 << endl;
					cout << "20c x " << c20 << endl;
					cout << "10c x " << c10 << endl;
					cout << "5c x " << c05 << endl;
				}
			}
		} else if (x == "R" || x == "r"){
			cout << "How many 5c coins? ==> ";
			cin >> five;
			cout << "How many 10c coins? ==> ";
			cin >> ten;
			cout << "How many 20c coins? ==> ";
			cin >> twenty;
			cout << "How many 50c coins? ==> ";
			cin >> fifty;
		} else {
			cout << "Invalid input";
		}
		StartScreen(five, ten, twenty, fifty, total);
		total = five*0.05 + ten*0.1 + twenty*0.2 + fifty*0.5;
		cout << "\n Total = RM" << total << endl;
		cout << "\n Please press any key to continue," << endl;
		cout << " press N or n to exit the program => ";
		cin >> exit;
	}
	return 0;
}
new people need to take a lesson from you on how to ask a question. The bolding didn't take but this is exactly how to get help, nicely done.

cout << "How many 10c coins? ==> ";
cin >> a;

ok, so what happens if some user types in the word "fifteen" here?

now consider...
string a; //we need to discuss variable names later... try to use something you can understand what it is by its name...
...
cin >> a;
a_the_int = stoi(a);

and we know that stoi throws an exception when it fails.
to deal with *that* ... you need this sort of code:

1
2
3
4
5
6
7
8
9
10
11
12
bool valid = false;

do
 try {
        a_the_int = stoi(a);
         valid = true;
  }
  catch (const std::invalid_argument& ia) {
	  std::cerr << "Invalid input \n ";  //you can get more details out of ia but this is enough here
      valid = false;
  }
while (!valid);


Hopefully this is correct. I am a little rusty on try/catch blocks. I shamelessly stole it from another place and filled it in.

you can add to this. You can check to see if it is in the range 0-100 or something for example, and if not, print an error and set valid to false on that condition too. Just add everything you want to validate to it.
Last edited on
Oh that worked wonders!!! Thank you so muchhhh!!!!
Now I have another problem. This time its the withdrawing part. If I have a total of 1 jack(5c), 1 ace(10c), 1 queen(20c) and 1 king(50c). I would have a total of 0.85. If I want to withdraw 70cents. It would take 1 king and 1 queen. But if I take out 0.5. It works! It takes out 1 king. I dont see where my problem is. Any help would be appreciated.

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

 else if (x == "W" || x == "w"){
			cout << "Please enter the amount (in RM) => ";
			cin >> amount;
			int tmp = amount*100;
			if(tmp%5 != 0 || amount > total){
				cout << "INVALID";
			} else {
				while(amount > 0){
					if(fifty > 0 && amount >= 0.5){
						fifty -= 1;
						c50 += 1;
						amount -= 0.5;
					} else if (twenty > 0 && amount >= 0.2){
						twenty -= 1;
						c20 += 1;
						amount -= 0.2;
					} else if (ten > 0 && amount >= 0.1){
						ten -= 1;
						c10 += 1;
						amount -= 0.1;
					} else if (five > 0 && amount >= 0.05){
						five -= 1;
						c05 += 1;
						amount -= 0.05;
					}
				}
				cout << "Yes, Please collect your coins: "; 
				if (c50 > 0){
					cout << "\n 50c x " << c50 << endl;
					cout << "20c x " << c20 << endl;
					cout << "10c x " << c10 << endl;
					cout << "5c x " << c05 << endl;
				}
			}
You should not expect floating point numbers to be exact. There are many numbers that cannot be represented exactly. 0.85 is one of them. What you get instead is an approximation. If the numbers need to be exact you should use integers.
how do i make it that so if I want to take out 0.55, it will take out 1 king and 1 jack?
I would've stored everything in cents using integers and only done the conversion from/to dollar when doing input/output.
Topic archived. No new replies allowed.