C++如何使用从一个函数到另一个函数的变量

C++ How do I use variables from one function to another?

本文关键字:函数 一个 另一个 变量 何使用 C++      更新时间:2023-10-16

记录在案,我对C++和任何其他编程语言都很熟悉,所以如果可能的话,如果你能用我能理解的方式回答,我会很高兴:)

我最近一直在做一个简单的石头、纸、剪刀游戏,其中我有三个基本功能,一个为用户,一个用于机器人,还有一个可以选择哪一个赢得游戏。

我知道使用system("cls")不是最好的方法,但我不打算在Windows之外使用它。

最后一个函数results()需要使用前两个函数中的变量xbrand,但我似乎找不到这样做的方法。当人们在其他地方解释它时,他们对我来说解释得太超前了。记住我不需要改变其中的任何一个,我只需要比较它们就可以确定获胜者。

我会在这里给你看我的代码,这样你就可以看到你在处理什么了。请给我其他可以改进的地方的意见。

#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;他们将定义自己。

相关文章: