繁体   English   中英

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

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

为了记录我在C ++和任何其他编程语言方面完全是绿色的,所以如果有可能我会很高兴,如果你能以我能理解的方式回答:)

我最近在一个简单的摇滚,纸张,剪刀游戏上工作,我有3个基本功能,一个用于用户,一个用于机器人,一个选择哪一个赢得游戏。

我知道使用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()
{

}

你可以将“参数”发送给一个函数。 这是创建变量的副本,您可以在另一个函数中使用它。

您需要使用您的函数名称(名为prototype)来指定它,如下所示:

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 ...
}

在ugame:

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; 或者crand;,然后像这样分配它们:y = int ugame(); 和crand = int bgame();. 然后y = x和crand =品牌,你可以在那里工作。 那么你不需要任何参数或任何东西。 另外,不要试图直接定义品牌,只要声明int品牌; 让随机函数(必须用srand播种)为品牌赋值。 您也不必将函数定义为int; 他们会定义自己。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM