-1

My assignment requires me to create a noughts and crosses program. I have managed to get all features working but I wish to add the ability to write to a text file, and call upon said file within a switch case (the switch case is used for the main menu). Writing the file is not a problem however when it comes to reading it I get 'E0546 transfer of control bypasses initialization of:'.

I have tried containing the switch case {} which allows the program to run but the input is not recognised as being correct.

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");

}
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Chris G
  • 3
  • 4

1 Answers1

2

You can't declare variables at the top level of a case block unless you add braces around the block, eg:

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

And don't forget the break statement on each of your case blocks, too. It is missing on your case 1 and case 5 blocks.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thanks for the reply. Cant believe I missed that out! I have added it in and it now seems to recognise it as a valid input, however outputs nothing to the console. The file is in the correct directory so now unsure once more :) – Chris G Oct 27 '19 at 22:45
  • You only output something if the file isn't opened properly. If it is, then it won't output anything to the console. –  Oct 27 '19 at 22:52
  • Thanks for your input guys. I can see now that the text file was opened but not outputted to console! Also added a line to close the file once completed. :) – Chris G Oct 28 '19 at 09:14