switch going through every case, even with return 0;

Ok so I have animal set as a char that the user inputs, and petWeight set as an int that the user also inputs. If the petWeight is above what is specified in the case I want the program to terminate, or if you think better, bo back and make them input a weight in range. I made each case to where if the weight was too large return 0; would execute and terminate the program so they'de have to run it again and put a weight that is in range.

When I run the program, I type "D" and type 120 for the petWeight. It goes through case 'D','d', and then hits case 'C','c' and terminates the program because petWeight is above 30. Why is it going through case 'C','c' if I didn't insert "C" or "c"?

Thanks ahead of time for your help!

char animal;
string petType;
string animalSize;
int petWeight;

// Prompting user for animal type
cout << "Welcome to We Fix Broken Pets!\n";
cout << "What type of animal do you have? \n
Type D for dog, C for cat, or H for horse: ";
cin >> animal;

//Assiging user input to type of pet
if (animal == 'D' || animal == 'd')
petType = "dog";
else if (animal == 'C' || animal == 'c')
petType = "cat";
else if (animal == 'H' || animal == 'h')
petType = "horse";
else{
cout << "That was not a valid choice, choices must be C,
D,or H.\nPlease run the program again.\n";
system("pause");
return 0;
}


//Prompting user for the weight of the animal
cout << "Please enter the weight of the " << petType << ": ";
cin >> petWeight;


//Testing for valid weight of the specific pet type is not above limit
switch(animal)
{
case 'D':
case 'd': if(petWeight >= 200)
{
cout << "Sorry your " << petType << " is above the weight limit, please see another clinic!\n";
system("pause");
return 0;
}
case 'C':
case 'c': if(petWeight >= 30)
{
cout << "Sorry your " << petType << " is above the weight limit, please see another clinic!\n";
system("pause");
return 0;
}
case 'H':
case 'h': if(petWeight >= 3500)
{
cout << "Sorry your " << petType << " is above the weight limit, please see another clinic!\n";
system("pause");
return 0;
}
}
Why is it going through case 'C','c' if I didn't insert "C" or "c"?

For the same reason that it goes through to case 'd': if you enter 'D'.

You need a break;
http://www.cplusplus.com/doc/tutorial/control/#switch
Last edited on
a case statement should look like this:
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
switch (letter)
{
case 'A' : cout << "Alpha";
break;
case 'B' : cout << "Bravo";
break;
case 'C' : cout << "Charlie";
break;
case 'D' : cout << "Delta";
break;
case 'E' : cout << "Echo";
break;
case 'F' : cout << "Foxtrot";
break;
case 'G' : cout << "Golf";
break;
case 'H' : cout << "Hotel";
break;
case 'I' : cout << "India";
break;
case 'J' : cout << "Juliet";
break;
case 'K' : cout << "Kilo";
break;
case 'L' : cout << "Lima";
break;
case 'M' : cout << "Mike";
break;
case 'N' : cout << "November";
break;
case 'O' : cout << "Oscar";
break;
case 'P' : cout << "Papa";
break;
case 'Q' : cout << "Quebec";
break;
case 'R' : cout << "Romeo";
break;
case 'S' : cout << "Sierra";
break;
case 'T' : cout << "Tango";
break;
case 'U' : cout << "Unifor";
break;
case 'V' : cout << "Victor";
break;
case 'W' : cout << "Whiskey";
break;
case 'X' : cout << "X-ray";
break;
case 'Y' : cout << "Yankee";
break;
case 'Z' : cout << "Zulu";
}

return 0;
}

you need a break after every statement so the program knows to leave after it reaches that statement.
Oh wow I figured it out!

case 'D':
case 'd': if(petWeight >= 200)
{
cout << "Sorry your " << petType << " is above the weight limit, please see another clinic!\n";
system("pause");
return 0;
}
break;

I had the break inside the if statement the first time haha! Thank you all for the help!
Last edited on
Topic archived. No new replies allowed.