losing my mind with switch statements

I'm writing a program in class. You're supposed to write a program that prints out pet insurance fee

Write a program that prints the insurance fee to pay for a pet according to the following rules:
(Note: must use a switch statement to determine pet fee)
A dog that has been neutered costs $50.
A dog that has not been neutered costs $80.
A cat that has been neutered costs $40.
A cat that has not been neutered costs $60.
A bird or reptile costs nothing.
Any other animal generates an error message.

The program should prompt the user for the appropriate information: a character code for the pet type, and a yes/no response for the neutered status. Use a code letter to determine the kind of animal (i.e. D or d represents a dog, C or c represents a cat, B or b represents a bird, R or r represents a reptile, and anything else represents some other kind of animal). Use a code letter to determine the neutered status(i.e. Y or y represents yes, N or n represents no). The user should be allowed to enter the input in either upper or lower case.
It prints out the type of animal (full name of animal) and the insurance fee. Any error in input data should generate an error message “Invalid data – no fee calculated”

NOTE: You must use a switch statement to determine the pet fee.)

I am not sure if I need to use if else statements before using switch statements or if those are completely unnecessary when using a switch.
Also, I don't know how to have it test in the next case for "n" for the char "neutered" because it tells me I have already used "d".
this is what I have so far...

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

using namespace std;

