Thread: Turn program into a void function

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    73

    Turn program into a void function

    I started out with making this program and figured it would be easier to turn it into a void function, how do I do that. I'm not catching on for some reason. here is my code

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
    	float A;
    	float B;
    	float C;
    	float D;
    	float E;
    	float Average;
    	float Sum;
    
    	cout << "Please enter the 1st integer ";	
    	cin >> A;
    	cout << "Please enter the 2nd integer ";	
    	cin >> B;
    	cout << "Please enter the 3rd integer ";	
    	cin >> C;
    	cout << "Please enter the 4th integer ";	
    	cin >> D;
    	cout << "Please enter the 5th integer ";	
    	cin >> E;
    
    	Average = A + B + C + D + E;
    	Sum = Average / 5;
     
    	cout << "The average of the following numbers are " << Sum << endl;	
    		 
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Why would it be easier? This function already does exactly one task (accepts 5 numbers, averages, and then outputs). It doesn't clearly break into pieces that could form standalone functions very easily. I mean, there are some possible improvements you could do, by adding more features for example, but nothing simple that would make it more elegant.

    Also, why on earth do you do this??:

    Code:
    	Average = A + B + C + D + E;
    	Sum = Average / 5;
    The first line computes the sum of 5 numbers, but you name it "Average", the second computes the average of those numbers but you call it "Sum". Unless you like confusing readers, why not call the sum "Sum" and the average "Average" and not vice versa?

  3. #3
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    A void function is basically a function that has no return type, ie. does not return anything. Therefore, if your function is not going to return anything, I would do something like this.

    Code:
    #include <iostream>
    
    using namespace std;
    
    void GetAverage(void);
    
    int main()
    {
    
    
      return 0;
    }
    
    void GetAverage(void)
    {
      float A;
      float B;
      float C;
      float D;
      float E;
      float Average;
      float Sum;
    
      cout << "Please enter the 1st integer ";
      cin >> A;
      cout << "Please enter the 2nd integer ";
      cin >> B;
      cout << "Please enter the 3rd integer ";
      cin >> C;
      cout << "Please enter the 4th integer ";
      cin >> D;
      cout << "Please enter the 5th integer ";
      cin >> E;
    
      Average = A + B + C + D + E;
      Sum = Average / 5;
    
      cout << "The average of the following numbers are "    << Sum << endl;
    }
    However, I would personally write it like this.

    Code:
    #include <iostream.h>
    
    float GetAverage(int NumItems = 5);
    
    int main()
    {
      cout << "\nThe Average is " << GetAverage();
    
      getchar();
    
      return 0;
    }
    
    float GetAverage(int NumItems)
    {
      int InVal, Average = 0;
    
      for(int i = 0; i < NumItems; i++)
      {
        cout << "Please enter integer " << i + 1 << ": ";
        cin >> InVal;
    
        Average += InVal;
      }
    
      return (float)Average / (float)NumItems;
    }
    Oh, use namespace and that forgot it.
    Be a leader and not a follower.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Based on your earlier thread, what you need to do is create a new function called GetMeanof() to find the average. Then call this function from your main() function.
    Code:
    #include <iostream>
    
    using namespace std;
    
    void GetMeanof( int n, float &mean );
                  /* in */ /* out */
    
    int main()
    {
            float mean;
    
            GetMeanof( 5, mean );
    	cout << "The average of the following numbers is " << mean << endl;
            return 0;
    
    }
    
    void GetMeanof( int n, float &mean )
                  /* in */ /* out */
    {
    	float A;
    	float Sum = 0;
    
            for (int i=1; i<=n; i++)
            {
    	   cout << "Please enter float number " << i << ": ";
    	   cin >> A;
               Sum += A;
            }
            mean = Sum / n;
    }
    Last edited by swoopy; 07-18-2003 at 10:37 PM.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    thanks for all the help. I was looking at all of themposts and I see that Iw as not using the for( calculations. I was trying to make them one by one vs making it a shorter version. Thanks for the help everyone.

    Bryan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM