473,698 Members | 2,242 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_val ue" 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_Ch ill(int &airtemp, int &windspeed, int &wcvalue);
void Wait();

int wind_chill_valu e; // **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 << "\nSELECTIO N: ";
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_valu e;

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_Ch ill(air_temp, wind_speed, wind_chill_valu e);
}
// ***** 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_valu e = 35.74 + (0.6215 * airtemp) - 35.75 *
( pow (windspeed , 0.16 )) + (0.4275 * airtemp
* ( pow (windspeed , 0.16 )));

}
// ***** Display_Wind_Ch ill *****
/* 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_Ch ill (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 2268
"da Vinci" <bl***@blank.co m> 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_val ue" 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.co m> wrote in message
news:k7******** *************** *********@4ax.c om...
// ** 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_Ch ill(int &airtemp, int &windspeed, int &wcvalue);
void Wait();

int wind_chill_valu e; // **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 << "\nSELECTIO N: ";
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_valu e;


This is the second time you've defined wind_chill_valu e. 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.********@com Acast.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.********@com Acast.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_valu e. -8...... huge negative number.

Ill try again and hope I find it.
Jul 19 '05 #5

"da Vinci" <bl***@blank.co m> wrote in message news:u4******** *************** *********@4ax.c om...
On Mon, 27 Oct 2003 20:00:28 GMT, "Victor Bazarov"
<v.********@com Acast.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_valu e.
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_valu e, references as the
wcvalue, is in the Display_Wind_Ch ill 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.********@com Acast.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.co m> wrote in message
news:qf******** *************** *********@4ax.c om...
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_valu e, references as the
wcvalue, is in the Display_Wind_Ch ill 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_Ch ill, 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_valu e global, but all your other variabels are not? :-) Again,
it looks inconsistent and confusing.
Jul 19 '05 #10

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

Similar topics

35
4537
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 go to great lengths to restructure my code just to look concise and make it more manageable. When I say this, I'm also referring to the way I write my functions. It seems to me sometimes that I shouldn't have many void functions accepting...
11
1916
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 this HTML file, all is OK but whenever the functions are separated (as it is now), when I run the page, I get the following error: Line 73, object expected.
7
3302
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 buffer into the character pointer. The code looks like the following: #include <stdio.h> #include <stdlib.h> #include "stdafx.h" BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call,
5
1428
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 Windows Controls. Say ControlA.dll and ControlB.dll. As usual I know how to add these .dlls as the reference and then start using
4
1217
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 one of them I need to change the
0
5569
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 ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
10
2108
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 #these are the operations + = , - = , * = , 1/ = only if 0 not in .
3
1851
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 seats as 0 and the flight no. is also repeating. If any can tell why is this please help me. #include<stdio.h> #include<ctype.h> #include<conio.h> #include<memory.h>
0
1449
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 function with two reference parameters of the same type is a typical example to explain the purpose of references to objects in C++ and the way they can be used, but since functions cannot be "changed" (in the sense "exchange the bodies of...
0
8676
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9164
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8898
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8870
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7734
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4370
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.