Thread: void!

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    29

    void!

    Code:
     
    
    #include<stdio.h>
    int capture();
    int count();
    void display_output(int x);
    int main()
    {
        int each=0;
        capture();
        each=count();
        display_output(each);
        system("pause");
        return 0;
    }
     int capture()
     {
         int x,y;
         printf("Enter the number of children:");
         scanf("%d",&x);
         printf("Enter the number of bag(s):");
         scanf("%d",&y);
         
     }
     int count()
     {
          int each,x,y;
          each=(y*200)/x;
          return each;
     }
     void display_output(int x)
     {
          printf("Each of the children gets %d candies\n",x);
     }
    how to take the value from capture() to be use in count()? then be displayed in display_output ()?

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    X and y are local variables. They will go out of scope after capture() is called. You have to pass pointer x and y into the function
    Code:
    void capture(int* x,int* y)
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Move the int x, y variables into main. Pass them to Capture as pointers...
    Code:
    void capture(int *x, int *y)
      {   printf("Enter the number of children:");
         scanf("%d",x);
         printf("Enter the number of bag(s):");
         scanf("%d",y);  }
    Actually, in this case getting input variables that you only acquire once isn't a very good candidate for a separate procedure. You could do this inline very easily...

    Also for your count() calculation to be meaningful you're going to have to pass x and y into that function. The copies you declared locally are uninitialized and will almost certainly return garbage answers in each.
    Code:
     int count(int x, int y)
     { return (y*200)/x; }
    Last edited by CommonTater; 01-10-2011 at 02:25 AM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    29
    i already done that but the ans still doesn't appear in display void.why?..

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    What does your code look like currently?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by amoeba532 View Post
    i already done that but the ans still doesn't appear in display void.why?..
    No, you didn't "already done that", because if you did all the above, it would be working.

    A little more reflection on what the above posts are recommending, and a bit less assertion of what you've done, would be helpful.

    When you change your code, and it's still not working, please post up the new version of the code - if we can't see the new code, it's impossible to give specific help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix operations using objects
    By circuitbreaker in forum C++ Programming
    Replies: 7
    Last Post: 05-04-2010, 08:53 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. msvc just ate one of my source files
    By Eber Kain in forum C++ Programming
    Replies: 6
    Last Post: 07-01-2004, 05:40 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM