首页 > 解决方案 > 在 switch case 中读取 .txt 文件

问题描述

我的任务要求我创建一个 noughts and crosss 程序。我已经设法使所有功能正常工作,但我希望添加写入文本文件的功能,并在开关盒中调用所述文件(开关盒用于主菜单)。写入文件不是问题,但是在读取文件时我得到“E0546 控制转移绕过初始化:”。

我尝试包含允许程序运行但输入未被识别为正确的 switch case {}。

do    //Do at least once (do loop) - contains the 'game loop'
{

    mainMenu();

    switch (menuChoice)
    {
    case 1:
        cout << "\t\t\t\t\t" << "Please enter your name" << endl;
        cout << "\t\t\t\t\t" << ">>" << endl;
        cin >> playerNames[1];
        init();                                                //Clear the grid and set to player one
        do {
            playerNames[2] = "Computer";
            system("color 03");                                //Change text colour to blue
            system("cls");                                     //Clear the screen
            DisplayGrid();                                     //Display grid to screen
            DisplayInput();                                    //Display input options relevant to single player
            CheckInput();                                      //Check player input                                    
            CheckStatus();                                     //Checks game status
            easyAI();                                          //Computer turn that randomly fills a square
            CheckStatus();
            system("cls");

        } while (toupper(input) != 'F' && toupper(input) != 'M');                              //Checks the player hasn't quit or returned to menu



    case 2:
        cout << "\t\t\t\t\t" << "Player one please enter your name" << endl;
        cout << "\t\t\t\t\t" << " >>";
        cin >> playerNames[1];
        cout << "\t\t\t\t\t" << "Player two please enter your name" << endl;
        cout << "\t\t\t\t\t" << " >>";
        cin >> playerNames[2];
        init();                                                //Clear the grid and set to player one
        do {
            system("color 03");                                //Change text colour to blue
            system("cls");                                     //Clear the screen
            DisplayGrid();                                     //Display grid to screen
            DisplayInput();                                    //Display input options relevant to multiplayer
            CheckInput();                                      //Check player input
            CheckStatus();                                     //Checks game status
            system("cls");

        } while (toupper(input) != 'F' && toupper(input) != 'M');                              //Checks the player hasn't quit or returned to menu
        break;


    case 3:
        howToplay();
        break;

    case 4:
        runProg = false;
        return 0;
        break;

    case 5:


        ifstream reader("score.txt");                       
        if (!reader)
        {
            cout << "Error opening file" << endl;
        }




    default:
        cout << "Please use the correct input from the available choices" << endl;
        system("pause");
    }
} while (toupper(input) != 'F');

return 0;

system("pause");

}

标签: c++visual-c++

解决方案


case除非在块周围添加大括号,否则不能在块的顶层声明变量,例如:

case 5:
{ // <-- add this!
    ifstream reader("score.txt");
    if (!reader)
    {
        cout << "Error opening file" << endl;
    }
} // <-- add this!

并且不要忘记break每个case块上的声明。您的case 1case 5块上缺少它。


推荐阅读