D
da Vinci
Hello again.
I have a question regaring pass-by-reference and multiple functions.
This is an assignment that I have to use pass-by-reference for
everything.
First off, I made the following program to figure out what was wrong
in my main program. This program works fine. It compiles AND the
values are correct in the output.
// ** START**
#include <iostream>
using namespace std;
void function_1 (int &xx, int &yy);
void function_2 (int &xx, int &yy, int &zz);
int z;
void main ()
{
int x=1, y=1;
function_1(x,y);
function_2(x,y,z);
}
void function_1 (int &xx, int &yy)
{
z = xx + yy;
}
void function_2 (int &xx, int &yy, int &zz)
{
cout << "X = " << xx;
cout << "Y = " << yy;
cout << "Z = " << zz;
}
// ** END**
Now, I used the same idea for the 'z' variable above. I moved the
variable "wind_chill_value" to a global varaible. Prior to doing this,
the program would not compile at all. Once I moved it to the global,
the program will complile and run just fine. Except the value I get at
the end for it is way wrong. I cannot figure out what is going on. My
program code is below.
// ** START **
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
void Main_Menu();
void Wind_Chill();
void Get_Fahrenheit (int &temp);
void Calculate_Wind_Chill (int &airtemp, int &windspeed);
void Display_Wind_Chill(int &airtemp, int &windspeed, int &wcvalue);
void Wait();
int wind_chill_value; // **global variable I am having problems with**
void main() // Temp Main for Testing Functions
{
Wind_Chill();
}
// ***** Main_Menu *****
/* This function will display the main menu and read in a variable
for their selection. No vlue will be returned as all variables will
be pass-by-reference in this program. */
void Main_Menu()
{
int selection=0;
system ("cls");
cout << "\n\n";
cout << "**********************\n";
cout << "* TEMPERATURE CENTER *\n";
cout << "****************************\n";
cout << "* o o *\n";
cout << "* 1. Convert F to C *\n";
cout << "* o o *\n";
cout << "* 2. Convert C to F *\n";
cout << "* *\n";
cout << "* 3. Determine Wind Chill *\n";
cout << "* *\n";
cout << "* 4. End Program *\n";
cout << "* *\n";
cout << "****************************\n";
cout << "\nSELECTION: ";
cin >> selection;
}
// ***** Wind_Chill *****
/* This function will receive a value for the air temperature and
wind speed to be used in a seperate function. This funstion will
also prompt the user to find out if the air temp input is in Celsius
or Fahrenheit. If it is in celsius, then it will call a seperate
function to convert it to fahrenheit. No computations will be
performed in this function. */
void Wind_Chill()
{
int air_temp; // Air Temperature For Wind Chill Calculation
int wind_speed; // Wind Speed For Wind Chill Calculation
int temp_not_f; // Determines if air_temp is in proper form
int wind_chill_value;
system ("cls");
cout << "\n\n";
cout << "**********************\n";
cout << "* WIND CHILL *\n";
cout << "**********************\n\n";
cout << "To calculate the wind chill, please enter the following"
<< " information....\n\n";
cout << "Indicated Air Temperature: ";
cin >> air_temp;
cout << "Indicated Wind Speed: ";
cin >> wind_speed;
cout << "What scale is this air temperature you entered measured
in?"
<< "\n(1 = Fahrenheit, 2 = Celsius)";
cin >> temp_not_f;
if (temp_not_f == 2) // Calls conversion function to convert to F
Get_Fahrenheit(air_temp);
Calculate_Wind_Chill(air_temp, wind_speed);
Display_Wind_Chill(air_temp, wind_speed, wind_chill_value);
}
// ***** Get_Fahrenheit *****
/* This function will bring in a variable by reference and convert
it to degrees fahrenheit. No value being returned as everything
will be pass-by-reference in this program. */
void Get_Fahrenheit (int &temp)
{
// Note: &temp represents variable temp_not_f
temp = 9 * temp / 5 + 32; // Converts temp to Fahrenheit
}
// ***** Calculate_Wind_Chill *****
/* This function will calculate the wind chill value. */
// ******* THIS IS WHERE THE VARIABLE I AM HAVING PROBLEMS WITH IS
ASSIGNED A VALUE **********
void Calculate_Wind_Chill (int &airtemp, int &windspeed)
{
wind_chill_value = 35.74 + (0.6215 * airtemp) - 35.75 *
( pow (windspeed , 0.16 )) + (0.4275 * airtemp
* ( pow (windspeed , 0.16 )));
}
// ***** Display_Wind_Chill *****
/* This function will reference all of the data entered and computed
and will display the final answers for the wind chill portion of
this program. */
// ****** THIS IS WHERE IT IS OUTPUT TO THE SCREEN *********
void Display_Wind_Chill (int &airtemp, int &windspeed, int &wcvalue)
{
cout << "\n\n";
cout << "*************************\n";
cout << "* Air Temperature = " << airtemp << " *\n";
cout << "* Wind Speed = " << windspeed << " *\n";
cout << "*************************\n\n\n";
cout << "Wind Chill = " << wcvalue << ".\n\n";
Wait();
}
// ***** Wait *****
/* This function will use a string to create a pause until the user
hits the enter key. Any other characters entered will be ignored.*/
void Wait()
{
string response;
cout << "Press Enter to continue";
getline(cin, response);
}
// ** END **
OK, so, thats the most of it. I know there are a few extra functions
and a few missing. I just havent added them in yet. The main function
is a single line right now for testing purposes.
Any help is greatly appreciated!!!
I have a question regaring pass-by-reference and multiple functions.
This is an assignment that I have to use pass-by-reference for
everything.
First off, I made the following program to figure out what was wrong
in my main program. This program works fine. It compiles AND the
values are correct in the output.
// ** START**
#include <iostream>
using namespace std;
void function_1 (int &xx, int &yy);
void function_2 (int &xx, int &yy, int &zz);
int z;
void main ()
{
int x=1, y=1;
function_1(x,y);
function_2(x,y,z);
}
void function_1 (int &xx, int &yy)
{
z = xx + yy;
}
void function_2 (int &xx, int &yy, int &zz)
{
cout << "X = " << xx;
cout << "Y = " << yy;
cout << "Z = " << zz;
}
// ** END**
Now, I used the same idea for the 'z' variable above. I moved the
variable "wind_chill_value" to a global varaible. Prior to doing this,
the program would not compile at all. Once I moved it to the global,
the program will complile and run just fine. Except the value I get at
the end for it is way wrong. I cannot figure out what is going on. My
program code is below.
// ** START **
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
void Main_Menu();
void Wind_Chill();
void Get_Fahrenheit (int &temp);
void Calculate_Wind_Chill (int &airtemp, int &windspeed);
void Display_Wind_Chill(int &airtemp, int &windspeed, int &wcvalue);
void Wait();
int wind_chill_value; // **global variable I am having problems with**
void main() // Temp Main for Testing Functions
{
Wind_Chill();
}
// ***** Main_Menu *****
/* This function will display the main menu and read in a variable
for their selection. No vlue will be returned as all variables will
be pass-by-reference in this program. */
void Main_Menu()
{
int selection=0;
system ("cls");
cout << "\n\n";
cout << "**********************\n";
cout << "* TEMPERATURE CENTER *\n";
cout << "****************************\n";
cout << "* o o *\n";
cout << "* 1. Convert F to C *\n";
cout << "* o o *\n";
cout << "* 2. Convert C to F *\n";
cout << "* *\n";
cout << "* 3. Determine Wind Chill *\n";
cout << "* *\n";
cout << "* 4. End Program *\n";
cout << "* *\n";
cout << "****************************\n";
cout << "\nSELECTION: ";
cin >> selection;
}
// ***** Wind_Chill *****
/* This function will receive a value for the air temperature and
wind speed to be used in a seperate function. This funstion will
also prompt the user to find out if the air temp input is in Celsius
or Fahrenheit. If it is in celsius, then it will call a seperate
function to convert it to fahrenheit. No computations will be
performed in this function. */
void Wind_Chill()
{
int air_temp; // Air Temperature For Wind Chill Calculation
int wind_speed; // Wind Speed For Wind Chill Calculation
int temp_not_f; // Determines if air_temp is in proper form
int wind_chill_value;
system ("cls");
cout << "\n\n";
cout << "**********************\n";
cout << "* WIND CHILL *\n";
cout << "**********************\n\n";
cout << "To calculate the wind chill, please enter the following"
<< " information....\n\n";
cout << "Indicated Air Temperature: ";
cin >> air_temp;
cout << "Indicated Wind Speed: ";
cin >> wind_speed;
cout << "What scale is this air temperature you entered measured
in?"
<< "\n(1 = Fahrenheit, 2 = Celsius)";
cin >> temp_not_f;
if (temp_not_f == 2) // Calls conversion function to convert to F
Get_Fahrenheit(air_temp);
Calculate_Wind_Chill(air_temp, wind_speed);
Display_Wind_Chill(air_temp, wind_speed, wind_chill_value);
}
// ***** Get_Fahrenheit *****
/* This function will bring in a variable by reference and convert
it to degrees fahrenheit. No value being returned as everything
will be pass-by-reference in this program. */
void Get_Fahrenheit (int &temp)
{
// Note: &temp represents variable temp_not_f
temp = 9 * temp / 5 + 32; // Converts temp to Fahrenheit
}
// ***** Calculate_Wind_Chill *****
/* This function will calculate the wind chill value. */
// ******* THIS IS WHERE THE VARIABLE I AM HAVING PROBLEMS WITH IS
ASSIGNED A VALUE **********
void Calculate_Wind_Chill (int &airtemp, int &windspeed)
{
wind_chill_value = 35.74 + (0.6215 * airtemp) - 35.75 *
( pow (windspeed , 0.16 )) + (0.4275 * airtemp
* ( pow (windspeed , 0.16 )));
}
// ***** Display_Wind_Chill *****
/* This function will reference all of the data entered and computed
and will display the final answers for the wind chill portion of
this program. */
// ****** THIS IS WHERE IT IS OUTPUT TO THE SCREEN *********
void Display_Wind_Chill (int &airtemp, int &windspeed, int &wcvalue)
{
cout << "\n\n";
cout << "*************************\n";
cout << "* Air Temperature = " << airtemp << " *\n";
cout << "* Wind Speed = " << windspeed << " *\n";
cout << "*************************\n\n\n";
cout << "Wind Chill = " << wcvalue << ".\n\n";
Wait();
}
// ***** Wait *****
/* This function will use a string to create a pause until the user
hits the enter key. Any other characters entered will be ignored.*/
void Wait()
{
string response;
cout << "Press Enter to continue";
getline(cin, response);
}
// ** END **
OK, so, thats the most of it. I know there are a few extra functions
and a few missing. I just havent added them in yet. The main function
is a single line right now for testing purposes.
Any help is greatly appreciated!!!