void function

This is a discussion on void function within the C++ Programming forums, part of the General Programming Boards category; im a beginner in void function... my knowledge about void functions aren't that good. i just want to ask your ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    1

    void function

    im a beginner in void function... my knowledge about void functions aren't that good. i just want to ask your help in this. i want to total all the commission but i don't know how..
    here is my code:
    Code:
    #include <iostream>
    using namespace std;
    
    void calculatecomm(float y)
    {
         float Commission = y  * .10;
    }
    void displaycomm(float x)
    {
      cout << " The commission is: $" << x * .10 << endl;
    }
    void totalcomm(float total)
    {
         float sc=0.0;
         total = total + sc;
         cout << " total : " << total << endl;
    }
    float getsales(float &s1)
    {
             float g=0;
             cout << endl << " Input negative number to exit " << endl;
             cout << " Enter sales amount: $";
             cin >> s1;
             
             while (s1 < 0)
             {     
                   totalcomm(g);
                   system("pause");
                   exit(0);
             }       
    }
    int main()
    {       
            float s1,s2,i;
            
            for ( i = 0; ;i++ )
            { 
                getsales(s1);
                displaycomm(s1);
            }
            //system ("pause");
            
    }
    i really need your help guys... tnx in advance !

  2. #2
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    Well the problem is void functions don't return anything. So when you use it to calculate a number the data inside the function will be deleted after the function runs. Well not necessarily "deleted" but it will go out of scope.

    You also have a float function, but you don't return a float. Your functions should return the type you are naming it to be.

    So you ask yourself does this function need to return a value or does it just need to run some code?

    If it needs to give the number back then you can use a function with a return type. You can also use the method you are demonstrating where you pass by reference and change the variable inside of a void function.

    As for accumulating the total you seem to have declared variable G to do this, but you aren't incrimenting it in any way during the course of the user inputting their sales amounts.

    The other problem is if you were adding to it you would still lose it as it would passes out of scope everytime that you leave this function. So the variable G should not be declared inside of this function, you need it to be a little more permanent.

    You could use variable s2 to do this, but rename it as something that truly describes its purpose, and then add to its value after each new sale amount is entered by the user.


    On a side note avoid using "system" commands.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. difference between void and static void function
    By mahaju in forum C++ Programming
    Replies: 7
    Last Post: 12-27-2011, 03:02 AM
  2. Replies: 12
    Last Post: 03-27-2009, 02:36 PM
  3. Passing a variable in void to another void function
    By stevedawg85 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 06:17 PM
  4. void, void function
    By Furious_George in forum C++ Programming
    Replies: 4
    Last Post: 10-14-2003, 05:04 PM
  5. void non void function
    By modance in forum C Programming
    Replies: 6
    Last Post: 01-28-2003, 07:06 AM

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