473,396 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help Please: References between Functions

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!!!
Jul 19 '05 #1
20 2234
"da Vinci" <bl***@blank.com> wrote...
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
Jul 19 '05 #2

"da Vinci" <bl***@blank.com> wrote in message
news:k7********************************@4ax.com...
// ** 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.
Jul 19 '05 #3
On Mon, 27 Oct 2003 20:00:28 GMT, "Victor Bazarov"
<v.********@comAcast.net> wrote:

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!
Jul 19 '05 #4
On Mon, 27 Oct 2003 20:00:28 GMT, "Victor Bazarov"
<v.********@comAcast.net> wrote:
'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.
Jul 19 '05 #5

"da Vinci" <bl***@blank.com> wrote in message news:u4********************************@4ax.com...
On Mon, 27 Oct 2003 20:00:28 GMT, "Victor Bazarov"
<v.********@comAcast.net> wrote:
'main' returns 'int'.


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.
Jul 19 '05 #6
On Mon, 27 Oct 2003 16:21:09 -0500, "jeffc" <no****@nowhere.com>
wrote:

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. :)
Jul 19 '05 #7
On Mon, 27 Oct 2003 16:31:43 -0500, "Ron Natalie" <ro*@sensor.com>
wrote:

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?

Jul 19 '05 #8
da Vinci wrote:

On Mon, 27 Oct 2003 20:00:28 GMT, "Victor Bazarov"
<v.********@comAcast.net> wrote:
'main' returns 'int'.


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
Jul 19 '05 #9

"da Vinci" <bl***@blank.com> wrote in message
news:qf********************************@4ax.com...
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.


OK.
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. :)


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.
Jul 19 '05 #10
On Mon, 27 Oct 2003 22:04:21 GMT, Default User
<fi********@boeing.com.invalid> wrote:

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.
Jul 19 '05 #11
<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
Jul 19 '05 #12
On Mon, 27 Oct 2003 17:18:46 -0500, "jeffc" <no****@nowhere.com>
wrote:
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). :-)

Jul 19 '05 #13

"da Vinci" <bl***@blank.com> wrote in message
news:u4********************************@4ax.com...
On Mon, 27 Oct 2003 20:00:28 GMT, "Victor Bazarov"
<v.********@comAcast.net> wrote:
'main' returns 'int'.


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++.
Jul 19 '05 #14

"da Vinci" <bl***@blank.com> wrote in message
news:tm********************************@4ax.com...

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.
Jul 19 '05 #15

"da Vinci" <bl***@blank.com> wrote in message
news:fm********************************@4ax.com...

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.
Jul 19 '05 #16
jeffc wrote:

"da Vinci" <bl***@blank.com> wrote in message
news:u4********************************@4ax.com...

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.


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
Jul 19 '05 #17
On Tue, 28 Oct 2003 10:31:54 -0500, "jeffc" <no****@nowhere.com>
wrote:
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!!
Jul 19 '05 #18
On Tue, 28 Oct 2003 10:33:13 -0500, "jeffc" <no****@nowhere.com>
wrote:
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. :-)

Jul 19 '05 #19

"da Vinci" <bl***@blank.com> wrote in message
news:fo********************************@4ax.com...
On Tue, 28 Oct 2003 10:31:54 -0500, "jeffc" <no****@nowhere.com>
wrote:
What version of the Deitel book do you have out of curiosity? It might beupdated 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!!


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.
Jul 19 '05 #20
On Tue, 28 Oct 2003 14:18:28 -0500, "jeffc" <no****@nowhere.com>
wrote:

Hmm, that appears to be the latest version. It has gotten good reviews.
It's not a bad book at all. I beleive my college uses the Deitel books
in all their programming classes except Visual C++. Don't qoute me on
that though.
I used to search for the one perfect book, until I realized I was better off with several books, several
perspectives, etc.


Very true. I have this book, two Visual C++ books (one by Bates &
Thomkins and one by Ivor Horton) and Schildt's 2e Programmers
reference (smaller one). Although I am probably going to get hammered
for saying the S word... :-)
Jul 19 '05 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
11
by: milkyway | last post by:
Hello, I have an HTML page that I am trying to import 2 .js file (I created) into. These files are: row_functions.js and data_check_functions.js. Whenever I bring the contents of the files into...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
5
by: Anand Ganesh | last post by:
Hi All, I need some help. I am sort of not sure how to approach this problem. I have a MAINPROGRAM. This is the core application. I have asked two of my staff to developed two different...
4
by: Tiraman | last post by:
Hi , Problem description : I have 2 assemblies (A.dll And B.dll) . They are both under the GAC and they are both using the functions of each other . Each time that I m doing a change in...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
10
by: CuTe_Engineer | last post by:
hii, i have cs assignment i tried to solve it but i still have many errors , plzz help mee :"< it`s not cheating becuz i`ve tried & wrote the prog. i just wanna you to show me my mistakes ...
3
by: Alami | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of...
0
by: Irfy | last post by:
Could someone elaborate on the purpose and concrete usage scenarios of references to functions. What can references to functions do that pointers to functions cannot. Why are they in C++? A swap...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.