C++ 全局和局部变量
我只是想知道为什么C++ 全局和局部变量,c++,variables,C++,Variables,我只是想知道为什么playerHp没有保持它的值,而是每次调用函数时都重置为30。是因为变量是局部变量吗?如果是,我如何使它从函数中变为全局变量? 我是编程新手,所以简单的解释会很好 我也知道这是“意大利面代码”,但如果你有时间和精力,清理它将不胜感激 #include <iostream> #include <string> #include <cstdlib> //RETURN CODES 1=WON 2=RAN AWAY 3=SLAIN 4=global
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;
}
#包括
#包括
#包括
//返回代码1=赢2=逃跑3=被杀4=全球赢5=全球失利
使用名称空间std;
国际音乐手;
int-enemyHp;
字符串enemyName;
int-dmg()
{
int modifier=rand()%10;//0-9
返回15+修饰符;
}
int playerDmg()
{
int modifier1=rand()%10;//0-9
返回15+修饰符1;
}
int enemyDmg()
{
int modifier2=rand()%10;//0-9
返回10+修饰符2;
}
智力战斗(智力玩家、智力敌人、字符串敌人)
{
int constwhile=1;
字符串输入;
cout如果要使用全局变量,则不能有同名的局部变量或参数
以下内容将始终更改局部变量:
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);
问题是全局变量被函数上定义的局部变量所遮蔽
int fight(int playerHp, int enemyHp, string enemyName)
解决问题的一个快速方法是将变量作为引用传递,如果您希望将指定的值保留在函数中
int fight(int &playerHp, int &enemyHp, string &enemyName)
尝试正确缩进代码。并尝试避免全局变量。当值不应为nullptr
时,首选引用而不是指针。
int fight(int playerHp, int enemyHp, string enemyName)
int fight(int &playerHp, int &enemyHp, string &enemyName)