probably something simple I'm overlooking

This is a discussion on probably something simple I'm overlooking within the C++ Programming forums, part of the General Programming Boards category; I can't seem to figure out what I am doing wrong with passing the parameters between functions. I know that ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    2

    Question probably something simple I'm overlooking

    I can't seem to figure out what I am doing wrong with passing the parameters between functions. I know that the calculations are correct (they are simple) and in the getTestScores function I went ahead and tested the calculations to make sure. It seems like the values for test1, 2 and 3 aren't being stored in memory as whatever I input gives a value of 0. getTestScores and displayAverage both need to be void functions and that's what I'm struggling with I think.

    -------------------------Here's the code-------------------------
    Code:
    #include <iostream>
    #include <iomanip>
    
    using std::cout;
    using std::cin;
    using std::endl;
    using std::fixed;
    using std::setprecision;
    
    //function prototypes
    void getTestScores (double, double, double);
    double calcAverage (double, double, double, double);
    void displayAverage (double); //void function, no return value to main.
    
    int main()
    {
    
    	//declare variables & initialize
    	double average = 0.0;
    	double test1 = 0.0;
    	double test2 = 0.0;
    	double test3 = 0.0;
    
    	//call function to get test scores
    	getTestScores (test1, test2, test3);
    	
    	//call function to calculate average
    	calcAverage (average, test1, test2, test3);
    	
    	//call function to display average
    	displayAverage (average);
    
    	system ("pause");
    	return 0;
    }   //End of the main function
    
    //*************************Function Definitions**********************
    
    //Get Test Scores function
    void getTestScores (double test1, double test2, double test3)
    {
    	cout << "Enter the first test score: ";
    	cin >> test1;
    	cout << "Enter the second test score: ";
    	cin >> test2;
    	cout << "Enter the third test score: ";
    	cin >> test3;
    }	//end of getTestScores function
    
    //Calculate Average function (return average value)
    double calcAverage (double average, double test1, double test2, double test3)
    {
    	average = (test1 + test2 + test3) / 3;
    	return average;
    }	//end of calcAverage function
    
    //Display Average function (double)
    void displayAverage (double average)
    {			
    	cout << "The average of the three scores you entered is: " << average << endl;
    }	//end of displayAverage function
    Any help would be greatly appreciated by this noob!

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    where does the return of calcAverage go?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    getTestScores is being called with parameters "by value" (aka, copies), not "by reference".
    As a result, you're not changing your values in main.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    2
    argh, I figured it out. Had a couple of issues actually and taking a break and coming back to it it was obvious.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 06:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 06:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM

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