繁体   English   中英

C ++中While循环的困难

[英]Difficulty with While loop in C++

我在执行C ++作业时遇到困难。 第一个问题是在循环的结尾(答案!=“是” &&客户<= 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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM