Animal insurance and functions...........

Hello all!

Am pretty new to c++ but very new to functions. Found an exercise online about animal insurance and thought it sounded like a good place to start with 'switch' and 'function'. Am very happy as the basics of my function seem to work well - namely the passing of the value. The problem i am having is with the return. What i want to happen is that entering a 'y' to the response "do you have another animal to insure?" will prompt the whole of main to repeat.

Here is the code so far: Any hints, help much 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
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
#include <iostream>
#include <cstdlib>

using namespace std;

char func (int n)
{
char yes;
cout << "The fee for insurance will be €" << n << endl;
cout << "Do you have another animal to insure?" << endl;
cin >> yes;
if (yes == 'y')
return yes;
}

int main ()
{
char yes = 'y';
char animal;
char neut;

while (yes == 'y')
{
cout << "Please select your animal type\n";
cin >> animal;

if (animal != 'D' || 'd' || 'C' || 'c' || 'R' || 'r' || 'B' || 'b')
 	cout << "Sorry, your animal was not recognised.\n";

 	
switch (animal)
{
case 'D':
		cout << "Has your dog been neutered?\n";
		cin >> neut;
			if (neut == 'y')
			func (50);
				else if (neut == 'n')
				func (80);
case 'd':
		cout << "Has your dog been neutered?\n";
		cin >> neut;
			if (neut == 'y')
			func (50);
				else if (neut == 'n')
				func (80);
case 'C':
		cout << "Has your cat been neutered?\n";
		cin >> neut;
			if (neut == 'y')
			func (40);
				else if (neut == 'n')
				func (60);
case 'c':
		cout << "Has your cat been neutered?\n";
		cin >> neut;
			if (neut == 'y')
			func (40);
				else if (neut == 'n')
				func (60);
case 'R':
		cout << "These animals cost nothing to insure.\n";
case 'r':
		cout << "These animals cost nothing to insure.\n";
}
}


 return 0;
 }
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
switch (animal)
{
case 'D':
		cout << "Has your dog been neutered?\n";
		cin >> neut;
			if (neut == 'y')
			func (50);
				else if (neut == 'n')
				func (80);
case 'd':
		cout << "Has your dog been neutered?\n";
		cin >> neut;
			if (neut == 'y')
			func (50);
				else if (neut == 'n')
				func (80);
case 'C':
		cout << "Has your cat been neutered?\n";
		cin >> neut;
			if (neut == 'y')
			func (40);
				else if (neut == 'n')
				func (60);
case 'c':
		cout << "Has your cat been neutered?\n";
		cin >> neut;
			if (neut == 'y')
			func (40);
				else if (neut == 'n')
				func (60);
case 'R':
		cout << "These animals cost nothing to insure.\n";
case 'r':
		cout << "These animals cost nothing to insure.\n";
}
}

you shall write this block as ;
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
switch (animal)
{
case 'D':
case 'd':
		cout << "Has your dog been neutered?\n";
		cin >> neut;
			if (neut == 'y')
			func (50);
				else if (neut == 'n')
				func (80);
               break;
case 'C':
case 'c':
		cout << "Has your cat been neutered?\n";
		cin >> neut;
			if (neut == 'y')
			func (40);
				else if (neut == 'n')
				func (60);
               break;
case 'R':		
case 'r':
		cout << "These animals cost nothing to insure.\n";
               break;
}
}
line 27 is wrong:
27
28
if (animal != 'D' || 'd' || 'C' || 'c' || 'R' || 'r' || 'B' || 'b')
 	cout << "Sorry, your animal was not recognised.\n";
should be
27
28
if (animal != 'D' && animal != 'd' && animal != 'C' && animal != 'c' && animal != 'R' && animal != 'r' && animal != 'B' && animal != 'b')
 	cout << "Sorry, your animal was not recognised.\n";
or you can put this in the default label inside the switch

BTW, if you want to make case insensitive comparison, it's easier if you use tolower or toupper
http://www.cplusplus.com/reference/clibrary/cctype/toupper/
Last edited on
Thanks very much for the assistance, both pieces of advice really helped.
Here is the code as it stands. Could look a lot nicer - but that's not the point at present is it?

Am working on the whole 'tolower' and 'toupper' functions - not got them to work yet, will persevere. Thanks guys,

Dan.

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
#include <iostream>
#include <cstdlib>

using namespace std;

char func (int n)
{
char yes;
cout << "The fee for insurance will be £" << n << endl;
cout << "Do you have another animal to insure?" << endl;
cin >> yes;
return yes;
}

int main ()
{
char yes = 'y';
char animal;
char neut;

while (yes == 'y')
{
cout << "Please select your animal type\n";
cin >> animal;
 	
switch (animal)
{
case 'D':
case 'd':
		cout << "Has your dog been neutered?\n";
		cin >> neut;
			if (neut == 'y')
			yes=func (50);
				else if (neut == 'n')
				yes=func (80);
				break;
case 'C':
case 'c':
		cout << "Has your cat been neutered?\n";
		cin >> neut;
			if (neut == 'y')
			yes=func (40);
				else if (neut == 'n')
				yes=func (60);
				break;
case 'R':
case 'r':
case 'B':
case 'b':
		cout << "These animals cost nothing to insure.\n";
		break;
default:
		cout << "Sorry, your animal was not recognised.\n";
		break;
}
}

 return 0;
 }


Topic archived. No new replies allowed.