Help Please: References between Functions

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!!!
 
V

Victor Bazarov

da Vinci said:
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 ()

'main' returns 'int'.
{
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.

Did you? Are you sure? Or did you create _another_ variable, now
global, with the same name as some local variable? It doesn't hurt
to debug your program, you know.
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.

Begin with _initialising_ _every_ variable to some value (better
different from any other value). Then see which one gets changed
and when.

Victor
 
D

da Vinci

'main' returns 'int'.

I dont follow. In this case, main returns nothing. It simply outputs
some information to the screen and terminates, returning nothing.

Did you? Are you sure? Or did you create _another_ variable, now
global, with the same name as some local variable? It doesn't hurt
to debug your program, you know.

I am not sure. I was under the understanding that a global variable
reaches into every function. Am I wrong?

Seeing as how I am a student and learning this for the first time, I
have run into a problem that I cannot figure out and have asked for
help. I have already looked it over many times, tried new things,
wrote smaller example programs to try sand figure it out, and still
cannot find where I have errored. Sarcasm isnt welcome. Im flustered
enough with this as it is.
Begin with _initialising_ _every_ variable to some value (better
different from any other value). Then see which one gets changed
and when.

I had already initilized the variables to values and entered in values
to which I knew the answer; but even then I still get the same output
for wind_chill_value. -8...... huge negative number.

Ill try again and hope I find it.
 
D

da Vinci

Did you? Are you sure? Or did you create _another_ variable, now
global, with the same name as some local variable? It doesn't hurt
to debug your program, you know.

OK, found the problem.

While I had looked over it many times before, I didnt realize I had
left a new variable, created with the same name, in there from before
when I was tweaking things. So, nice catch on that! :)

I do debug the code. But, I must just be inexperianced at this whole
thing because I didnt catch the one little variable I had re-declared.
:) Darn it!!!

Thanks for your help. While the sarcasm annoyed me at first, its kinda
funny now that I look back at it and how stupid a mistake I made! :)
Thanks again!
 
J

jeffc

da Vinci said:
// ** 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;

This is the second time you've defined wind_chill_value. That's pretty
confusing. Also, why are you passing all those parameters by reference?
Why are you passing the wcvalue parameter to some functions but not others?
That's also confusing.
 
R

Ron Natalie

da Vinci said:
I dont follow. In this case, main returns nothing. It simply outputs
some information to the screen and terminates, returning nothing.

It is required to be declared as a function returning int.
 
D

da Vinci

This is the second time you've defined wind_chill_value.

I had just found that one after Victors response. I looked over the
program quite a few times and never saw that. Talk about being annoyed
when I found it!!! :)
That's pretty confusing. Also, why are you passing all those parameters by reference?

It is a school assignment. She wanted everything passed by reference.
I found that in this case it would have been easier to do something
like....

wind_chill_temp = Calculate_Wind_Chill (.......)

and have the Calculate_Wind_Chill return an INT value.... but the
assignment said all references. So, I had to play the game....
Why are you passing the wcvalue parameter to some functions but not others?
That's also confusing.

The only time I actually need the wind_chill_value, references as the
wcvalue, is in the Display_Wind_Chill function where it outputs the
final values.

I hope I understood your question right. :)
 
D

da Vinci

It is required to be declared as a function returning int.

Is this under ANSI/ISO?

The only reason I ask is because a few people on this group told me a
while back that there are only three formats for main....

int main()
void main ()
void main (void)

I looked through my college text (by: Deitel) and they do not state
that main must be int main. Directly after it talks about the main
function in Chapter 3 (for those of you with the book), they begin
talking about the void type. But they do not specify main must be int.

Don't think I am saying your wrong. I am just trying to find the
correct answer, who makes it required, and stating my own research.
:)

The compiler doesn't mind! But is that portable amongst all compilers
or is that why int is required?
 
D

Default User

da said:
I dont follow. In this case, main returns nothing. It simply outputs
some information to the screen and terminates, returning nothing.

That's where you are wrong, the program returns to the hosting
environment. Victor is absolutely correct, main() must be declared with
return type int, the standard says so. You can leave out the return
value, for main() only, and the compiler will default return 0.



Brian Rodenborn
 
D

da Vinci

That's where you are wrong, the program returns to the hosting
environment. Victor is absolutely correct, main() must be declared with
return type int, the standard says so. You can leave out the return
value, for main() only, and the compiler will default return 0.
Brian Rodenborn


Interesting. OK, I see the logic in that. Wasn't sure what required
it, but apparently it is ANSI/ISO.

Thanks for the info.
 
J

