0

OK, so Im close to wrapping up my work on this simple C++ Game and now its just a matter of connecting the classes together so to speak so that when a certain event happens, it would start running code from another class.

However my menu is a particularly stubborn problem. Im trying to set it so when the user enters option "1" as their choice it starts running the map code in another C++ file but its having none of it.

It gives me the following 3 errors whenever I try run the code.

1   IntelliSense: transfer of control bypasses initialization of:
        variable "A" (declared at line 37)  e:\C++ - Copy\Menu_3\Menu_3\Menu.cpp    27  3   Menu_3

2   IntelliSense: class "Map" has no member "standby"   e:\C++ - Copy\Menu_3\Menu_3\Menu.cpp    38  6   Menu_3

3   IntelliSense: return value type does not match the function type    e:\C++ - Copy\Menu_3\Menu_3\Menu.cpp    46  9   Menu_3

This is my menu code:

#pragma once
#include "Map.h"
#include "Menu.h"
#include <iostream>
#include <string>
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using std::string;

Menu::Menu()
{
}

void menu()
{
    int choice;

    bool gameOn = true;
    while (gameOn != false){
        cout << " 1 - Play\n";
        cout << " 2 - Quit\n";

        cin >> choice;

        switch (choice)
        {
        case 1:
            cout << "Your adventure starts now!\n";
            system("PAUSE");

            cout << "Welcome to ATAG brave adventurer!\n";
            system("PAUSE");

            //Starts the next part of the game, in this case, the map
            Map A;
            A.standby();

        //Ends the game
        case 2:
            system("PAUSE");
            exit(0);
        }
    }
    return 0;
}

Menu::~Menu()
{
}

FYI, I'm using Visual Studio 2013 in case that helps. Anyone got any advice because this is probably the last thing as far as code goes that's stopping me from finishing the project, and I'd be grateful if someone can tell me how to make those 3 errors go away.

Rakete1111
  • 47,013
  • 16
  • 123
  • 162
Thane_Mantis
  • 1
  • 1
  • 5
  • Why does your `void menu()` end up `return 0;` – Jonas Mar 31 '17 at 11:51
  • Also, what is the point of all those `using` statements when you also do `using namespace std;`? – Jonas Mar 31 '17 at 11:52
  • Because the programs bug catching thing mentioned something about a return value or whatever so I just added those to shut it up. As for the using namespace thing, I can't remember why I added those, their just there. – Thane_Mantis Mar 31 '17 at 12:59

1 Answers1

1

Here is a good explanation, why you are having this problem -> Why can't variables be declared in a switch statement?

How to fix - simply enclose case body in curly braces. This limits scope of variable A to those curly braces (not whole switch).

case 1: {
    cout << "Your adventure starts now!\n";
    system("PAUSE");
    cout << "Welcome to ATAG brave adventurer!\n";
    system("PAUSE");
    Map A;
    A.standby();
} break;
Community
  • 1
  • 1
Starl1ght
  • 4,422
  • 1
  • 21
  • 49
  • 1
    You really should flag/vote to close as a dupe if this is answered somewhere else. – NathanOliver Mar 31 '17 at 11:53
  • Its not a duplicate though. Im not sure what that other question is about, but mine is not about declaring variables but making my menu run the next .cpp file of code when the correct option is entered on the menu. – Thane_Mantis Mar 31 '17 at 12:45
  • OK, I've got 2 errors now. They say the following. Error 1 error LNK2019: unresolved external symbol "public: void _thiscall Class2::standby(void)" (?standby@Class2@@QAEXXZ) referenced in function _main E:\C++ - Copy\ATAG2_\ATAG2_\Class1.obj ATAG2 Error 2 error LNK1120: 1 unresolved externals E:\C++ - Copy\ATAG2_\Debug\ATAG2_.exe 1 1 ATAG2_ Anyone got any solutions to solve them? – Thane_Mantis Mar 31 '17 at 12:54