请帮助:功能之间的参考 [英] Help Please: References between Functions

查看:58
本文介绍了请帮助:功能之间的参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再次问好。


我有一个问题,即逐个引用和多个函数。

这是我必须使用的一个赋值参考

一切。


首先,我做了以下程序来弄清楚出了什么问题

in我的主要计划。这个程序工作正常。它编译并且

值在输出中是正确的。


// ** START **

#include< iostream>


使用命名空间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;

}

// **结束**


现在,我对''z使用了同样的想法''变量在上面。我移动了

变量wind_chill_value全球可变的。在此之前,

该程序根本无法编译。一旦我将它移动到全局,

该程序将完成并运行正常。除了我在

得到的价值,它的结束是错误的。我无法弄清楚发生了什么。我的

程序代码如下。


// ** START **

#include< iostream>

#include< cmath>

#include< string>


使用命名空间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; // **全局变量我遇到问题**


void main()//测试函数的Temp Main

{

Wind_Chill();

}

// ***** Main_Menu *****

/ *此函数将显示主菜单并读入变量

进行选择。没有vlue将被返回,因为所有变量将在此程序中通过引用传递。 * /


无效Main_Menu()

{

int selection = 0;


system(" cls");

cout<< " \ n \ n";

cout<< " ********************** \ n";

cout<< " * TEMPERATURE CENTER * \ n";

cout<< " **************************** \ n";

cout<< " * o o * \ n";

cout<< * 1.将F转换为C * \ n;;

cout<< " * o o * \ n";

cout<< * 2.将C转换为F * \ n;;

cout<< " * * \ n";

cout<< * 3.确定风寒* \ n;

cout<< " * * \ n";

cout<< * 4.结束程序* \ n;

cout<< " * * \ n";

cout<< " **************************** \ n";


cout< < " \ nSELECTION:";

cin>>选择;

}

// ***** Wind_Chill *****

/ *此函数将收到一个值空气温度和

风速用于单独的功能。这个funstion将

还提示用户查看气温输入是否以摄氏度为b
或华氏度。如果它是摄氏度,那么它将调用单独的

函数将其转换为华氏度。在此函数中不执行任何计算

。 * /


void Wind_Chill()

{

int air_temp; //用于风寒计算的空气温度

int wind_speed; //风寒计算的风速

int temp_not_f; //确定air_temp是否处于正确的形式

int wind_chill_value;


system(" cls");


cout<< " \ n \ n";

cout<< " ********************** \ n";

cout<< " * WIND CHILL * \ n";

cout<< ********************** \ n \ nn;


cout<< 要计算风寒,请输入以下内容

<< "信息.... \ n \ n" ;;


cout<< 指示气温:;

cin>> air_temp;


cout<< 指示风速:;

cin>> wind_speed;


cout<< 您输入的气温是多少?

in?

<< \ n(1 =华氏,2 =摄氏度)" ;;

cin>> temp_not_f;


if(temp_not_f == 2)//调用转换函数转换为F

Get_Fahrenheit(air_temp);


Calculate_Wind_Chill(air_temp,wind_speed);


Display_Wind_Chill(air_temp,wind_speed,wind_chill_value);

}

// ***** Get_Fahrenheit *****

/ *此函数将通过引用引入变量并将其转换为华氏度数。没有返回任何值,因为这个程序中的所有内容都将通过引用传递。 * /


void Get_Fahrenheit(int& temp)

{

//注意:& temp代表变量temp_not_f


temp = 9 * temp / 5 + 32; //将temp转换为Fahrenheit

}

// ***** Calculate_Wind_Chill *****

/ *此函数将计算风寒的价值。 * /

// *******这就是我遇到问题的变量

分配值********* *


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))); < br $>

}

// ***** Display_Wind_Chill *****

/ *此函数将引用所有的输入和计算的数据

并显示此程序的风寒部分

的最终答案。 * /


// ******这就是输出到屏幕的地方*********


void Display_Wind_Chill(int& airtemp,int& windspeed,int& wcvalue)

{

cout<< " \ n \ n";


cout<< " ************************* \ n";

cout<< *气温= << airtemp<< " * \ n" ;;

cout<< *风速= << windspeed<< " * \ n" ;;

cout<< ************************* \ n \ n \\ n \\ nn; \\ n;

cout<< ; Wind Chill = << wcvalue<< 。\ n \\ nn;


等待();

}

// **** *等待*****

/ *此函数将使用字符串创建暂停,直到用户

