0

I have difficulty with my C++ assignment. The first problem is at the end of the loop with (answer != "yes" && customers <= 5), the output is not working, because it gives both condition. The second problem is that the code is too complex and need to be simplify (any suggestion)?

The code:

#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;
}
3
  • 1
    Stackoverflow is not an assignment solving service, unfortunately. Your first question could be looked into, but requesting how to simplify your code (without showing what you have attempted) is outside our scope. Commented Oct 26, 2017 at 1:25
  • first you should format your code correctly, and trim your program to go straight to the point. are you sure you can use != to compare the content of two strings ? Commented Oct 26, 2017 at 1:30
  • Adding to what @roelofs said, perhaps CodeReview would be a better place to ask for suggestions on how to simplify this. Commented Oct 26, 2017 at 3:34

1 Answer 1

1

Put the if else condition outside the while loop and remove cout<<"\nWell that was a poor day! I had " <<customers<<" customer<s> today. Tomorrow is another day ..."<< endl; outside your while loop.

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Just else would suffice.
@cantordust fixed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.