Repeat Code

Hello. I'm having problems figuring out how to continuously ask the user to input age, if the age entered is below 0 or above 150 (line 16 to 24). Also if I want to add at the end of the program if the ticket price is equal to zero, the program will repeat asking the user to input information again.

Thanks, here is my code:

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

struct sPassenger {int age; bool student; bool disabled; int from; int to;};
int main()
{
	int age; bool student; bool disabled; int from; int to; 

	cout << "Please Enter Your Age (0 - 150)" << endl;
	cin >> age;

	if (age < 0, age > 150)
	{
		do
		{
		cout << "Please Enter Your Age Again (0 - 150)" << endl;
		cin >> age;
		if ( age > 12 && age < 65) age = 0;
		else if ( age >= 65) age = 1;
		else if ( age <= 12) age = 2;
		}
		while (age < 0, age > 150);
	}

	else
	{
		if ( age > 12 && age < 65) age = 0;
		else if ( age >= 65) age = 1;
		else if ( age <= 12) age = 2;
	}

	cout << "Are you Disabled (Y/N)?" << endl;
	char answer1;
	cin >> answer1;
	switch( answer1 )
		{
		case 'Y': case 'y': 
		disabled = true;
		break;

		case 'N': case 'n':
		disabled = false;
		break;
		}

	cout << "Are you a Student (Y/N)?" << endl;
	char answer2;
	cin >> answer2;
	switch( answer2 )
		{
		case 'Y': case 'y': 
		student = true;
		break;

		case 'N': case 'n':
		student = false;
		break;
		}

	cout << "From (1)Taipei, (2)Taichung, (3)Tainan:" << endl;
	cin >> from;

	cout << "To (1)Taipei, (2)Taichung, (3)Tainan:" << endl;
	cin >> to;

double distance;
	if (from == 1 && to == 1) distance = 0;
	if (from == 1 && to == 2) distance = 165.0;
	if (from == 1 && to == 3) distance = 324.9;
	if (from == 2 && to == 2) distance = 0;
	if (from == 2 && to == 3) distance = 159.9;
	if (from == 2 && to == 1) distance = 165.0;
	if (from == 3 && to == 3) distance = 0;
	if (from == 3 && to == 2) distance = 159.9;
	if (from == 3 && to == 1) distance = 324.9;

double price;
	if (age == 0 && disabled == true && student == true) price = 2.8;
	else if (age == 1 && disabled == true && student == true) price = 1.7;
	else if (age == 2 && disabled == true && student == true) price = 1.8;
	else if (age == 0 && disabled == false && student == true) price = 3.6;
	else if (age == 1 && disabled == false && student == true) price = 2.9;
	else if (age == 2 && disabled == false && student == true) price = 1.9;
	else if (age == 0 && disabled == true && student == false) price = 3.3;
	else if (age == 1 && disabled == true && student == false) price = 2.0;
	else if (age == 2 && disabled == true && student == false) price = 2.2;
	else if (age == 0 && disabled == false && student == false) price = 5.6;
	else if (age == 1 && disabled == false && student == false) price = 3.7;
	else if (age == 2 && disabled == false && student == false) price = 3.2;

double ticketprice;
	ticketprice = price * distance;

printf ("Your Ticket Price is $ %.2f\n" , ticketprice);

return (0);
}
Last edited on
Line 24:
while (age < 0, age > 150);
should be:
while (age < 0 || age > 150);

Regarding:
if the ticket price is equal to zero, the program will repeat asking the user to input information again

You could just declare double ticketprice; at the beginning of main, then use a do{}while(ticketprice==0) loop around the rest of the code, in the same way as you did for checking the age.
Topic archived. No new replies allowed.