24,593 views
12 votes
12 votes
Write a loop that continually asks the user what pets the user has until the user enters stop, in which case the loop ends. It should acknowledge the user in the following format. For the first pet, it should say You have one cat. Total # of Pets: 1 if they enter cat, and so on.

Sample Run


What pet do you have? lemur
What pet do you have? parrot
What pet do you have? cat
What pet do you have? stop

Sample Output

What pet do you have? lemur
You have one lemur. Total # of Pets: 1
What pet do you have? parrot
You have one parrot. Total # of Pets: 2
What pet do you have? cat
You have one cat. Total # of Pets: 3
What pet do you have? stop

User Mickalot
by
2.6k points

1 Answer

25 votes
25 votes

Answer:

#include <iostream>

using namespace std;

int main() {

/*

What pet do you have? lemur

parrot

What pet do you have? cat

What pet do you have? stop

*/

string pet;

int num=1;

while (pet!="stop"){

cin>>pet;

cout<<"What pet do you have?";

if (pet!="stop"){

cout<<"you have one "<< pet<<" total numof # is "<<num++<<endl;

}

else{

cout<<"stop";

}

}

}

Step-by-step explanation:

you did not specify the language so i went with c++

i used while loop which will keep asking the user for an input until he/she write stop in order to break the loop

first i declared the data type of the input

used it as a conditionin the while loop

then printed the input everytime the loop runs

User The Puma
by
3.0k points