473,732 Members | 2,217 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
20 2274
On Mon, 27 Oct 2003 22:04:21 GMT, Default User
<fi********@boe ing.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_valu e; // **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_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_val ue 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.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.


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.co m> wrote in message
news:tm******** *************** *********@4ax.c om...

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.co m> wrote in message
news:fm******** *************** *********@4ax.c om...

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.l earn.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.co m> wrote in message
news:u4******** *************** *********@4ax.c om...

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
"implementa tion-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.l earn.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.co m> wrote in message
news:fo******** *************** *********@4ax.c om...
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

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

Similar topics

35
4542
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
1924
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
3306
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
1430
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
1220
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
5573
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
2114
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
1852
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
1454
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
8946
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
8774
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9447
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...
0
9307
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9181
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
6031
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
4550
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...
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2180
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.