C++/passing values by reference
Expert: vijayan - 11/24/2009
QuestionMy program is almost finished ,but I am confused on how to pass values by reference through functions. I know that you have to use the ampersand, but I do not know which values exactly need to be passed by reference.
The menu towards the top of my code should explain what each function needs to do. Right now none of my parameters match for function calls, prototypes, or definitions. I just can not figure out exactly how passing by reference works.
Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
double const PI = 3.14159;
void wordLengths (string&);
void integers (string&);
void sphere (float&);
void cylinder (float&, float&);
void angle (double&);
int main()
{
int choice;
ifstream inFile;
float radius, height;
double degrees;
string fileName;
do
{
cout << "1. Display the Longest and Shortest Word In a File"
<< endl;
cout << "2. Display the Maximum and Minimum Integer In a File"
<< endl;
cout << "3. Compute the Surface Area and Volume of a Sphere"
<< endl;
cout << "4. Compute the Surface Area and Volume of a Cylinder"
<< endl;
cout << "5. Compute the Sine, Cosine, and Tangent of an Angle"
<< endl;
cout << "6. Quit" << endl << endl;
cout << "Choice : ";
cin >> choice;
cout << endl;
switch (choice)
case 1: cout << "Enter the Input File Name : ";
cin >> fileName;
cout << endl;
wordLengths (wordMax, wordMin);
cout << "Longest Word : " << wordMax << endl;
cout << "Shortest Word : " << wordMin << endl;
break;
case 2: cout << "Enter the Input File Name : ";
cin >> fileName;
cout << endl;
integers (maxInt, minInt);
cout << "Maximum of Integers : " << maxInt << endl;
cout << "Minimum of Integers : " << minInt << endl;
break;
case 3: cout << "Enter the radius : ";
cin << radius;
cout << endl;
sphere (surfaceSphere, volumeSphere);
cout << "Surface Area : " << surfaceSphere << endl;
cout << " Volume : " << volumeSphere << endl;
break;
case 4: cout << "Enter the radius & height : ";
cin << radius << height;
cout << endl;
cylinder (surfaceCyl, volumeCyl);
cout << "Surface Area : " << surfaceCyl << endl;
cout << " Volume : " << volumeCyl << endl;
break;
case 5: cout << "Enter the angle in degrees : "
cin << degrees;
cout << endl;
angle (rads, sine, cosine, tangent);
cout << "Angle in Radians : " << rads << endl;
cout << " Sine : " << sine << endl;
cout << " Cosine : " << cosine << endl;
cout << " Tangent : " << tangent << endl;
break;
case 6: cout << "Thank You -- Goodbye!" << endl;
exit(1);
default: cout << "Invalid Option -- Try Again!"
break;
}
while (choice != 6);
cout << "Thank You -- Goodbye!" << endl;
inFile.close();
exit (1);
}
void wordLengths (string& fileName)
{
string maxWord, minWord, word;
inFile.open(fileName.c_str())
if (inFile.fail())
{
cout << "Error opening input file -- " <<
fileName << "please try again" << endl;
return;
}
inFile >> word;
maxWord.length() = word.length();
minWord.length() = word.length();
// initialize values
while (inFile)
{
if (word.length() >= maxWord.length())
{
maxWord = word;
}
if (word.length() <= minWord.length())
{
minWord = word;
}
inFile >> word;
}
inFile.close ();
return;
}
void integers (string& fileName)
{
int maxInt, minInt, integer;
inFile.open(fileName.c_str())
if (inFile.fail())
{
cout << "Error opening input file -- " <<
fileName << "please try again" << endl;
return;
}
inFile >> integer;
maxInt = integer;
minInt = integer;
// initialize values
while (inFile)
{
if (integer >= maxInt)
{
maxInt = integer;
}
if (integer <= minInt)
{
minInt = integer;
}
inFile >> integer;
}
inFile.close();
return;
}
void sphere (float& radius)
{
double surfaceSphere, volumeSphere;
float radius;
surfaceSphere = 4 * PI * radius * radius;
volumeSphere = 4.0 / 3 * PI * pow(radius, 3);
return;
}
void cylinder (float& height, float& radius)
{
double surfaceCyl, volumeCyl;
float radius, height;
surfaceCyl = (2 * PI * radius * radius) +
(2 * PI * radius * height);
volumeCyl = PI * radius * radius * height;
return;
}
void angle (double& degrees)
{
double tangent, cosine, sine, rads, degrees;
rads = degrees * PI / 180;
tangent = tan(rads);
cosine = cos(rads);
sine = sin(rads);
return;
}
AnswerAs a programmer, just think of a reference as another name (an alias) for a variable. For example,
void swap( int& i, int& j )
{
int temp = i ;
i = j ;
j = temp ;
}
int main()
{
int x = 20, y = 45 ;
// ...
swap( x, y ) ; // x and y are passed by reference
// ...
}
In the above example, when swap is called, i and j are aliases for main's x and y respectively.
i *IS* x — not a pointer to x, nor a copy of x, but x itself. Anything you do to i gets done to x, and vice versa. And j *IS* y.