Validation so that the program only allows entry of numbers

Hi, could someone help me with my program. I have my program that is from a restaurant where you have to enter what you will eat for lunch, drink and dessert. The program gives you the invoice-like quantities and prices, all right there, the problem is that I need that when you enter any character that is not a number the program allows you to re-select the correct option for each option. If anyone could help me with this I would greatly appreciate it.

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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <iostream>
using namespace std;
 
const float torta=20000;
const float helado=15000;
const float oferta_postre=8000;
const float sopa=15000;
const float arroz_pollo=25000;
const float pasta_bolona=35000;
const float oferta_almuerzo=15000;
const float jarra_jugo=15000;
const float vaso_jugo=5000;
const float jarra_agua=10000;
const float vaso_agua=3000;
float total = 0;
int continuar = 0;
int acum_op1 = 0;
int acum_op2 = 0;
int acum_op3 = 0;
int acum_op4 = 0;
int acum_op5 = 0;
int acum_op6 = 0;
int acum_op7 = 0;
int acum_op8 = 0;
int acum_op9 = 0;
int acum_op10 = 0;
int acum_op11 = 0;
int opcion=0;
 
void pedido(int op){
	int cantidad = 0;
	cout<<"Ingrese la cantidad a pedir:  "<<endl;
	cin>>cantidad;
	switch(op)
	{
		case 1:
			total = total + (sopa * cantidad);
			acum_op1 = acum_op1 + cantidad;
			break;
		case 2:
			total = total + (arroz_pollo * cantidad);
			acum_op2 = acum_op2 + cantidad;
			break;
		case 3:
			total = total + (pasta_bolona * cantidad);
			acum_op3 = acum_op3 + cantidad;
			break;
		case 4:
			total = total + (oferta_almuerzo * cantidad);
			acum_op4 = acum_op4 + cantidad;
			break;
		case 5:
			total = total + (jarra_jugo * cantidad);
			acum_op5 = acum_op5 + cantidad;
			break;
		case 6:
			total = total + (vaso_jugo * cantidad);
			acum_op6 = acum_op6 + cantidad;
			break;
		case 7:
			total = total + (jarra_agua * cantidad);
			acum_op7 = acum_op7 + cantidad;
			break;
		case 8:
			total = total + (vaso_agua * cantidad);
			acum_op8 = acum_op8 + cantidad;
			break;
		case 9:
			total = total + (torta * cantidad);
			acum_op9 = acum_op9 + cantidad;
			break;
		case 10:
			total = total + (helado * cantidad);
			acum_op10 = acum_op10 + cantidad;
			break;
		case 11:
			total = total + (oferta_postre * cantidad);
			acum_op11 = acum_op11 + cantidad;
			break;
		default:
			cout<<"Ingreso una opción invalida"<<endl;
			break;
	}
}
 
