全局和局部变量

时间:2016-02-18 16:19:15

标签: c++ variables

我只是想知道为什么playerHp不会保持它的值,但每次调用该函数时重置为30。是因为变量是本地的,如果是这样的话,我如何让它从函数中变为全局变量? 我是编程的新手,所以很容易解释。

我也知道这是“意大利面条代码”,但如果你有时间和精力,清理它将非常感激。

#include <iostream>
#include <string>
#include <cstdlib>
//RETURN CODES 1=WON 2=RAN AWAY 3=SLAIN 4=globalWin 5=GlobalLose
using namespace std;

int playerHp;
int enemyHp;
string enemyName;

int dmg()
{
    int modifier = rand() % 10 ; // 0-9
    return 15 + modifier;
}

int playerDmg() 
{
    int modifier1 = rand() % 10 ; // 0-9
    return 15 + modifier1;
}

int enemyDmg()
{
    int modifier2 = rand() % 10 ; // 0-9
    return 10 + modifier2;
}

int fight(int playerHp, int enemyHp, string enemyName)
{
    int constwhile = 1;
    string input;

    cout << "You encountered a ";
    cout << enemyName <<"\n";
    while (constwhile = 1) {
        cout << "What will you do?\n";
        cout << "Options Fight or Flee: " << flush;
        cin >> input;
        cin.ignore();
        cin.get();

        if (input == "Fight" || input == "fight") {
            int damageDone = playerDmg();
            int finalEnemyHp = enemyHp - damageDone;
            cout << "You did ";
            cout << damageDone;
            cout << " damage" << "\n";

            enemyHp = finalEnemyHp;
            if (enemyHp < 0) {
                cout << "Congratulations! You won!"<<"\n";
                return 1;
            }
            cout << "The monster now has ";
            cout << enemyHp;
            cout << " health"<<"\n";
        } else if (input == "Flee" || input == "flee") {
            cout << "You ran away...";
            return 2;
        } else {
            cout<<"Invalid Input"<<"\n";
        }

        cout << "The enemy fights back!"<< "\n";
        int damageDone = enemyDmg();
        int finalPlayerHp = playerHp - damageDone;
        cout << "It did ";
        cout << damageDone;
        cout << " damage" << "\n";
        playerHp = finalPlayerHp;
        if (playerHp < 0) {
            cout << "You have been slain...";
            return 3;
        }
        cout << "You now have ";
        cout << playerHp;
        cout << " health"<<"\n";
    }
}

int main()
{
    int playerHp = 30;
    while(playerHp > 1) {
        int fightOutcome = fight(playerHp, rand() % 30, "monster");
        if (fightOutcome == 3) {
            return 5;
        } else if (fightOutcome == 2) {
            return 5;
        }
     }  
     return 4;
}

2 个答案:

答案 0 :(得分:1)

问题是你的全局变量是在函数上定义的本地阴影

  int fight(int playerHp, int enemyHp, string enemyName)

解决问题的一种快速方法是将变量作为参考传递,如果你想让你保持在函数内部分配的值

  int fight(int &playerHp, int &enemyHp, string &enemyName)

答案 1 :(得分:0)

如果要使用全局变量,则必须没有具有相同名称的局部变量或参数。

以下内容将始终更改局部变量:

int myVar = 0;

void foo(int myVar)
{
  myVar = 100;
}

int main()
{
  foo(myVar);
  cout << myVar;//0
  return 0;
}

现在您可以接受指针而不是值,也可以完全删除参数。但我真的不喜欢全球污染,所以我更喜欢指针:

带指针:

int myVar = 0;

void foo(int* myVar)
{
  *myVar = 100;
}

int main()
{
  foo(&myVar);
  cout << myVar;//100
  return 0;
}

没有指针:

int myVar = 0;

void foo()
{
  myVar = 100;
}

int main()
{
  foo(myVar);
  cout << myVar;//100
  return 0;
}