Public Group

# Help!

This topic is 2902 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi,

I am new to the developing community and I was writing a small app for my french class and I need some help. I just can't figure it out. I want you to be able to select the menu and I can't get the syntax right. Tips on developing also welcome!

 #include <iostream.h> using namespace std; int a; int main(){ int b; cout << "Welcome to the interactive Cafe Les Cinq E'Toiles Menu. Please Select 1 for Bevrages, 2 for Entrees, and 3 for Dessert."; cin >> a; if (a = 1) cout << "Bevrage Menu. Cafe de Lait: 2€50 Cafe de Creme: 3€ "; cin >> b; else if (a = 2) cout << "Entree Menu. "; cin >> b; else if (a = 3) cout << "Dessert Menu. "; cin>> b; system("pause"); } 
[size="1"]Poll removed, please see the forum FAQ. -- jpetrie

##### Share on other sites
Well, your main problems lies in the fact that you use = in trying to compare values (= stands for assignment; you must use == for "equals" evaluation) and lack of {} to denote the bodies of the if statements. When you write an if statement without { } denoting the beginning and ending of it's body, it will only apply to the statement that follows immediately after it while the rest will be unaffected. For example, suppose you have the following

 if(a == 1) someStatement(); someOtherStatement(); 

in this case, someStatement() will be executed only if a equals 1, but someOtherStatement() will always be executed. In order to encompass more than two statements, you need to surround the body with {} like so

 if(a == 1) { someStatement(); someOtherStatement(); } 

in which case, both statements will execute only if a equals 1.

With both mistakes, I take it every statement executes, since a = 1, a = 2, and a = 3 will always result in a non-zero value, which is synonymous to "true" in C++.

##### Share on other sites

Hi,

I am new to the developing community and I was writing a small app for my french class and I need some help. I just can't figure it out. I want you to be able to select the menu and I can't get the syntax right. Tips on developing also welcome!

 #include <iostream.h> using namespace std; int a; int main(){ int b; cout << "Welcome to the interactive Cafe Les Cinq E'Toiles Menu. Please Select 1 for Bevrages, 2 for Entrees, and 3 for Dessert."; cin >> a; if (a = 1) cout << "Bevrage Menu. Cafe de Lait: 2€50 Cafe de Creme: 3€ "; cin >> b; else if (a = 2) cout << "Entree Menu. "; cin >> b; else if (a = 3) cout << "Dessert Menu. "; cin>> b; system("pause"); } 
[size="1"]Poll removed, please see the forum FAQ. -- jpetrie

Ah the infamous if ( var = val) bug.. *** Programmer achievement *** (only 5 points) I've spent many hours hunting these down. And they get harder to find the longer you look for it. Because in your head there is nothing logically wrong saying "If var equals val then...." So you tend to miss it. Gratz on that it's a programmers right of passage.

Programming tip:
You may want to consider a switch case statement instead of if(a) / else /if(a) / else. To be clear I am not saying you are doing anything wrong the way you are coding it. It just may be a bit cleaner using switch();

Keep coding!

##### Share on other sites

Well, your main problems lies in the fact that you use = in trying to compare values (= stands for assignment; you must use == for "equals" evaluation) and lack of {} to denote the bodies of the if statements. When you write an if statement without { } denoting the beginning and ending of it's body, it will only apply to the statement that follows immediately after it while the rest will be unaffected. For example, suppose you have the following

 if(a == 1) someStatement(); someOtherStatement(); 

in this case, someStatement() will be executed only if a equals 1, but someOtherStatement() will always be executed. In order to encompass more than two statements, you need to surround the body with {} like so

 if(a == 1) { someStatement(); someOtherStatement(); } 

in which case, both statements will execute only if a equals 1.

With both mistakes, I take it every statement executes, since a = 1, a = 2, and a = 3 will always result in a non-zero value, which is synonymous to "true" in C++.

[quote name='daliberator' timestamp='1320893053' post='4882378']
Hi,

I am new to the developing community and I was writing a small app for my french class and I need some help. I just can't figure it out. I want you to be able to select the menu and I can't get the syntax right. Tips on developing also welcome!

 #include <iostream.h> using namespace std; int a; int main(){ int b; cout << "Welcome to the interactive Cafe Les Cinq E'Toiles Menu. Please Select 1 for Bevrages, 2 for Entrees, and 3 for Dessert."; cin >> a; if (a = 1) cout << "Bevrage Menu. Cafe de Lait: 2€50 Cafe de Creme: 3€ "; cin >> b; else if (a = 2) cout << "Entree Menu. "; cin >> b; else if (a = 3) cout << "Dessert Menu. "; cin>> b; system("pause"); } 
[size="1"]Poll removed, please see the forum FAQ. -- jpetrie

Ah the infamous if ( var = val) bug.. *** Programmer achievement *** (only 5 points) I've spent many hours hunting these down. And they get harder to find the longer you look for it. Because in your head there is nothing logically wrong saying "If var equals val then...." So you tend to miss it. Gratz on that it's a programmers right of passage.

Programming tip:
You may want to consider a switch case statement instead of if(a) / else /if(a) / else. To be clear I am not saying you are doing anything wrong the way you are coding it. It just may be a bit cleaner using switch();

Keep coding!
[/quote]

Thank you both so much! I fixed it and am so happy! I couldn't have done it without you!

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 9
• 11
• 15
• 21
• 26