C++ if/else

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • suemez
    New Member
    • Feb 2008
    • 2

    C++ if/else

    I am trying to do an if/else problem.

    User is asked to input if they have a meal plan.

    User inputs a string "yes" or "no"

    My problem is running the if/else to later calculate the cost of a meal plan if the answer yes = true.

    I have this so far:
    [CODE=cpp]const double MEAL_PLAN = 1245.00;

    int num1 = 0;

    string str1 = "yes"
    string str2 = "no"

    cout << " Please enter yes or no for meal plan: ";
    cin >> str1, str2;

    if ( str1 != str2){
    meal_plan = true;
    }
    else
    {
    meal_plan = false;
    }
    cout << endl;


    cout << "Meal Plan: " ;
    if (meal_plan == true){
    MEAL_PLAN; // to return cost of meal plan
    }
    else
    {
    num1; // to return 0
    }[/CODE]

    I am not sure if I can use the string in this. I am not getting it to calculate. I have tried using a different variable in cin, and still cannot get it to work.
    Any ideas?

    Thank you,

    Suemez
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    Your code is utterly broken. As a side note, when posting code here, please use CODE tags so that it is actually readable.

    ---
    string str1 = "yes"
    string str2 = "no"
    ---
    Won't compile because of missing semicolons. Don't be careless about them.

    ---
    cout << " Please enter yes or no for meal plan: ";
    cin >> str1, str2;
    ---
    Explain that to me. Explain how that syntax translates to logic.

    ---if ( str1 != str2){
    meal_plan = true;
    }
    ---
    meal_plan is not defined anywhere. Won't compile. Why compare str1 to str2?

    ---if (meal_plan == true){
    MEAL_PLAN; // to return cost of meal plan
    }
    ---
    What do you mean "return" cost of meal plan?

    ---else
    {
    num1; // to return 0
    }
    ---
    Uh, if you want to return 0, return 0. Don't create a variable to stand in for 0, and then return that. Not unless that variable not being 0 is a plausible scenario.

    Your code is utterly broken. You may as well post it in its entirety here (but only your original broken version). Also, as a side note, where are you learning C++ from? The code you wrote is atrocious and some of it is meaningless nonsense. While as a beginner, your mistakes are forgiven, some of the code you write is very awkward in that the logic it tries to represent is meaningless here. Seriously, where are you learning C++ from?

    Comment

    • suemez
      New Member
      • Feb 2008
      • 2

      #3
      I had written my code in quickly and apologize for the scramble. Next time I will copy and paste. I appreciate your help and thoughtful words. I have figured out the answer.

      Comment

      Working...