Skip to main content
gamedev.net
🔒 Locked

Help!

Started by HenryO'Callaghan Nov 10, 2011 at 2:44 AM 2 replies 1.1k views
Original Post
HenryO'Callaghan
HenryO'Callaghan
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
boogyman19946
boogyman19946
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++.
Yo dawg, don't even trip.
Okidata30
Okidata30

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!
I always seem to solve my bugs on the drive back to work the following day! So why wont my boss let me go home now? Twitter_Follow
HenryO'Callaghan
HenryO'Callaghan

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!


Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.