C++如何使用从一个函数到另一个函数的变量
C++ How do I use variables from one function to another?
记录在案,我对C++和任何其他编程语言都很熟悉,所以如果可能的话,如果你能用我能理解的方式回答,我会很高兴:)
我最近一直在做一个简单的石头、纸、剪刀游戏,其中我有三个基本功能,一个为用户,一个用于机器人,还有一个可以选择哪一个赢得游戏。
我知道使用system("cls")
不是最好的方法,但我不打算在Windows之外使用它。
最后一个函数results()
需要使用前两个函数中的变量x
和brand
,但我似乎找不到这样做的方法。当人们在其他地方解释它时,他们对我来说解释得太超前了。记住我不需要改变其中的任何一个,我只需要比较它们就可以确定获胜者。
我会在这里给你看我的代码,这样你就可以看到你在处理什么了。请给我其他可以改进的地方的意见。
#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()
{
}
您可以向函数发送"参数"。这就是创建变量的副本,然后可以在另一个函数中使用它。
你需要用你的函数名(命名为原型)来指定它,如下所示:
int results(int x, int brand)
您放置了一个类型名和一个变量名。例如,此函数将使用2 int作为参数。
int results(int x, int brand)
{
// do something with your variables
}
当你调用你键入的函数时:
results(x, brand);
如果您愿意,此链接将通过图片、示例和更多详细信息进行解释:http://www.cplusplus.com/doc/tutorial/functions/
首先,学习如何使用带参数的函数。
在那之后,你应该能够做你想做的事。
正如建议的那样,你只需要做这样的事情:
声明:
int bgame(int x){
... what bgame do ...
}
在乌干达:
int ugame(){
int x;
...
cin >> x;
...
bgame(x);
}
希望这能有所帮助。
使用参数,按值传递。在函数头中有参数,这样您就可以将值传递到函数中。
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).
}
对您来说,每个函数只需要返回1个值,所以返回就可以了。不是返回0;,使用return x;回归品牌;。在比较函数中,定义两个新的变量,例如y;或裂隙;,然后这样分配它们:y=int-ugame();和crand=int bgame();。然后y=x和crand=brand,你就可以在那里工作了。所以你不需要任何参数或任何东西。此外,不要试图直接定义品牌,只需声明int品牌;并让随机化函数(顺便说一句,它必须与srand一起播种)为brand赋值。您也不必将函数定义为int;他们将定义自己。
- 函数向量_指针有不同的原型,我可以构建一个吗
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 如何仅为一个函数添加延迟
- 构造函数正在调用一个使用当前类类型的函数
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 如何创建函数管道,以便函数一个接一个地运行?
- 如何巧妙地编写两个函数——一个用于检查是否存在解决方案,另一个用于获取所有解决方案
- 在c++中的复制构造函数/一个声明语句中的初始化的延续中使用chain方法
- C :基类调用自己的虚拟函数 - 一个反图案
- 如何在这个交换函数(一个单独的链表)中找到错误
- 两个相同的函数(一个使用模板模式,另一个不使用)
- 你怎么能一次给一个函数一个参数呢
- 为什么要做两个函数?(一个是非const,另一个是const)
- 当代码在其他地方使用时,如何保证函数一个接一个地被调用