在C ++中使用While循环的难度

时间:2017-10-26 01:15:05

标签: c++

我的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;
}

1 个答案:

答案 0 :(得分:1)

将if else条件放在while循环之外,并在while循环外删除continueQuery = JOptionPane.showInputDialog("Another activity? (y/n)"); if ("y".equals(continueQuery)) { } else if ("n".equals(continueQuery)){ //i = i + 30; //break; }

cout<<"\nWell that was a poor day! I had " <<customers<<" customer<s> today. Tomorrow is another day ..."<< endl;