Error Traping

Pages: 12
How can I error trap my program that allows only numbers to be enter? Thanks
closed account (SECMoG1T)
show us your code.
I can't sir. It says that the codes is too long.
It divide it into two sir.

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
#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <ctype.h>
#include <string>
using namespace std;

int main()
{
		SetConsoleTitle( TEXT( "Vending Machine" ) );
		char moreCoins,transactNew;
		string choiceDrinks;
        int amount = 0, current = 0,change;
        bool isRunning = true;

startUp:

	cout<<"**********************************************"<<endl;
	cout<<"*---------------Vending Machine--------------*"<<endl;
	cout<<"**********************************************";

	cout<<endl<<endl;
		cout<<"\n1.Insert 1php coin";
		cout<<"\n5.Insert 5php coin";
		cout<<"\n10.Insert 10php coin"<<endl;
	
		cout<<"\na. Orange Juice --------------------- 8.00php"<<endl;
		cout<<"b. Apple Juice ---------------------- 8.00php"<<endl;
		cout<<"c. Mango Juice ---------------------- 8.00php"<<endl;
		cout<<"d. Pineapple Juice ------------------ 9.00php"<<endl;
		cout<<"e. Milo Juice ---------------------- 10.00php"<<endl;
		cout<<"f. Milk Juice ---------------------- 10.00php"<<endl;
		cout<<"g. Iced Milo ----------------------- 11.00php"<<endl;
		cout<<"h. Lemon Iced Tea ------------------ 11.00php"<<endl;
		cout<<"i. Hot Choco ----------------------- 12.00php"<<endl;
		cout<<"j. 3 in 1 Coffee ------------------- 12.00php"<<endl;


	do
	{

coins:

		cout<<"\nCurrent amount: "<<current<<".00php";
	    
		cout<<"\nInsert coin: ";
		cin>>amount;
		
	    
		cout<<endl;
		{
			if (amount == 0)
			{
				isRunning = false;
				break;
			}
			else if ((amount == 2)||(amount == 3)|| (amount == 4)|| (amount == 6)|| (amount == 7)|| (amount == 8)||(amount == 9)||(amount > 10))
			{
				system("cls");
				goto startUp;
				break;
			}
			else if ((amount == 1)||(amount == 5)||(amount == 10))
			{
				current = current + amount;
			}
	
				if (current <= 7)
				{
					cout<<"";
				}
				else if (current == 8)
				{
					cout<<endl;
					cout<<"Current amount: "<<current<<".00php";
	
					cout<<"\na. Orange Juice --------------------- 8.00php"<<endl;
					cout<<"b. Apple Juice ---------------------- 8.00php"<<endl;
					cout<<"c. Mango Juice ---------------------- 8.00php"<<endl;
				}
				else if (current == 9)
				{
					cout<<endl;
					cout<<"Current amount: "<<current<<".00php";

					cout<<"\na. Orange Juice --------------------- 8.00php"<<endl;
					cout<<"b. Apple Juice ---------------------- 8.00php"<<endl;
					cout<<"c. Mango Juice ---------------------- 8.00php"<<endl;
					cout<<"d. Pineapple Juice ------------------ 9.00php"<<endl;
				}
				else if (current == 10)
				{
					cout<<"Current amount: "<<current<<".00php";
	
					cout<<"\na. Orange Juice --------------------- 8.00php"<<endl;
					cout<<"b. Apple Juice ---------------------- 8.00php"<<endl;
					cout<<"c. Mango Juice ---------------------- 8.00php"<<endl;
					cout<<"d. Pineapple Juice ------------------ 9.00php"<<endl;
					cout<<"e. Milo Juice ---------------------- 10.00php"<<endl;
					cout<<"f. Milk Juice ---------------------- 10.00php"<<endl;
				}
				else if (current == 11)
				{
					cout<<"Current amount: "<<current<<".00php";
	
					cout<<"\na. Orange Juice --------------------- 8.00php"<<endl;
					cout<<"b. Apple Juice ---------------------- 8.00php"<<endl;
					cout<<"c. Mango Juice ---------------------- 8.00php"<<endl;
					cout<<"d. Pineapple Juice ------------------ 9.00php"<<endl;
					cout<<"e. Milo Juice ---------------------- 10.00php"<<endl;
					cout<<"f. Milk Juice ---------------------- 10.00php"<<endl;
					cout<<"g. Iced Milo ----------------------- 11.00php"<<endl;
					cout<<"h. Lemon Iced Tea ------------------ 11.00php"<<endl;
				}
				else if (current > 11)
				{
					cout<<"Current amount: "<<current<<".00php";
	
					cout<<"\na. Orange Juice --------------------- 8.00php"<<endl;
					cout<<"b. Apple Juice ---------------------- 8.00php"<<endl;
					cout<<"c. Mango Juice ---------------------- 8.00php"<<endl;
					cout<<"d. Pineapple Juice ------------------ 9.00php"<<endl;
					cout<<"e. Milo Juice ---------------------- 10.00php"<<endl;
					cout<<"f. Milk Juice ---------------------- 10.00php"<<endl;
					cout<<"g. Iced Milo ----------------------- 11.00php"<<endl;
					cout<<"h. Lemon Iced Tea ------------------ 11.00php"<<endl;
					cout<<"i. Hot Choco ----------------------- 12.00php"<<endl;
					cout<<"j. 3 in 1 Coffee ------------------- 12.00php"<<endl;
				}
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
if ((current >= 8))
					{

yesNo:
						cout<<"\nDo you want to insert more coins(y/n)?";
						cin>>moreCoins;
							
							switch (moreCoins)
							{
							case 'y':
								goto coins;
								break;
							case 'n':
enterChoice:
								cout<<"Enter your choice: \b";
								cin>>choiceDrinks;
								goto choices;
								break;
							default:
								goto yesNo;
							}
					}
choices:
							if (choiceDrinks == "a")
							{
								cout<<endl;
								cout<<"\nYou've selected Orange Juice!"<<endl;
								change = current - 8;
								cout<<"Your change is "<<change<<".00php"<<endl;
								cout<<"Enjoy your drink!"<<endl;
								goto transNew;
							}
							else if (choiceDrinks == "b")
							{
								cout<<endl;
								cout<<"\nYou've selected Apple Juice!"<<endl;
								change = current - 8;
								cout<<"Your change is "<<change<<".00php"<<endl;
								cout<<"Enjoy your drink!"<<endl;
								goto transNew;
							}
							else if (choiceDrinks == "c")
							{
								cout<<endl;
								cout<<"\nYou've selected Mango Juice!"<<endl;
								change = current - 8;
								cout<<"Your change is "<<change<<".00php"<<endl;
								cout<<"Enjoy your drink!"<<endl;
								goto transNew;
							}
							else if (choiceDrinks == "d")
							{
								cout<<endl;
								cout<<"\nYou've selected Pineapple Juice!"<<endl;
								change = current - 9;
								cout<<"Your change is "<<change<<".00php"<<endl;
								cout<<"Enjoy your drink!"<<endl;
								goto transNew;
							}
							else if (choiceDrinks == "e")
							{
								cout<<endl;
								cout<<"\nYou've selected Milo Juice!"<<endl;
								change = current - 10;
								cout<<"Your change is "<<change<<".00php"<<endl;
								cout<<"Enjoy your drink!"<<endl;
								goto transNew;
							}
							else if (choiceDrinks == "f")
							{
								cout<<endl;
								cout<<"\nYou've selected Milk Juice!"<<endl;
								change = current - 10;
								cout<<"Your change is "<<change<<".00php"<<endl;
								cout<<"Enjoy your drink!"<<endl;
								goto transNew;
							}
							else if (choiceDrinks == "g")
							{
								cout<<endl;
								cout<<"\nYou've selected Iced Milo!"<<endl;
								change = current - 11;
								cout<<"Your change is "<<change<<".00php"<<endl;
								cout<<"Enjoy your drink!"<<endl;
								goto transNew;
							}
							else if (choiceDrinks == "h")
							{
								cout<<endl;
								cout<<"\nYou've selected Lemon Iced Tea!"<<endl;
								change = current - 11;
								cout<<"Your change is "<<change<<".00php"<<endl;
								cout<<"Enjoy your drink!"<<endl;
								goto transNew;
							}
							else if (choiceDrinks == "i")
							{
								cout<<endl;
								cout<<"\nYou've selected Hot Choco!"<<endl;
								change = current - 12;
								cout<<"Your change is "<<change<<".00php"<<endl;
								cout<<"Enjoy your drink!"<<endl;
								goto transNew;
							}
							else if (choiceDrinks == "j")
							{
								cout<<endl;
								cout<<"\nYou've selected 3 in 1 Coffee!"<<endl;
								change = current - 12;
								cout<<"Your change is "<<change<<".00php"<<endl;
								cout<<"Enjoy your drink!"<<endl;
								goto transNew;
							}
							else
							{
								goto enterChoice;
							}
transNew:
							cout<<"\nDo you want another transaction(Y/N)?";
							cin>>transactNew;
							toupper(transactNew);
							
							switch (transactNew)
							{
							case 'y':
								current = 0;
								system("cls");
								goto startUp;
								break;
							case 'n':
								cout<<"\nGood bye! Have a nice day!"<<endl;
								system("pause");
								return 0;
								break;
							default:
								goto transNew;
							}	
		}
			
			
	}
			while (isRunning);
			system("pause");
getch();
return 0;
}
closed account (SECMoG1T)
well i see now but i'll be fine if you just call me andy, sir is way to big for me at-least not for now, well back to business what error would you like to trap within you code? some more details about the error would help ease my lookup.
I can't enter any amount andy, I don't know why. The program will accept only, 1, 5, 10. It will not accept 2,3,4,6,7,8,9 and letters.
closed account (SECMoG1T)
well the way you use your gotos and loops might be surprisingly difficult to follow but that will be no problem, if you don't mind we could go through your program one step at a time and transform it into something simpler.