void submenu(int op){
	int plato = 0;
	if(op == 1)
	{
		cout <<"En el almuerzo hay:  "<<endl<<endl;
		cout <<"1) Sopa = 15000 "<<endl;
		cout <<"2) Arroz con Pollo = 25000 "<<endl;
		cout <<"3) Pasta Boloñesa = 35000 "<<endl;
		cout <<"4) OFERTA Especial = 15000 (1 bebida y Arroz con Salsa) "<<endl<<endl;
		cin>>plato;
		if(plato >= 1 && plato <= 4){
			pedido(plato);
		}
		else{
			cout<<"Ingreso una opción invalida"<<endl;
			submenu(op);
		}
	}
	else if(op == 2)
	{
		cout <<"De bebida tenemos:  "<<endl<<endl;
		cout <<"5) Jarra de Jugo = 15000 "<<endl;
		cout <<"6) Vaso de Jugo = 5000 "<<endl;
		cout <<"7) Jarra de Agua = 10000 "<<endl;
		cout <<"8) Vaso de Agua = 3000 "<<endl;
		cin>>plato;
		if(plato >= 5 && plato <= 8){
			pedido(plato);
		}
		else{
			cout<<"Ingreso una opción invalida"<<endl;
			submenu(op);
		}
	}
	else{
		cout <<" De postre hay:  "<<endl<<endl;
		cout <<"9)torta = 20000 "<<endl;
		cout <<"10)helado = 15000 "<<endl;
		cout <<"11)OFERTA ESPECIAL = 8000 (1 cafe y galletas dulces) "<<endl<<endl;
		cin>>plato;
		if(plato >= 9 && plato <= 11){
			pedido(plato);
		}
		else{
			cout<<"Ingreso una opción invalida"<<endl;
			submenu(op);
		}
	}
}
 
 
void menu(){
	cout<<"1) Almuerzo"<<endl;
	cout<<"2) Bebida"<<endl;
	cout<<"3) Postre"<<endl;
	cin>>opcion;
 
	switch(opcion){
		case 1:
			submenu(1);
			break;
		case 2:
			submenu(2);
			break;
		case 3:
			submenu(3);
			break;
		default:
			cout<<"Ingreso una opción invalida"<<endl;
			menu();
			break;
	}
	while(continuar != 1 and continuar != 2){
		cout<<"¿Desea pedir algo más? Si=1;No=2"<<endl;
		cin>>continuar;
	}
	if(continuar==1){
		continuar=0;
		menu();
	}
	else{
		cout<<"\n*** PEDIDO *** "<<endl<<endl;
 
		if(acum_op1 >0 ){
			cout<<acum_op1<<" Sopa"<<endl<<endl;
		}
		if(acum_op2 >0 ){
			cout<<acum_op2<<" Arroz con Pollo"<<endl<<endl;
		}
		if(acum_op3 >0 ){
			cout<<acum_op3<<" Pasta Boloñesa"<<endl<<endl;
		}
		if(acum_op4 >0 ){
			cout<<acum_op4<<" OFERTA Especial (1 bebida y Arroz con Salsa)"<<endl<<endl;
		}
		if(acum_op5 >0 ){
			cout<<acum_op5<<" Jarra de Jugo"<<endl<<endl;
		}
		if(acum_op6 >0 ){
			cout<<acum_op6<<" Vaso de Jugo"<<endl<<endl;
		}
		if(acum_op7 >0 ){
			cout<<acum_op7<<" Jarra de Agua"<<endl<<endl;
		}
		if(acum_op8 >0 ){
			cout<<acum_op8<<" Vaso de Agua"<<endl<<endl;
		}
		if(acum_op9 >0 ){
			cout<<acum_op9<<" Torta"<<endl<<endl;
		}
		if(acum_op10 >0 ){
			cout<<acum_op10<<" Helado"<<endl<<endl;
		}
		if(acum_op11 >0 ){
			cout<<acum_op11<<" OFERTA ESPECIAL (1 cafe y galletas dulces)"<<endl<<endl;
		}
		cout<<"Total a Pagar:  "<<total<<endl<<endl;
	}
}
 
int main(int argc, char *argv[]) {
	cout<<"*** BIENVENIDOS AL RESTAURANTE BONIN ***"<<endl<<endl<<endl;
	cout<<"  ** ¿ QUE DESEA ORDENAR ? ** "<<endl<<endl;
	menu();
	return 0;
Last edited on
Did you know you can putcin >> variable into the condition of a while loop?

1
2
3
while(cin >> variable) {
    // If we're here, we read into variable successfully. Do something with it!
}


It's a handy little pattern. However, here, you want to do something repeatedly so long as the read failed. That would look more like:

1
2
3
4
while( !( cin >> variable ) ) {
    //TODO: Print a message complaining about the input.
    cin.clear(); //Reset the error flags for cin so that we can try again.
}


You have a lot of cin >> someinteger lines, so you might want to consider writing a function that returns an int and does that whole while loop for you.

Good luck!

-Albatross
thanks for the reply. and if I deal with the ctype.h library (isalpha isdigit) how would it be? to validate the options and the menu in this case.
Topic archived. No new replies allowed.