# Thread: Can't call from within switch?

1. ## Can't call from within switch?

I've been trying to get the menu to call the two functions using a switch function but I can't seem to get it to work properly. The codes work fine individually but when I try to put them together they end up just messing up.
Code:
```/*Author: Leo-Aldo A. Delos Reyes
Date Created: Apr 25,2011
*/

#include <stdio.h>

void celsius(float *cres, float *cinp);

void fahrenheit(float *res, float *inp);

main()
{

float result,input,cresult,cinput;

celsius(&cresult,&cinput);

fahrenheit(&result,&input);

}

{

printf("Press input a number. \n[1] for Celsius to Farenheit Conversion \n [2] Farenheit to Celsius Conversion \n [3] EXIT \n");
scanf("%d",input);

switch(*input)
{
case 1:
celsius(&cresult,&cinput);
break;
case 2:
fahrenheit(&result,&input);
break;

}

}

void celsius(float *cres, float *cinp)
{

printf("\n[Celsius to Fahrenheit Conversion]\nINPUT CELSIUS:\n");
scanf("%f", cinp);

*cres=((*cinp*9)/5)+32;

printf("RESULT IS %0.2f\n\n", *cres);
}

void fahrenheit(float *res, float *inp)
{

printf("\n[Fahrenheit to Celcius Conversion]\nINPUT FAHRENHEIT:\n");
scanf("%f", inp);

*res=((*inp-32)*5)/9;

printf("RESULT IS %0.2f\n\n", *res);

}```

2. Code:
```void menu(int *input)
{

printf("Press input a number. \n[1] for Celsius to Farenheit Conversion \n [2] Farenheit to Celsius Conversion \n [3] EXIT \n");
scanf("%d",input);

switch(*input)
{

case 1:

celsius(*cres,*cinp);
break;
case 2:
fahrenheit(*res,*inp);
break;```
None of those are actually known to the function they are in. You have to be passing them arguments (and in this case, those arguments even if known are probably being passed wrong) that you actually have in scope some place.

Quzah.

3. main not returning anything is a fail.

Look at the way your code is indented. There must be a reason right? You included both fahrenheit and celcius inside the function menu. I've never since function written that way before.

4. Err so what do I have to do? Do I have to declare them within the menu function?

5. You can define functions within a function. You need to write three separate functions. You should have:
Code:
```void menu( whatever )
{
body of the function
}

void fahrenheit( something)
{
body of hte function
}

void celcius( bleh)
{
body
}```
Go count the open and close bracelet. Where is the "}" for menu located?

6. > float result,input,cresult,cinput;
Put this at the start of menu

> celsius(&cresult,&cinput);
> fahrenheit(&result,&input);
Put these in the appropriate cases.

7. By at the start of the menu do you mean the start of the menu function?

8. Your code contains nested functions. You can't do that in C.

9. Ok, so I've separated the functions. How do I get the menu function to call them? It says the variables are undeclared.

10. You made 2 mistakes from my point of view.
1. you are not clear how to handle the pointers.
2. you have used function definitions inside another functions. That is very critical.

Explanation for the 1 point.
Code:
```celsius(&cresult,&cinput);

fahrenheit(&result,&input);

}

{

printf("Press input a number. \n[1] for Celsius to Farenheit Conversion \n [2] Farenheit to Celsius Conversion \n [3] EXIT \n");
scanf("%d",input);

switch(*input)
{
case 1:
celsius(&cresult,&cinput);
break;
case 2:
fahrenheit(&result,&input);
break;

}```
As per menu function declaration, pointer to an integer is the only argument for that. And you passed the address of the integer variable "menu_input" from the main function correctly.
You access the same variable "menu_input" inside the menu function by its address. But what you done???

Code:
`scanf("%d",input);`
Change your code as per the below line.
Code:
`scanf("%d",&input);`

11. Originally Posted by ArunS
As per menu function declaration, pointer to an integer is the only argument for that. And you passed the address of the integer variable "menu_input" from the main function correctly.
You access the same variable "menu_input" inside the menu function by its address. But what you done???

Code:
`scanf("%d",input);`
Change your code as per the below line.
Code:
`scanf("%d",&input);`
Actually, scanf("%d", input) is correct. Taking the address of the pointer to int does not make sense since we want to pass a pointer to int as the second argument of scanf.

12. Modify your code as per below changes, then it will work.
I think you will know from the changes what are all the mistakes you had done.
Do reply me if you have any doubt.

Code:
```/*Author: Leo-Aldo A. Delos Reyes
Date Created: Apr 25,2011
*/

#include <stdio.h>

void celsius(float *cres, float *cinp);

void fahrenheit(float *res, float *inp);

float result,input,cresult,cinput;

main()
{

celsius(&cresult,&cinput);

fahrenheit(&result,&input);

}

{

printf("Press input a number. \n[1] for Celsius to Farenheit Conversion \n [2] Farenheit to Celsius Conversion \n [3] EXIT \n");
scanf("%d",m_input);

switch(*m_input)
{
case 1:
celsius(&cresult,&cinput);
break;
case 2:
fahrenheit(&result,&input);
break;

}

}

void celsius(float *cres, float *cinp)
{

printf("\n[Celsius to Fahrenheit Conversion]\nINPUT CELSIUS:\n");
scanf("%f", cinp);

*cres=((*cinp*9)/5)+32;

printf("RESULT IS %0.2f\n\n", *cres);
}

void fahrenheit(float *res, float *inp)
{

printf("\n[Fahrenheit to Celcius Conversion]\nINPUT FAHRENHEIT:\n");
scanf("%f", inp);

*res=((*inp-32)*5)/9;

printf("RESULT IS %0.2f\n\n", *res);

}```

I was little confused with the scanf function.

14. Originally Posted by ArunS
Modify your code as per below changes, then it will work.
I think you will know from the changes what are all the mistakes you had done.
Do reply me if you have any doubt.
Sorry, but I doubt that your code is a "model answer". TheWhiffet's code uses local variables. Your code uses global variables unnecessarily, yet you actually dare propose that TheWhiffet modify his/her code to be like yours. There was no need to offer a "model answer" in the first place, at this point of time.

Originally Posted by TheWhiffet
Ok, so I've separated the functions. How do I get the menu function to call them? It says the variables are undeclared.