20.2k views
5 votes
Currently when an animal other than a cat or dog is entered (such as a hamster), the program asks if it is spayed or neutered before displaying the message that only cats and dogs need pet tags. Find a way to make the program only execute the spay/neuter prompt and input when the pet type is cat or dog.

User Ziyang Liu
by
6.0k points

1 Answer

3 votes

Answer:

int main() {

string pet; //stores the pet string which is cat or dog

char spayed; // stores the choice from y or n

cout << "Enter the pet type (cat or dog): "; //prompts user to enter pet type

cin >> pet; //reads the input from user

if(pet=="cat"|| pet=="dog") { // if user enters cat or dog

cout << "Has the pet been spayed or neutered (y/n)? ";

//asks user about pet been spayed of neutered

cin >> spayed;} //reads y or n input by the user

else // else part will execute if user enters anything other than cat or dog

cout<<"only cats and dogs need pet tags";

// displays this message if input value of pet is other than cat or dog

Step-by-step explanation:

The answer to the complete question is provided below:

#include <string>

using namespace std;

int main() {

string pet; // "cat" or "dog"

char spayed; // 'y' or 'n'

// Get pet type and spaying information

cout << "Enter the pet type (cat or dog): ";

cin >> pet;

if(pet=="cat"|| pet=="dog") {

cout << "Has the pet been spayed or neutered (y/n)? ";

cin >> spayed;}

else

cout<<"only cats and dogs need pet tags";

// Determine the pet tag fee

if (pet == "cat")

if (spayed == 'y'

else if (pet == "dog")

spayed=='Y')

cout << "Fee is $6.00 \\";

else

cout << "Fee is $12.00 \\";

return 0; }

According to part a) of this question the OR operator is used in this statement if (spayed == 'y' || spayed=='Y') so that if the user enters capital Y or small y letter both are acceptable.

According to the part b) if(pet=="cat"|| pet=="dog") statement is used to make program only execute spay/neuter prompt and input when the pet type is cat or dog otherwise the program displays the message: only cats and dogs need pet tags.

Currently when an animal other than a cat or dog is entered (such as a hamster), the-example-1
User Dmitro
by
5.8k points