【问题标题】:C++ having issue with my variable going back to zeroC++ 有我的变量变回零的问题
【发布时间】:2015-03-07 18:29:36
【问题描述】:

在程序完成后用户得分没有回到 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;,我不会感到惊讶。
  • 是的,你的代码真的很糟糕......清理它。你想要什么?你是什​​么意思你希望程序完成后分数为0?分数是程序完成时的分数。您的意思是在每次“播放”后您都希望重置所有内容吗?

标签: c++ curly-braces


【解决方案1】:

你应该重置变量:

userscore = 0;

在 do 语句的开头或 while 语句的结尾之前。这将解决您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多