Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

if stateements in C


  • Please log in to reply

#1
BTPH

BTPH

    Member

  • Member
  • PipPipPip
  • 167 posts
hi everyone

basically im working on making a menu in C

Ive made the menu, it shows everything but now i need to get the user to select a function from the menu, i.e. pushing 1 will lead you to a new function called kitchen, 2 will lead you to lounge, etc. there are 7 options so ive used 7 if statements, but for some reason they arent working, no matter what i push they all go to a function called kitchen(), when i use a non numeric character the program just crashes,

below is the code for my main function, can someone see any problems?

int main(void)
{
float menu;
printf("Renovation Calculation \n");
printf("1. Calculate Kitchen \n");
printf("2. Calculate Lounge \n");
printf("3. Calculate Bedroom \n");
printf("4. Calculate Dining Room \n");
printf("5. Calculate Bathroom \n");
printf("6. Calculate Total \n");
printf("7. Exit \n");
scanf("%d", &menu);
if (menu=1)
kitchen();
{
if (menu=2)
lounge();
{
if (menu=3)
bedroom();
{
if (menu=4)
dining();
{
if (menu=5)
bathroom();
{
if (menu=6)
total();
{
if (menu=7)
fo();
else
{
printf("Sorry, you have entered an invallid character. Please enter a number between 1 and 7. \n");
main();
}
}
}
}
}
}
}
}

id appreciate ne help,

cheers,
ben
  • 0

Advertisements


#2
BuRnEr

BuRnEr

    Member

  • Member
  • PipPip
  • 48 posts
try to enter gt 7 and which statement get called see, i did not try it but i guess else part gets called, not guess i am sure else part gets called...
base on this try to fixe it if you still need hellp check about how to use "if and else" statements together...like "if, else if, else"

and you can also use switch statement...

take care
BuRnEr
  • 0

#3
bdlt

bdlt

    Member

  • Member
  • PipPipPip
  • 876 posts
great advice from BuRnEr.

once you get if-elseif/switch working there's more to do.

you may want to set aside an hour or 2 for reading your book/tutorial.
here are some other topics to read:

1. the difference between = and ==
2. scanf() - %d is for int, not float

for best results: get if-elseif/switch working first, then work on =/== and scanf()
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP