1 / 2100%
#include <iostream>
#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;
}
// Determine the pet tag fee
if (pet == "cat")
{ if (spayed == 'y' || spayed == 'Y')
cout << "Fee is $4.00 \n";
else
cout << "Fee is $8.00 \n";
}
else if (pet == "dog")
{ if (spayed == 'y' || spayed == 'Y')
cout << "Fee is $6.00 \n";
else
cout << "Fee is $12.00 \n";
}
else
cout << "Only cats and dogs need pet tags. \n";
return 0;
}
/*
Step 3: Improve the program in the following two ways.
Use a logical OR on lines 22 and 28 so that either a lowercase y or an
uppercase Y is
accepted.
if (pet == "cat")
{ if (spayed == 'y' || spayed == 'Y')
cout << "Fee is $4.00 \n";
else
cout << "Fee is $8.00 \n";
}
else if (pet == "dog")
{ if (spayed == 'y' || spayed == 'Y')
cout << "Fee is $6.00 \n";
else
cout << "Fee is $12.00 \n";
}
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.
cin >> pet;
if (pet == "cat" || pet == "dog")
{
cout << "Has the pet been spayed or neutered (y/n)? ";
cin >> spayed;
}
*/
Powered by TCPDF (www.tcpdf.org)
Students also viewed