Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
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
  • 875 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