C++ 如何使用变量从一个函数到另一个函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17718601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ How do I use variables from one function to another?
提问by Rezic
For the record I am completely green in C++ and any other programming language for that matter, so if it's possible I'd be happy if you can answer in a way that I can understand :)
根据记录,我完全不了解 C++ 和任何其他编程语言,所以如果可能的话,如果您能以我能理解的方式回答我会很高兴:)
I have been working on a simple rock, paper, scissors game recently where I have 3 basic functions, one for the user, one for the bot, and one that choose which one wins the game.
我最近一直在做一个简单的石头剪刀布游戏,我有 3 个基本功能,一个给用户,一个给机器人,一个选择谁赢得比赛。
I know using system("cls")
isn't the best way to go, but I'm not planning on using this outside Windows.
我知道使用system("cls")
不是最好的方法,但我不打算在 Windows 之外使用它。
The final function results()
need to use the variables x
and brand
from the two previous functions, however I can't seem to find a way to do this. And where people explain it anywhere else, they explain it too advanced for me. Remeber that I don't need to change any of them, I simply have to compare them to determine the winner.
最后一个函数results()
需要使用变量x
和brand
前两个函数,但是我似乎找不到办法做到这一点。人们在其他任何地方解释它的地方,他们对我来说解释得太高级了。请记住,我不需要更改它们中的任何一个,我只需比较它们即可确定获胜者。
I'll show you my code here so you can see what you are dealing with. Please give me comments on other things I can improve here.
我将在此处向您展示我的代码,以便您了解正在处理的内容。请给我评论我可以在这里改进的其他方面。
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
using namespace std;
int bgame(), ugame(), results();
int main()
{
srand(time(NULL));
cout<<"Welcome to RPS!\n" <<"You know what to do.\n" <<"\n[ENTER]";
cin.ignore();
system("cls");
ugame();
return 0;
}
int ugame()
{
int x;
cout<<"Type a number from 1-3: ";
cin>> x;
cin.ignore();
if ( x == 1 )
{
cout<<"\nYou chose rock!\n" <<"\n[ENTER]";
cin.ignore();
bgame();
}
else if ( x == 2)
{
cout<<"\nYou chose paper!\n" <<"\n[ENTER]";
cin.ignore();
bgame();
}
else if ( x == 3 )
{
cout<<"\nYou chose scissors!\n" <<"\n[ENTER]";
cin.ignore();
bgame();
}
else
{
cout<<"\nTry again.\n" <<"\n[ENTER]";
cin.ignore();
ugame();
system("cls");
}
return 0;
}
int bgame()
{
int brand = rand()>>4;
system("cls");
cout<<"The bot will now choose an item.\n" <<"\n" <<"[ENTER]\n";
cin.ignore();
brand = rand() % 3 + 1;
if ( brand == 1)
{
cout<<"\nBot chose rock!";
cin.ignore();
results();
}
else if ( brand == 2 )
{
cout<<"\nBot chose paper!";
cin.ignore();
results();
}
else if ( brand == 3 )
{
cout<<"\nBot chose scissors!";
cin.ignore();
results();
}
else
{
cout<<"\nError.";
cin.ignore();
bgame();
system("cls");
}
return 0;
}
int results()
{
}
回答by Shar
you can send "parameters" to a function. That's create a copy of your variable and you can use it in another function.
您可以将“参数”发送到函数。这就是创建变量的副本,您可以在另一个函数中使用它。
You need to specifies it with your function name (that's named prototype) like this :
您需要使用您的函数名称(名为原型)指定它,如下所示:
int results(int x, int brand)
You put a type name and a variable name. For example this function will take 2 int as parameters.
您输入类型名称和变量名称。例如,此函数将采用 2 个 int 作为参数。
int results(int x, int brand)
{
// do something with your variables
}
And when you call the function you type:
当你调用函数时,你输入:
results(x, brand);
If you want, this link explains it with images and examples and more details: http://www.cplusplus.com/doc/tutorial/functions/
如果你愿意,这个链接用图像和例子以及更多细节来解释它:http: //www.cplusplus.com/doc/tutorial/functions/
回答by Fla?ndil
First, learn how to use functions with parameters.
首先,学习如何使用带参数的函数。
After that, you should be able to do what you want.
在那之后,你应该能够做你想做的。
As suggested, you only have to do something like this :
按照建议,您只需要执行以下操作:
declaration :
宣言 :
int bgame(int x){
... what bgame do ...
}
and in ugame :
在 ugame 中:
int ugame(){
int x;
...
cin >> x;
...
bgame(x);
}
Hope this helps.
希望这可以帮助。
回答by Thanushan
Use parameters, pass by value. Have parameters in the function header, so you can pass the value into the funcion.
使用参数,按值传递。函数头中有参数,因此您可以将值传递给函数。
using namespace std;
int bgame(int x_p), ugame(), results(int x_p, int brand_p);
int main()
{
srand(time(NULL));
cout<<"Welcome to RPS!\n" <<"You know what to do.\n" <<"\n[ENTER]";
cin.ignore();
system("cls");
ugame();
return 0;
}
int ugame()
{
int x;
cout<<"Type a number from 1-3: ";
cin>> x;
cin.ignore();
if ( x == 1 )
{
cout<<"\nYou chose rock!\n" <<"\n[ENTER]";
cin.ignore();
bgame(x);
}
else if ( x == 2)
{
cout<<"\nYou chose paper!\n" <<"\n[ENTER]";
cin.ignore();
bgame(x);
}
else if ( x == 3 )
{
cout<<"\nYou chose scissors!\n" <<"\n[ENTER]";
cin.ignore();
bgame(x);
}
else
{
cout<<"\nTry again.\n" <<"\n[ENTER]";
cin.ignore();
ugame();
system("cls");
}
return 0;
}
int bgame(int x_p)
{
int brand = rand()>>4;
system("cls");
cout<<"The bot will now choose an item.\n" <<"\n" <<"[ENTER]\n";
cin.ignore();
brand = rand() % 3 + 1;
if ( brand == 1)
{
cout<<"\nBot chose rock!";
cin.ignore();
results(x_p, brand);
}
else if ( brand == 2 )
{
cout<<"\nBot chose paper!";
cin.ignore();
results(x_p, brand);
}
else if ( brand == 3 )
{
cout<<"\nBot chose scissors!";
cin.ignore();
results();
}
else
{
cout<<"\nError.";
cin.ignore();
bgame(x_p);
system("cls");
}
return 0;
}
int results(int x_p, int brand_p)
{
// Do whatever you want to do with x (x_p) and brand (brand_p).
}
回答by Musicrafter12
For you, you only need to return 1 value for each function, so return would work. Instead of return 0;, use return x; and return brand;. In the comparing function, define two new variables, such as y; or crand;, then assign them like this: y=int ugame(); and crand=int bgame();. Then y=x and crand=brand and you can work from there. So then you don't need any parameters or anything. Also, don't try to define brand straight out, just declare int brand; and let the randomizing function (which must be seeded with srand by the way) assign a value to brand. You also don't have to define the functions as int; they will define themselves.
对你来说,你只需要为每个函数返回 1 个值,所以 return 会起作用。使用 return x; 代替 return 0; 并返回品牌;. 在比较函数中,定义两个新变量,如y;或 crand;,然后像这样分配它们:y=int ugame(); 和 crand=int bgame();。然后 y=x 和 crand=brand ,您可以从那里开始工作。那么你不需要任何参数或任何东西。另外,不要试图直接定义品牌,只需声明 int 品牌;并让随机函数(顺便说一句,必须用 srand 播种)为品牌分配一个值。您也不必将函数定义为 int;他们会定义自己。