点击回车键。输入的任何其他字符都将被忽略。* /

void等待()

{

字符串响应;

cout<< 按Enter键继续;

getline(cin,response);

}


// ** END * *


好​​吧,所以,这就是最重要的。我知道有一些额外的功能

和一些失踪。我还没有添加它们。主要功能

现在是一行用于测试目的。


非常感谢任何帮助!!!

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

推荐答案

" da Vinci" < BL *** @ blank.com>写了...
"da Vinci" <bl***@blank.com> wrote...
再次问好。

我有一个问题,即逐个引用和多个函数。
这是我必须使用的一项任务

首先,我制作了以下程序来弄清楚我的主程序中出了什么问题。这个程序工作正常。它编译并且
值在输出中是正确的。

// ** START **
#include< iostream>

使用命名空间std;

void function_1(int& xx,int& yy);
void function_2(int& xx,int& yy,int& zz);

int z;

void main()


''main''返回''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;
}
// **结束**
现在,我对上面的''z''变量使用了相同的想法。我移动了
变量wind_chill_value。全球可变的。


你呢?你确定吗?或者您是否创建了_another_变量,现在是全局的
,与某个局部变量同名?你知道,调试你的程序并没有受到伤害。

在这之前,程序根本无法编译。一旦我将它移动到全球,
程序将完成并运行得很好。除了我得到的值
结束它是错误的。我无法弄清楚发生了什么。我的
程序代码如下。
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.




从_initialising_ _every_变量开始到某个值(更好

与其他任何值不同) 。然后看看哪一个改变了

以及何时。


Victor



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

Victor




da Vinci < BL *** @ blank.com>在消息中写道

新闻:k7 ******************************** @ 4ax.com ...

"da Vinci" <bl***@blank.com> wrote in message
news:k7********************************@4ax.com...
// ** START **
#include< iostream>
#include< cmath>
#include< string>

使用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; // **全局变量我遇到问题**

void main()// Temp Main for Testing Functions
{
Wind_Chill();
}

// ***** Main_Menu *****
/ *此功能将显示主菜单并读入变量
供选择。不会返回vlue,因为所有变量都将在此程序中通过引用传递。 * /

无效Main_Menu()
{
int selection = 0;

系统(" cls");

cout<< " \ n \ n";
cout<< " ********************** \ n";
cout<< *温度中心* \ n;
cout<< " **************************** \ n";
cout<< " * o o * \ n";
cout<< * 1.将F转换为C * \ n;;
cout<< " * o o * \ n";
cout<< * 2.将C转换为F * \ n;;
cout<< " * * \ n";
cout<< * 3.确定风寒* \ n;
cout<< " * * \ n";
cout<< * 4.结束程序* \ n;;
cout<< " * * \ n";
cout<< " **************************** \ n";

cout<< " \ nSELECTION:";
cin>>选择;
}

// ***** Wind_Chill *****
/ *此功能将获得气温和风的值在单独的功能中使用的速度。此功能还将提示用户查明气温输入是以摄氏度还是华氏度为单位。如果它是摄氏度,那么它将调用单独的
函数将其转换为华氏度。在此功能中不会执行任何计算。 * /

void Wind_Chill()
{
int air_temp; //用于风寒计算的空气温度
int wind_speed; //风寒计算的风速
int temp_not_f; //确定air_temp是否处于正确的形式
int wind_chill_value;
// ** 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;




这是你第二次定义wind_chill_value。那太漂亮

令人困惑。另外,为什么要通过引用传递所有这些参数?

为什么要将wcvalue参数传递给某些函数但不传递给其他函数?

这也让人感到困惑。 br />



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.


2003年10月27日星期一20:00:28 GMT,Victor Bazarov

< v。****** **@comAcast.net>写道:

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

你呢?你确定吗?或者您是否创建了_another_变量,现在是全局的,与某个局部变量同名?你知道调试程序并没有什么坏处。
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.




好​​的,发现了问题。


虽然之前我多次看过它,但我没有意识到我已经用了一个新的变量,用相同的名字创建了一个新的变量

当我正在调整事情。所以,很好的抓住了! :-)


我调试代码。但是,我必须只是在这整个

的事情上没有实例,因为我没有抓住我重新宣布的一个小变量。

:-) Darn !!!! br />

感谢您的帮助。虽然这个讽刺一开始让我很生气,但是现在我回想起它并且我犯了一个多么愚蠢的错误! :-)

再次感谢!



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!


这篇关于请帮助:功能之间的参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