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;
}
}
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!