Output of a function into another function

This is a discussion on Output of a function into another function within the C Programming forums, part of the General Programming Boards category; how do i get an output of a function into another function? for example: Code: #include <stdio.h> int initialize (void); ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    8

    Output of a function into another function

    how do i get an output of a function into another function?
    for example:

    Code:
    #include <stdio.h>
    int initialize (void);
    int menu (void);
    
    int main (void)
    {
          initialize ();
    
          return 0;
    }
    
    void initialize ()
    {
          int numbers1;
    
          printf("Please input 5 numbers\n");
          scanf("%d",&numbers1);
    
          printf("\nThe numbers are %d\n",numbers1);
    
          menu ();
    }
    
    void menu ()
    {
         /*I would like the above numbers from initialize () that have been input to show here, how can i do that?*/
    }

    having problem working that out, someone please help me
    Last edited by Dave_Sinkula; 07-30-2006 at 10:06 PM. Reason: Added [code][/code] tags. You're welcome.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Five numbers aren't going to fit into a single number, so an array might be appropriate. Then, since arrays are passed as a pointer to the first member, problems ought to melt away. [edit]I ought to mention that you should pass "the array" a parameter -- are you familiar with doing so?[/edit] But first, reconsider your user input.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    8
    lol, i just simply wrote there
    lets say it goes like this

    Code:
    #include <stdio.h>
    
    int initialize (void);
    int menu (void);
    
    int main (void)
    {
          initialize ();
          
          return 0;
    }
    
    void initialize ()
    {
          int numbers1,numbers2,numbers3,numbers4,numbers5;
    
          printf("Please enter 5 numbers\n");
          scanf("%d",&numbers1);
          scanf("%d",&numbers2);
          scanf("%d",&numbers3);
          scanf("%d",&numbers4);
          scanf("%d",&numbers5);
    
          printf("\n%d %d %d %d %d\n", numbers1,numbers2,numbers3,numbers4,numbers5);
    
          menu ();
    }
    
    void menu ()
    {
          /*How do i get the above input here*/
    }

    well, i dont know wut array is. can help me out?

    btw, is it possible to input words and get the output as words?
    example, i type "Hello world" into the scanf and the output i get is "Hello world"
    how do i write such coding? is it possible?

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    123
    An array is where you have a repeating data structure, each element in the structure having the same data type.

    This can be an internal standard data type (such as int) or a user-defined structure.

    In this case, you could declare an integer array using int numbers[5]. This would give you 5 values, numbers[0] through to numbers[4].

    The subscript can be an index variable, for example...

    Code:
    for (i = 0; i < 5; i ++)
    {
    	x = numbers[i];
    	...
    	...
    }
    However, this does look like homework to me so don't use anything you haven't been taught about yet (or it'll look line you've cheated!). You may need to pass the 5 numbers as discrete parameters.

    To input and receive string values, use a character array or a pointer to an array of char.

    In other words, char instring[100]

    or char *instring and then malloc() some space (and free() it afterwards)
    I think you can put a signature here.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    8
    thanks for the help, i understand the array part now
    yeah, its a homework. but i'm practicing different things to try out new things.
    how bout getting an input of a word, example "Nike" and get the output "Nike"
    the only input and output i learn are the "int, float, double,char"

    Code:
    int main (void)
    {
         char choice;
    
         printf("Please input your brand choice :");
         scanf("%c",&choice);   /*Could i input the word Nike instead of just a N? */
    
         printf("%c",choice); /* Can i get the output Nike instead of just a N? */
    
         return 0;
    }
    done, thanks for all the help. the char instring had work out. thanks again.
    Last edited by skydancer; 07-31-2006 at 07:29 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  2. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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