Like Tree1Likes

Can't call from within switch?

This is a discussion on Can't call from within switch? within the C Programming forums, part of the General Programming Boards category; I've been trying to get the menu to call the two functions using a switch function but I can't seem ...

  1. #1
    Registered User TheWhiffet's Avatar
    Join Date
    Apr 2011
    Location
    Hell.
    Posts
    14

    Question 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);
    
    void menu(int *input);
    
    main()
    	{
    
    	float result,input,cresult,cinput;
    
    	int menu_input;
    
    	celsius(&cresult,&cinput);
    
    	fahrenheit(&result,&input);
    
    	menu(&menu_input);	
    	}
    
    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(&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); 
    	
    }
    Last edited by TheWhiffet; 04-26-2011 at 12:28 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    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.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  4. #4
    Registered User TheWhiffet's Avatar
    Join Date
    Apr 2011
    Location
    Hell.
    Posts
    14
    Err so what do I have to do? Do I have to declare them within the menu function?

  5. #5
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    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?
    Last edited by nimitzhunter; 04-25-2011 at 11:51 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > float result,input,cresult,cinput;
    Put this at the start of menu

    > celsius(&cresult,&cinput);
    > fahrenheit(&result,&input);
    Put these in the appropriate cases.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User TheWhiffet's Avatar
    Join Date
    Apr 2011
    Location
    Hell.
    Posts
    14
    By at the start of the menu do you mean the start of the menu function?

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,293
    Your code contains nested functions. You can't do that in C.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User TheWhiffet's Avatar
    Join Date
    Apr 2011
    Location
    Hell.
    Posts
    14
    Ok, so I've separated the functions. How do I get the menu function to call them? It says the variables are undeclared.

  10. #10
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    63
    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);
    
    	menu(&menu_input);	
    	}
    
    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(&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);
    You are storing the input value into the pointer address not into the "menu_input" variable address.
    Change your code as per the below line.
    Code:
    scanf("%d",&input);

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,407
    Quote Originally Posted by ArunS View Post
    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);
    You are storing the input value into the pointer address not into the "menu_input" variable address.
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    63
    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);
    
    void menu(int *input);
    
    float result,input,cresult,cinput;
    
    int menu_input;
    
    
    main()
    	{
    
    	celsius(&cresult,&cinput);
    
    	fahrenheit(&result,&input);
    
    	menu(&menu_input);	
    	}
    
    void menu(int *m_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); 
    	
    }

  13. #13
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    63
    @laselight: Thanks for your valuable reply.
    I was little confused with the scanf function.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,407
    Quote 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.

    Quote 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.
    What is your current code?
    Salem likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    63
    As per TheWhiffet's code, he/she try to access his/her local variables in "menu" function which are defined in another function. So either he/she has to make variable as global or has to change the "menu" function prototype. i.e pass those local variables as an arguments in "menu" function.
    Welcome to any other opinion!!!

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help!! call-by-reference and call-by-value
    By geoffr0 in forum C Programming
    Replies: 14
    Last Post: 04-01-2009, 07:15 PM
  2. A switch that doesn't switch
    By Death_Wraith in forum C++ Programming
    Replies: 5
    Last Post: 11-06-2004, 11:18 AM
  3. C system call and library call
    By Coconut in forum C Programming
    Replies: 6
    Last Post: 08-22-2002, 11:20 AM
  4. Call-by-value Vs. Call-by-reference
    By Wiz_Nil in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2002, 08:06 AM
  5. call by reference and a call by value
    By IceCold in forum C Programming
    Replies: 4
    Last Post: 09-08-2001, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21