jeffc

da Vinci said:
by reference?

It is a school assignment. She wanted everything passed by reference.
OK.


The only time I actually need the wind_chill_value, references as the
wcvalue, is in the Display_Wind_Chill function where it outputs the
final values.

I hope I understood your question right. :)

No, you also use it in Calculate_Wind_Chill. For that function, you just
use the global variable. For Display_Wind_Chill, you pass it as a
parameter. That looks inconsistent. Either you should use a global
variable, or you shouldn't. But if you do, then I will ask why is only
wind_chill_value global, but all your other variabels are not? :) Again,
it looks inconsistent and confusing.
 
A

Agent Mulder

<Da Vinci> I cannot figure out what is going on. </>

Your program is just great to hack for starters. Make objects out of it. I
wish I had this when I was learning.

<Da Vinci>
int wind_chill_value; // **global variable I am having problems with**
</>

Yes, frosty ;-)

-X
 
D

da Vinci

No, you also use it in Calculate_Wind_Chill. For that function, you just
use the global variable. For Display_Wind_Chill, you pass it as a
parameter. That looks inconsistent. Either you should use a global
variable, or you shouldn't. But if you do, then I will ask why is only
wind_chill_value global, but all your other variabels are not? :) Again,
it looks inconsistent and confusing.

AH!! I see exactly what you mean now!

OK, awesome, I fixed that. I hadn't even though of the fact that it
was global and easily used without passing it in. Duh!

Thanks for the insight on that one. I missed that (obviously). :)
 
J

jeffc

da Vinci said:
I dont follow. In this case, main returns nothing. It simply outputs
some information to the screen and terminates, returning nothing.

He meant that the C++ standard says that main is always supposed to have an
int return type. If you don't have anything to return, return 0. Some
compilers enforce this and some don't. If yours doesn't, don't worry about
it until you use one that does. But it would be better to get into the
habit now, so you're using standard C++.
 
J

jeffc

da Vinci said:
I looked through my college text (by: Deitel) and they do not state
that main must be int main.

What version of the Deitel book do you have out of curiosity? It might be
updated in a newer version.
The compiler doesn't mind! But is that portable amongst all compilers
or is that why int is required?

void main will only work on some compilers. I think int main will work on
all compilers.
 
J

jeffc

da Vinci said:
Thanks for your help. While the sarcasm annoyed me at first, its kinda
funny now that I look back at it and how stupid a mistake I made! :)
Thanks again!

Supposedly alt.comp.lang.learn.c-c++ is easier on newbie questions. But you
need a thick skin everywhere you go nowadays.
 
D

Default User

jeffc said:
He meant that the C++ standard says that main is always supposed to have an
int return type. If you don't have anything to return, return 0.

Well, returning 0 is returning something, to be specific an
"implementation-defined form of the status successful termination [to
the host environment]."

Basically, you should never have "nothing" to return. The program is
either exiting normally, in which case 0 or EXIT_SUCCESS is appropriate,
or it is exiting due to error, in which case EXIT_FAILURE should be
returned.





Brian Rodenborn
 
D

da Vinci

What version of the Deitel book do you have out of curiosity? It might be
updated in a newer version.

We are using the "C++ How To Program" 4E (Fourth Edition) ISBN
0-13-038474-7

Its not a bad book... I just wish they put more indepth examples in
the book instead of the ones they have. They are to simple and when
you want to make a more complex program using the same ideas, its
tough.

Although, alot can be said for personal motivation in doing this as
well. I just hate referencing 3 other books and a newsgroup to find
the info I need. :)

All in all, I don't mind the book. I have had A LOT worse!!
 
D

da Vinci

Supposedly alt.comp.lang.learn.c-c++ is easier on newbie questions. But you
need a thick skin everywhere you go nowadays.

Very true. I am normally that way but was having one of "those days"
if you know what I mean.

Besides, it was a pretty stupid mistake. :)
 
J

jeffc

da Vinci said:
We are using the "C++ How To Program" 4E (Fourth Edition) ISBN
0-13-038474-7

Its not a bad book... I just wish they put more indepth examples in
the book instead of the ones they have. They are to simple and when
you want to make a more complex program using the same ideas, its
tough.

Although, alot can be said for personal motivation in doing this as
well. I just hate referencing 3 other books and a newsgroup to find
the info I need. :)

All in all, I don't mind the book. I have had A LOT worse!!

Hmm, that appears to be the latest version. It has gotten good reviews.
Not sure why int main isn't more clear in it. I used to search for the one
perfect book, until I realized I was better off with several books, several
perspectives, etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top