int main()
{
	char animal, neutered;
	const int dogcost = 80, catcost = 60, birdOrReptilecost = 0;

	//Take inputs
	cout << "Pet Insurance Program\n\n";
	cout << "Please enter the first letter of the type of animal you would like to insure\n";
	cout << fixed << setw(30) << "Dog\n" << setw(30) << "Cat\n" << setw(30) << "Bird\n" << setw(31) << "Reptile\n\n";
	cout << "Enter the kind of your animal: ";
	cin >> animal;
	cout << "Is your pet neutered? ('y' for yes 'n' for no) ";
	cin >> neutered;

	switch (animal && neutered)
	{ 
	case 'd' || 'D' && 'y': cout << "the fee to insure your neutered dog is $" << dogcost - 30 << endl;
		break;
	}
	return 0;
Last edited on
Because you're treating switch/case as if they were if statements.

1
2
3
4
switch ( constant ) {
  case constant:
    break;
}



Eg.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
switch ( animal ) {
  case 'D':
    case ( neutered ) {
      case 'Y':
      case 'y': // you can do this, or just use toupper() on the variables beforehand.
        break;
      case 'N':
        break;
    }
    break;
  case 'C':
    break;
  case 'B':
    break;
  case 'R':
    break;
}


Beware that nested case statements look ugly real quickly.


> cin >> animal;
Input into chars can be tricky, you might want to look into cin.flush() as well.
Last edited on
sorry I am pretty new to all of this. should I be using a different variable identifier than 'char' for animals and neutered?
Is it possible for you to go into a little more detail with your example?
I did try and change it using your example but its giving me an error in this area
case (neutered) { "expression must have a constant value"
also getting an error for the bracket at the end of that statement^
1
2
3
4
5
6
7
8
9
10
11
12
13
	switch (animal) {
	case 'D' && 'd':
		case (neutered) {
			case 'y':
			case 'Y':
			cout << "the fee to insure your neutered dog is $" << dogcost - 30 << endl;
			break;
			case 'n':
			case 'N':
			cout << "the fee to insure your non - neutered dog it $" << dogcost << endl;
			break;
		}
	}


im not sure what cin.flush() is. i know if i used it i would lose points because it is something we have not gone over.
Last edited on
> case 'D' && 'd':
Is NOT the same as
case 'D':
case 'd':

Remember, NO expressions in switch / case.

> im not sure what cin.flush() is.
It removes unwanted chars from the input stream.

1
2
3
char animal, neutered;
cin >> animal;
cin >> neutered;

You're going to be typing in something like
D
N

As written, you will find that neutered contains the newline character, not the N character.
You need to have a way to remove that newline, so that neutered becomes the Y or N you intend.
Last edited on
not sure what I can try. I attempted to use if else statements before my switch statements but it turned into a pretty big mess and not working

I tried using getchar() but it's telling me "there are too many arguments in function to call"
also tried cin.get(), not working either.

would using a for loop work in this scenario?
Last edited on
It's hard to say.
So many questions without the code.
ok so i have figured out a little more and got the code working a little, but i messed it up trying to get it to switch to cat and now it stopped and i have no idea where its wrong. it stops running after asking user if pet is neutered.

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

using namespace std;

int main()
{
	char animal, neutered;
	const int dogcost = 80, catcost = 60, birdOrReptilecost = 0;

	//Take inputs
	cout << "Pet Insurance Program\n\n";
	cout << "Please enter the first letter of the type of animal you would like to insure\n";
	cout << fixed << setw(30) << "Dog\n" << setw(30) << "Cat\n" << setw(30) << "Bird\n" << setw(31) << "Reptile\n\n";
	cout << "Enter the kind of your animal: ";
	cin >> animal;


	switch (animal) {
	case 'D':
	case 'd':
	case 'c':
	case 'C':
		cout << "Is your pet neutered? ";
		cin >> neutered;
		break;

		switch (neutered) 
		case 'y':
		case 'Y':
			cout << "the fee to insure your neutered dog is $" << dogcost - 30 << endl;
			break;
		case 'n':
		case 'N':
			cout << "the fee to insure your non - neutered dog is $" << dogcost << endl;
			break;
		
	case 'r':
	case 'R':
		cout << "There is no cost to insure your reptile\n";
		break;
	case 'b':
	case 'B':
		cout << "There is no cost to insure your bird\n";

	}
	return 0;
}
Last edited on
follow the logic...
looks like to ask if its neutered, you had to type c or d. If you typed c or d, you didn't type r or b. So, you typed c or d an got the neutered stuff and … its done, return 0.

So, having said that, its time to ask the question.. what does it NOT do that you wanted it to do? It looks to need a default: 'no such animal' case. You need an 'invalid input' as well, read your problem description and make sure you covered all the needs.

do you want to loop and keep asking the user stuff about a bunch of pets? (this is unclear, trying to follow your posts).
Last edited on
yeah, I haven't gotten to the default yet because I wanted to get everything else working.

so here is where I am stuck now..
this is when implementing all variables besides default. They all work correctly, BUT when i enter variables for cat, reptile, or bird i get run time check errors.
(i tried using a different variable for cat neutered and thought it would fix error but it did not.)

>as far as loops. I was thinking about using that but since our professor told us to use switch statements I didn't know if I can use a loop.

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

using namespace std;

int main()
{
	char animal, neutered, neuteredcat;
	const int dogcost = 80, catcost = 60, birdOrReptilecost = 0;

	//Take inputs
	cout << "Pet Insurance Program\n\n";
	cout << "Please enter the first letter of the type of animal you would like to insure\n";
	cout << fixed << setw(30) << "Dog\n" << setw(30) << "Cat\n" << setw(30) << "Bird\n" << setw(31) << "Reptile\n\n";
	cout << "Enter the kind of your animal: ";
	cin >> animal;


	switch (animal) {
	case 'D':
	case 'd':
		cout << "Is your pet neutered? ";
		cin >> neutered;
		break;

	}
	switch (neutered) {
	case 'y':
	case 'Y':
		cout << "the fee to insure your neutered dog is $" << dogcost - 30 << endl;
		break;
	case 'n':
	case 'N':
		cout << "the fee to insure your non - neutered dog is $" << dogcost << endl;
		break;
	}

	switch (animal) {
	case 'c':
	case 'C':
		cout << "Is your pet neutered?";
		cin >> neuteredcat;
		break;
	}
		switch (neuteredcat) {
		case 'y':
		case 'Y':
			cout << "the fee to insure your neutered cat is $" << catcost - 20 << endl;
			break;
		case 'n':
		case 'N':
			cout << "the fee to insure your non - neutered cat is $" << catcost << endl;
			break;
		}
		switch (animal) {
		case 'r':
		case 'R':
			cout << "There is no cost to insure your reptile\n";
			break;
		case 'b':
		case 'B':
			cout << "There is no cost to insure your bird\n";
			break;
		}
		return 0;
}

Last edited on
ok, you have put in a solid try and this is subtle... until you have seen it, its not something you would pick up on right away. I have boiled your program down to a bare bones example, with some lookup tables to make the text processing easier than dozens of hard coded items, and ordered the logic so that the double switch will go down the paths correctly. This is the key part... order of conditions matter if you want to keep it simple.

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

int main()
{   
  string s[] = {"cat ", "dog ", "bird ", "rept "};
  int cost[] = {10,20,30,40};
  char lookup[256] = {0};
  lookup['c'] = 0;
  lookup['d'] = 1;
  lookup['b'] = 2;
  lookup['r'] = 3;
  char c, n;
  cin >> c;
  switch(c)
  {
   case 'r':
   case 'b':
		cout << "your normal " << s[lookup[tolower(c)]] << "costs " << cost[lookup[tolower(c)]] << endl;
		break;
	case 'c':  
	case 'C':
	case 'd':
	case 'D':
	   cout << "is n \n";
	   cin >> n;
	   switch(n)
	   {
	    case 'y':
		case 'Y':
		cout << "your neut" << s[lookup[tolower(c)]] << "costs " << cost[lookup[tolower(c)]]+5 << endl;
		break;	
		case 'n':
		case'N':
				cout << "your normal " << s[lookup[tolower(c)]] << "costs " << cost[lookup[tolower(c)]] << endl;
		break;
        default: ;		
	   }
		break;
  	default: cout << "default\n";
  }  
}


if you don't like tolower you can set the caps versions of the table to the same index values as the lower ones.

see if you can do it now. You can arrange what you had to do the logic in the double switches with hard coded strings if you don't want to do the fill-in-the-blank string stuff. Forgive the shortened text & variables... its just meant as a quick example to steer you.

at some point the logical spaghetti becomes unmanageable. If you had a decision tree 4+ deep to do here, with 10 or so species... it would be a nightmare and you would need to break the problem up differently (the approach works but the result is incomprehensible).
Last edited on
Topic archived. No new replies allowed.