edit: your problem must be resulting from your overall structure .
Last edited on
no problem with me. i'll follow everything that you'll say :)
closed account (SECMoG1T)
are aware of functions
how can we make it simpler? what you mean by overall structure?
closed account (SECMoG1T)
we can make it simpler by using functions
 overall structure? 
am getting dizzy following this gotos.
how sir? i'm not aware of that functions but you can make it andy then if it is okay, can you explain it and i will study it :) thanks so much!
@ryanjoshiii I just want to add that, Using gotos is very bad programing practice, and shouldnt really use them, its a sign of bad design. atleast thats what Ive picked up from everyone.
why sir? I think its because it destroys the structure of the program?
closed account (SECMoG1T)
if we get some functions such as these
1
2
3
4
1. show_menu()
2. show_choice()
3.show_change()
4. manage()///conrols the whole program 


things might get a little better

ohh srry i'm not aware of that functions maybe then we can restructure your program to avoid gotos
Last edited on
closed account (SECMoG1T)
 It will not accept 2,3,4,6,7,8,9 and letters. 


1
2
3
4
5
6
else if ((amount == 2)||(amount == 3)|| (amount == 4)|| (amount == 6)|| (amount == 7)|| (amount == 8)||(amount == 9)||(amount > 10))
{
	system("cls");
	goto startUp;
	break;
}


that will never allow you to use those values
How's that work andy? Please guide me more. I'm so sorry if my codings is not proper and dirty. I'm so newbie with c++ and I just know the basics.
yes. but how can make the variable amount to accept only 1php, 5php and 10php? not to accept letters and 2,3,4,6,7,8,9?
closed account (SECMoG1T)
what that means is that whenever the amount equates to any of those values you will always pass the control to the goto .

but how can make the variable amount to accept only 1php, 5php and 10php
that is what it currently does or maybe i din't get that clearly .
Pages: 12