Function 2 Help Please!

This is a discussion on Function 2 Help Please! within the C Programming forums, part of the General Programming Boards category; 2. Write a program with two functions. In main ask for a number. Pass this to the first function and ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    31

    Function 2 Help Please!

    2. Write a program with two functions. In main ask for a number. Pass this to the first function and print the value. In the first function ask for a character. Pass the first number and the character to the second function. In the second function print the number entered in main and the character entered in the first function. Back in main print "That's the end of this lab".

    Code:
     
    Code:
    #include<stdio.h>
    void fun (float); /* the FUNCTION PROTOTYPE  */	
    main()
    {
    	float a;
    	printf("Please enter a number: "); /* Program in MAIN */
    	scanf("%f",&a);
    	fun(a);
        getch();
    }
    
    
    void fun (float x) /* Function Header/Definition   NO semicolon  */
    {
    	char q;
        printf("The number you entered is %4.2f.",x);
    	printf("\n\nPlease enter a character: ");
    	scanf("%c",q);
    	return(0); /* value of product is returned to the main function */
    }
    Can you Please help to send from one function to another? Please!

    Thank You So Much!

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by hnparmar View Post
    Can you Please help to send from one function to another?
    You have already successfully passed a value from one function to another (from main() to fun()). So why do you have a problem doing it again? The only difference is that instead of one value you have to pass two values to the second function.

    Bye, Andreas

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Even more so, you have a call to scanf being passed two parameters already, so it's not like there is some mysterious notation that you don't know how to use.
    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.

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409
    haha - nice copy and paste job mate, I think the 'learning lab' comments give it away a bit too

    You have already successfully passed a value from one function to another (from main() to fun()). So why do you have a problem doing it again?
    My guess is because the OP does not understand the code they had as example guide. Call me a cynic maybe.
    Last edited by rogster001; 03-16-2013 at 05:14 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  6. #6
    Registered User
    Join Date
    Mar 2013
    Posts
    31
    Code:
    #include<stdio.h>
    void fun (float); /* the FUNCTION PROTOTYPE  */	
    void fun2 (char , float);
    main()
    {
    	float a;
    	printf("Please enter a number: "); /* Program in MAIN */
    	scanf("%f",&a);
    	fun(a);
    	printf("\nBack in main.");
    	printf("\n\nThat 's the end of this lab.");
        getchar();
       }
    
    
    void fun (float x ) /* Function Header/Definition –  NO semicolon  */
    {
    	char c;
    	printf("\nFirst function");
        printf("\n\nThe number you entered is %4.2f.",x);
    	printf("\n\nPlease enter a character:  ");
    	scanf("%c",&c);
    	getch();
    	fun2(c,x);
    	return(0); /* value of product is returned to the main function */
    }
    
    
    void fun2(char c , float x)
    {
    	
    	printf("\n\nSecond function.");
    	printf("The numer  you entered is %4.2f.",x);
    	printf("\n\nThe character you have entered is %c",c);
    	getch();
    	return(0);
    }
    Can you help me please with passing a character ?
    Last edited by hnparmar; 03-18-2013 at 03:45 PM.

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by hnparmar View Post
    Can you help me please with passing a character ?
    You have to tell us what's wrong with your program.

    I guess that your program skips the line where you try to get the character input. The problem is that there is still a newline charater in the input buffer after you have entered the float number. Use
    Code:
    scanf(" %c", &c); // note the leading space before %
    See also FAQ > How do I avoid a "dangling" newline when reading single character user input? - Cprogramming.com

    Bye, Andreas

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    He loses the char from the first function, so it won't print in the second one, also.

    Code:
    #include<stdio.h>
    
    void fun (char *,float); /* the FUNCTION PROTOTYPE  */
    void fun2 (char, float);
    int main(void)
    {
        float a;
        char c;
        printf("Please enter a number: "); /* Program in MAIN */
        scanf("%f",&a);
        fun(&c,a);
        printf("\nBack in main.");
        printf("\n\nThat 's the end of this lab.");
        getchar();
    
       return 0;
    }
     
     
    void fun (char *c,float x ) /* Function Header/Definition –  NO semicolon  */
    {
        printf("\nFirst function");
        printf("\n\nThe number you entered is %4.2f.",x);
        printf("\n\nPlease enter a character:  ");
        scanf(" %c",c);
        getchar();
        fun2(*c,x);
    }
     
     
    void fun2(char c, float x)
    {
         
        printf("\n\nSecond function.");
        printf("The numer  you entered is %4.2f.",x);
        printf("\n\nThe character you have entered is %c",c);
        getchar();
    }
    That's one way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 09-07-2012, 04:35 AM
  2. Replies: 13
    Last Post: 03-20-2012, 08:29 AM
  3. Replies: 2
    Last Post: 02-26-2009, 10:48 PM
  4. Print function: sending a function.. through a function?
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 11-05-2008, 04:03 PM
  5. Replies: 9
    Last Post: 01-02-2007, 03:22 PM

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