Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue with icluding text from .txt file in C++ for credits
    text
    copied!<p>I've been working on a main menu to use for my text-adventure game (which I use to learn, not to publish it), but I'm having a big problem with it. The main menu has three options, play, show the credits and terminate the program. The second option, showing the credits, needs to get the information out of credits.txt and publish this in the screen, and after the user presses a button, bring the user back to the main menu. The main menu is build as a header file (menu.h) and the main game is in test_project.cpp. I will give the full menu.h and a part of the .cpp here, plus the error of course:</p> <hr> <p><strong>Menu.h:</strong></p> <pre><code>#ifndef MENU_H #define MENU_H void displayMenu () { int menuItem; bool menuRunning = true; while ( menuRunning ) { cout &lt;&lt; "Choose a menu item:\n"; cout &lt;&lt; "1. Play\n2. Credits\n3. Exit\n"; cin &gt;&gt; menuItem; switch ( menuItem ) { case 1: menuRunning = false; break; case 2: ifstream creditsFile("credits.txt"); while ( !creditsFile.eof() ) { string readLine; getline(creditsFile, readLine); cout &lt;&lt; readLine; } creditsFile.close(); break; case 3: menuRunning = false; exit(0); default: cout &lt;&lt; ""; } cout &lt;&lt; "\n---\n"; } } #endif </code></pre> <hr> <p><strong>Test_project.cpp:</strong></p> <pre><code>#include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;string&gt; #include &lt;windows.h&gt; using namespace std; #include "menu.h" int main() { displayMenu(); // This is where the functions from menu.h must be loaded, before the actual game starts. system("TITLE Title of the program comes here"); // This is where the game starts, which loads when the user presses 1. int ab, cd; </code></pre> <hr> <p><strong>And here is the error:</strong></p> <pre><code>menu.h: In function `void displayMenu()': In file included from test_project.cpp:11: menu.h:29: error: jump to case label menu.h:20: error: crosses initialization of `std::ifstream creditsFile' menu.h:32: error: jump to case label menu.h:20: error: crosses initialization of `std::ifstream creditsFile' menu.h:29: warning: destructor needed for `creditsFile' menu.h:29: warning: where case label appears here menu.h:29: warning: (enclose actions of previous case statements requiring destructors in their own scope.) menu.h:32: warning: destructor needed for `creditsFile' menu.h:32: warning: where case label appears here In file included from test_project.cpp:11: menu.h:40:7: warning: no newline at end of file </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload