C++ 有问题,我的变量回到零
C++ having issue with my variable going back to zero
在程序完成后,用户评分的值没有回到 0,我想我在某处弄乱了括号,我们将不胜感激。
此代码的重点是制作一个简单的基于文本的 C++ 程序,该程序将具有一系列三个菜单,并在每个菜单后添加到分数中
int main()
{
//declare and initialize variables
int menuChoice = 0, highscore = 0, race = 0, weapon = 0, userscore = 0, enemy = 0, randomscore = 0, enemydamage = 0, enemydamage1 = 0, enemydamage2 = 0 ;
string username, username1 ;
srand(time(NULL)) ;
ifstream infile ;
ofstream outfile ;
//intro
cout<<"------------------Welcome to the Arena Get ready to Fight------------------\n\n" ;
//prompt for username
cout<<"Please tell me your First Name: ";
cin>>username ;
cout<<"\n\n" ;
//display menu
do
{
cout<<"Hello "<<username<<endl ;
cout<<"Please choose from the following menu:\n";
cout<<"1) See Rules\n2) Play Game\n3) Quit\n";
//prompt for menu choice
cout<<"Enter your choice here: ";
cin>>menuChoice;
//switch structure
switch(menuChoice)
{
case 1:
//display rules
cout<<"Welcome to the Arena "<<username<<"you will be given 3 choices which will let you choose your race, weapon, and enemy. Eacho choice has its own point value which might be random or a fixed number the goal is to surive and not have you health hit 0\n\n" ;
break;
case 2:
//Game Intro
cout<<"Welcome "<<username<<" time to fight!!!!!!\n" ;
randomscore = rand()%100 + 1 ;
do
{
//display menu
cout<<"Please choose a Race\n";
cout<<"1) Elf\n2) Dwarf\n3) HUman\n";
//Prompt user to choose race
cout<<"Enter your choice here: ";
cin>>race;
if (race < 1 || race > 3)
{
cout<<"Invalid. Must be a Valid Menu Choice between 1-3.\n\n";
}
}
while(race < 1 || race > 3) ;
{
if ( race == 1)
{
userscore += 100 ;
}
else if (race == 2 )
{
userscore += 200 ;
}
else {
userscore += randomscore;
}
}
{
//display userscore
cout<<"Nice Choice "<<username<<" your current score is "<<userscore<<endl;
}
do
{
//menu 2
cout<<"Please choose a Weapon\n";
cout<<"1) Sword\n2) Axe\n3) Bow\n";
//prompt user to choose a weapon
cout<<"Enter your choice here: ";
cin>>weapon;
if(weapon < 1 || weapon > 3)
{
cout<<"Invalid. Must be a Valid Menu Choice between 1-3.\n\n";
}
}
while(weapon < 1 || weapon > 3) ;
{
if (weapon == 1)
{
userscore += 60 ;
}
else if (weapon == 2)
{
userscore += randomscore ;
}
else
{
(userscore += 100) ;
}
}
{
//display userscore
cout<<"Hope that can help you "<<username<<" your current score is "<<userscore<<endl;
}
{
//menu 3
enemydamage = rand()%100 + 1 ;
enemydamage1 = rand()%200 + 1 ;
enemydamage2 = rand()%50 + 30 ;
}
do
{
cout<<"Please choose a Enemy\n";
cout<<"1) Orc\n2) Troll\n3) Goblin\n";
//prompt user to choose a enemy
cout<<"Enter your choice here: ";
cin>>enemy;
if(enemy < 1 || enemy > 3)
{
cout<<"Invalid. Must be a Valid Menu Choice between 1-3.\n\n";
}
}
while(enemy < 1 || enemy > 3) ;
{
if ( enemy == 1 )
{
userscore -= enemydamage ;
}
else if ( enemy == 2 )
{
userscore -= enemydamage1 ;
}
else
{
userscore -= enemydamage2 ;
}
{
cout<<"Its time "<<username<<" to show you your score\n\n";
}
}
{
infile.open("highscore.txt", ios::in);
infile>>username1>>highscore ;
infile.close() ;
}
{
if
(userscore < 0 )
{
//display you are dead try again
cout<<"Your Are dead try again\n" ;
}
else if (userscore < highscore)
{
//display userscore
cout<<"Nice Job!! "<<username<<" but you didn't beat the High Score, your final score is "<<userscore<<endl;
}
else
{
outfile.open("highscore.txt", ios::out) ;
outfile<<username<<"\t"<<userscore ;
outfile.close();
cout<<"Nice Job Your Final Score is "<<userscore<<" and that beats the High Score\n\n";
}
}
break;
case 3: //Dislpay program Exit
cout<<"Have a Nice Day!!\n\n" ;
break;
default:
cout<<"Invalid. You must choose 1 - 3 from the menu.\n\n" ;
}
}
while(menuChoice !=3) ;
return 0;
}
您应该重置变量:
userscore = 0;
在 do 语句开始或 while 语句结束之前。这将解决您的问题。
在程序完成后,用户评分的值没有回到 0,我想我在某处弄乱了括号,我们将不胜感激。
此代码的重点是制作一个简单的基于文本的 C++ 程序,该程序将具有一系列三个菜单,并在每个菜单后添加到分数中
int main()
{
//declare and initialize variables
int menuChoice = 0, highscore = 0, race = 0, weapon = 0, userscore = 0, enemy = 0, randomscore = 0, enemydamage = 0, enemydamage1 = 0, enemydamage2 = 0 ;
string username, username1 ;
srand(time(NULL)) ;
ifstream infile ;
ofstream outfile ;
//intro
cout<<"------------------Welcome to the Arena Get ready to Fight------------------\n\n" ;
//prompt for username
cout<<"Please tell me your First Name: ";
cin>>username ;
cout<<"\n\n" ;
//display menu
do
{
cout<<"Hello "<<username<<endl ;
cout<<"Please choose from the following menu:\n";
cout<<"1) See Rules\n2) Play Game\n3) Quit\n";
//prompt for menu choice
cout<<"Enter your choice here: ";
cin>>menuChoice;
//switch structure
switch(menuChoice)
{
case 1:
//display rules
cout<<"Welcome to the Arena "<<username<<"you will be given 3 choices which will let you choose your race, weapon, and enemy. Eacho choice has its own point value which might be random or a fixed number the goal is to surive and not have you health hit 0\n\n" ;
break;
case 2:
//Game Intro
cout<<"Welcome "<<username<<" time to fight!!!!!!\n" ;
randomscore = rand()%100 + 1 ;
do
{
//display menu
cout<<"Please choose a Race\n";
cout<<"1) Elf\n2) Dwarf\n3) HUman\n";
//Prompt user to choose race
cout<<"Enter your choice here: ";
cin>>race;
if (race < 1 || race > 3)
{
cout<<"Invalid. Must be a Valid Menu Choice between 1-3.\n\n";
}
}
while(race < 1 || race > 3) ;
{
if ( race == 1)
{
userscore += 100 ;
}
else if (race == 2 )
{
userscore += 200 ;
}
else {
userscore += randomscore;
}
}
{
//display userscore
cout<<"Nice Choice "<<username<<" your current score is "<<userscore<<endl;
}
do
{
//menu 2
cout<<"Please choose a Weapon\n";
cout<<"1) Sword\n2) Axe\n3) Bow\n";
//prompt user to choose a weapon
cout<<"Enter your choice here: ";
cin>>weapon;
if(weapon < 1 || weapon > 3)
{
cout<<"Invalid. Must be a Valid Menu Choice between 1-3.\n\n";
}
}
while(weapon < 1 || weapon > 3) ;
{
if (weapon == 1)
{
userscore += 60 ;
}
else if (weapon == 2)
{
userscore += randomscore ;
}
else
{
(userscore += 100) ;
}
}
{
//display userscore
cout<<"Hope that can help you "<<username<<" your current score is "<<userscore<<endl;
}
{
//menu 3
enemydamage = rand()%100 + 1 ;
enemydamage1 = rand()%200 + 1 ;
enemydamage2 = rand()%50 + 30 ;
}
do
{
cout<<"Please choose a Enemy\n";
cout<<"1) Orc\n2) Troll\n3) Goblin\n";
//prompt user to choose a enemy
cout<<"Enter your choice here: ";
cin>>enemy;
if(enemy < 1 || enemy > 3)
{
cout<<"Invalid. Must be a Valid Menu Choice between 1-3.\n\n";
}
}
while(enemy < 1 || enemy > 3) ;
{
if ( enemy == 1 )
{
userscore -= enemydamage ;
}
else if ( enemy == 2 )
{
userscore -= enemydamage1 ;
}
else
{
userscore -= enemydamage2 ;
}
{
cout<<"Its time "<<username<<" to show you your score\n\n";
}
}
{
infile.open("highscore.txt", ios::in);
infile>>username1>>highscore ;
infile.close() ;
}
{
if
(userscore < 0 )
{
//display you are dead try again
cout<<"Your Are dead try again\n" ;
}
else if (userscore < highscore)
{
//display userscore
cout<<"Nice Job!! "<<username<<" but you didn't beat the High Score, your final score is "<<userscore<<endl;
}
else
{
outfile.open("highscore.txt", ios::out) ;
outfile<<username<<"\t"<<userscore ;
outfile.close();
cout<<"Nice Job Your Final Score is "<<userscore<<" and that beats the High Score\n\n";
}
}
break;
case 3: //Dislpay program Exit
cout<<"Have a Nice Day!!\n\n" ;
break;
default:
cout<<"Invalid. You must choose 1 - 3 from the menu.\n\n" ;
}
}
while(menuChoice !=3) ;
return 0;
}
您应该重置变量:
userscore = 0;
在 do 语句开始或 while 语句结束之前。这将解决您的问题。