473,320 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

C++ if/else

2
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:
Expand|Select|Wrap|Line Numbers
  1. const double MEAL_PLAN = 1245.00;
  2.  
  3. int num1 = 0;
  4.  
  5. string str1 = "yes"
  6. string str2 = "no"
  7.  
  8. cout << " Please enter yes or no for meal plan: ";
  9. cin >> str1, str2;
  10.  
  11. if ( str1 != str2){
  12.    meal_plan = true;
  13.    }
  14. else
  15.   {
  16.    meal_plan = false;
  17.   }
  18. cout << endl;
  19.  
  20.  
  21. cout << "Meal Plan: " ;
  22. if (meal_plan == true){
  23.   MEAL_PLAN;   // to return cost of meal plan
  24.  }
  25. else
  26. {
  27.   num1;       // to return 0
  28. }
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
Feb 13 '08 #1
2 2724
oler1s
671 Expert 512MB
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?
Feb 13 '08 #2
suemez
2
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.
Feb 13 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

33
by: Diez B. Roggisch | last post by:
Hi, today I rummaged through the language spec to see whats in the for ... else: for me. I was sort of disappointed to learn that the else clauses simply gets executed after the loop-body -...
6
by: Christian Seberino | last post by:
I am looking at the ELSE home page and trying to figure out if I should invest the time to learn about the ELSE minor mode for Emacs. Is there any programmer out there using ELSE that is getting...
27
by: Ron Adam | last post by:
There seems to be a fair amount of discussion concerning flow control enhancements lately. with, do and dowhile, case, etc... So here's my flow control suggestion. ;-) It occurred to me (a...
3
by: Patrice | last post by:
Hi, I need to do multi-conditional statements like below, but this error is displayed : Expected 'End' /myFilepath, line x else response.write(arrCorpo(sparam,sdiv)) end if I don't...
5
by: WindAndWaves | last post by:
Hi Team The function below searches all the tables in a database. However, if subsearch = true then it searches all the objects listed in a recordset (which are all table names). I thought to...
5
by: Brie_Manakul | last post by:
Is there a way in javascript to do an if else that shows a script in an iframe? Let me know if that doesn't make sense. We have a portal and in a portlet I need to grab these javascript links to...
4
by: Brie_Manakul | last post by:
I need to set up an if else to show different weather scripts based on the city selection they choose. Any help on this would be great. Thanks! <%@ page language="java" import="java.util.*,...
8
by: pelicanstuff | last post by:
Hi - Was wondering if anybody could tell me why this rather crappy code is giving me an 'Else without If' error on compile? All the Elses and Ifs look ok to me but there's a few. Private Sub...
23
by: bearophileHUGS | last post by:
So far in Python I've almost hated the 'else' of the 'for' loops: - I have problems to remember its meaning; - It gives me little problems when I later want to translate Python code to other...
17
by: JRough | last post by:
I'm trying to get error proof code. I have this code which seems to work but now I look at it I think it should be elseif not else and I wonder why it works. It is in the block:...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.