Thread: Passing a variable in void to another void function

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    36

    Passing a variable in void to another void function

    Ok, so in my first void, the user inputs 4 numbers via a looping statement. In the next void statement, it is supposed to add these four numbers up and print them on screen. We are currently on structures/classes but I'm not sure how to print the summed up number in another void statement.
    Code:
    void Salesperson :: getsales ()
    {
       double revenue; 
    
       for ( int i = 1; i <= 4; i++ ) 
       {
          cout << "Enter sales for quarter " << i << ": ";
          cin >> revenue;
          setsales( i, revenue);
    
       } // end for
    
    }
    As you can see, here is were the user inputs the four numbers. I am not sure if the numbers are added here as well or if they are added in the next void statement.
    Last edited by stevedawg85; 05-05-2006 at 04:48 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What you call a void statement is just a function. In this case, it is a function with a void return type (meaning it does stuff but doesn't "return" any results).

    It appears setsales is a function that should be storing the values that are input in the Salesperson class. You have to figure out where in the Salesperson data that information is kept (checking the setsales function is a good start). Then you have to figure out how to retrieve that information in the function that does the adding. There might even be a getsales() function that you can use. You just have to look at the definition of the Salesperson class to see. If you haven't coded it yet, then maybe you have to add those things yourself.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    36
    here is where the function is passed to, but i'm not sure if it is right:
    [code]
    void Salesperson :: setsales (int quarternum, double amount)
    {
    // test for valid month and amount values
    double amount = 0.0; // initialize total

    if
    ( quarternum >= 1 && quarternum <= 4 && amount > 0 )
    sales[ quarternum - 1 ] = amount ;

    else
    cout << "Invalid month or sales figure" << endl;
    for ( int i = 0; i < 4; i++ ) // summarize sales results
    amount += sales[ i ];

    }
    Here is my setsales funtion.
    my next void function is basically:

    Code:
    void printsales( )
    {
       cout << "\nThe total annual sales are: $" << amount;
    }
    My prof gave me a hint and said, "stores all variables in your class" What does that mean?
    Last edited by stevedawg85; 05-05-2006 at 05:04 PM.

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Quote Originally Posted by stevedawg85
    here is where the function is passed to, but i'm not sure if it is right:
    [code]
    void Salesperson :: setsales (int quarternum, double amount)
    {
    // test for valid month and amount values
    double amount = 0.0; // initialize total
    This RE-initializes the total amount passed to it to 0.0, making all of this below pretty futile

    if
    ( quarternum >= 1 && quarternum <= 4 && amount > 0 )
    sales[ quarternum - 1 ] = amount ;

    else
    cout << "Invalid month or sales figure" << endl;
    for ( int i = 0; i < 4; i++ ) // summarize sales results
    amount += sales[ i ];

    }
    Here is my setsales funtion.
    my next void function is basically:

    Code:
    void printsales( )
    {
       cout << "\nThe total annual sales are: $" << amount;
    }
    My prof gave me a hint and said, "stores all variables in your class" What does that mean?
    this function kind of "erases" the amount value passed to it. It should be something like
    Code:
     double total = 0.0

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by stevedawg85
    My prof gave me a hint and said, "stores all variables in your class" What does that mean?
    Assuming you know the difference between a class and an object..
    He means that the variables need to be inside the Salesperson class declaration. This means that every Salesperson object you create in your program will have their own unique data for values such as the number of sales achieved.

    Judging by what you've said, Your Salesperson class probably should have a member variable which holds the sum of all the sales, or otherwise some kind of container or array which holds the sales individually for each quarter.
    Last edited by Bench82; 05-05-2006 at 05:40 PM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> sales[ quarternum - 1 ] = amount ;

    That is where you store the data, in an array called sales. So in the function that calculates the total, get the four values from the sales array and total them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  4. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 10:19 AM