C ++如何使用从一个函数到另一个函数的变量? [英] C++ How do I use variables from one function to another?

查看:233
本文介绍了C ++如何使用从一个函数到另一个函数的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我最近在做一个简单的摇滚,纸,剪刀游戏,我有3个基本功能,一个是用户,一个是机器人,一个是选择哪一个赢得比赛。 / p>

我知道使用 system(cls)并不是最好的选择,但我不是计划在Windows以外使用这个软件。



最终函数 results()需要使用变量 x brand ,但是我似乎无法找到这样做的方法。人们在别处解释的地方,他们解释说它对我来说太高级了。记住我不需要改变他们中的任何一个,我只需要比较它们以确定赢家。



我会在这里向您展示我的代码,以便您可以看看你在处理什么。
请给我评论我可以在这里改进的其他东西。

  #include< iostream> 
#include< stdlib.h>
#include< time.h>
#include< windows.h>

使用namespace std;
int bgame(),ugame(),results();
int main()
{
srand(time(NULL));
cout<欢迎来到RPS!\\\
<<您知道该怎么做。\\\
<<\\\
[ENTER];
cin.ignore();
系统(cls);
ugame();

返回0;
}

int ugame()
{
int x;
cout<<在1-3中键入一个数字:;
cin>> X;
cin.ignore();

if(x == 1)
{
cout <<<\\\
您选择了rock!\\\
<<\\\
[ENTER] ;
cin.ignore();
bgame();
}
else if(x == 2)
{
cout <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<输入];
cin.ignore();
bgame();
}
else if(x == 3)
{
cout <<<\\\
您选择了剪刀!\\\
<<\ n [输入];
cin.ignore();
bgame();
}
else
{
cout<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<¥¥
cin.ignore();
ugame();
系统(cls);
}
返回0;
}


int bgame()
{
int brand = rand()>> 4;

系统(cls);
cout<<机器人现在选择一个项目。\\ n<<\ n<<[ENTER] \ n;
cin.ignore();
brand = rand()%3 + 1;

if(brand == 1)
{
cout <<<<< \ \\ nBot选择了rock!
cin.ignore();
results();
}
else if(brand == 2)
{
cout <<<<< \ \\ nBot选择纸张!
cin.ignore();
results();
}
else if(brand == 3)
{
cout <<<\\\
Bot选择了剪刀!
cin.ignore();
results();
}
else
{
cout<<\\\
Error。;
cin.ignore();
bgame();
系统(cls);
}
返回0;
}

int结果()
{

}


解决方案

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



你需要用你的函数名称来命名这:

  int results(int x,int brand)

你输入一个类型名称和一个变量名称。
例如,这个函数将以2个int作为参数。

  int结果(int x,int品牌)
{
//对你的变量做一些事情
}

你调用你输入的函数:

  results(x,brand); 

如果你愿意,这个链接用图像,例子和更多细节来解释它: http://www.cplusplus.com/doc/tutorial/functions/


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 :)

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.

I know using system("cls") isn't the best way to go, but I'm not planning on using this outside 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.

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()
{

}

解决方案

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.

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/

这篇关于C ++如何使用从一个函数到另一个函数的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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