gpt4 book ai didi

c++ - C++ 中 While 循环的困难

转载 作者:行者123 更新时间:2023-11-28 04:56:58 24 4
gpt4 key购买 nike

我的 C++ 作业有困难。第一个问题是在循环结束时 (answer != "yes"&& customers <= 5),输出不起作用,因为它给出了两个条件。第二个问题是代码太复杂,需要简化(有什么建议)?

代码:

#include <iostream> // Access output and input stream
#include <string> // Access string variable

using namespace std;

int main() {
int characters, food, movie, customers=0; //Declare variables
char gender; //Declare variables
string name, answer; //Declare variables



cout <<"Is there a customer? <enter yes if there is and anything else to end program>"<<endl;
cin>>answer;

while (answer == "yes")
{
customers++;
cout <<"\nWhat is your name dear? ";
cin >> name;
cout <<"Well "<<name<<", welcome to my salon!\n";
cout <<"I will ask you a few questions and your answers will determine which haircut I will give you.\nPlease enter your choice by using the character between < > next to your choice.\n\n";
cout <<"Are you <m>ale or <f>emale? ";
cin >>gender;

if (gender == 'm')
{
cout <<"Are you a Super Hero<0> or a Super Villain<1>? ";
cin >>characters;
if (characters == 1)
{cout <<name <<", You should get a mohawk.\n";}
else if (characters == 0)
{
cout <<"OK "<<name<<", do you prefer steak<0> or sushi<1>? ";
cin >>food;
if (food == 0)
cout <<name<<", You should get a flat top.\n";
else if (food == 1)
cout <<name<<", You should get a pompadour.\n";
}
cout <<"Hope you like it!!\n------------\n";

}
else if (gender == 'f')
{
cout <<"Are you a Super Hero<0> or a Super Villain<1>? ";
cin >>characters;
if (characters == 1)
{cout <<name <<", You should get a mohawk.\n";}
else if (characters == 0)
{
cout <<"OK "<<name<<", do you prefer anime<0> or sitcom<1>? ";
cin >>movie;
if (movie == 0)
cout <<name<<", You should go with bangs.\n";
else if (movie == 1)
cout <<name<<", You should go with feathered.\n";
}
cout <<"Hope you like it!!\n------------\n";
}
cout <<"Any other customers? <enter yes if there are and anything else if I am done for the day> "<<endl;
cin >>answer;
if (answer != "yes" && customers >= 5)
cout<<"\nWell that was a good day! I had " <<customers<<" customer<s> today. Tomorrow is another day ..."<< endl;
else if (answer != "yes" && customers < 5)
cout<<"\nWell that was a poor day! I had " <<customers<<" customer<s> today. Tomorrow is another day ..."<< endl;
}
cout<<"\nWell that was a poor day! I had " <<customers<<" customer<s> today. Tomorrow is another day ..."<< endl;
return 0;
}

最佳答案

将 if else 条件放在 while 循环之外并删除 cout<<"\nWell that was a poor day! I had " <<customers<<" customer<s> today. Tomorrow is another day ..."<< endl;在你的 while 循环之外。

while (answer == "yes")
{
...
}
if (customers >= 5)
cout<<"\nWell that was a good day! I had "
<<customers
<<" customer<s> today. Tomorrow is another day ..."
<< endl;
else
cout<<"\nWell that was a poor day! I had "
<<customers
<<" customer<s> today. Tomorrow is another day ..."
<< endl;

关于c++ - C++ 中 While 循环的困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46944341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com