Jump to content
Science Forums

Recommended Posts

Posted

Hey guys

 

Can you use a funCtion in a switch block instead of the normal pfintf("")

 

#include <stdio.h>

 

 

int main()

 

{

char letter;

printf("press A or B or C...");

scanf("%c",letter);

 

switch(letter)

{

case 'a' : option();break; //CAN THIS CASE TRIGGER A FUNCTION?

case 'b' : option2();break;

case 'c' : option3();break;

defualt : printf("not a b or c");

}

 

return main();

 

}

 

 

WHERE WOULD I DECLARE A FUCTION IF I CAN CALL FROM THE SWITCH BLOCK CASE.?

 

I was trying to create a multiple choice program where each individual answer would give way to different multiple choice questions for each origional question.

 

i thought maybe that each answer would call a function that would call another switch block.....:turtle:

Posted

Your code should work fine (except for a few typos: "defualt" and "return main();"). You can put the function declarations right before the main block:

void option() {
  ...code here...
}
void option2() {
  ...code here...
}
void option3() {
  ...code here...
}

int main() {
  ...your current code...
}

 

Happy hacking!

Buffy

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...