在C ++中包含来自.txt文件的文本的问题

时间:2012-03-02 13:46:02

标签: c++ file-io include

我一直在使用主菜单来用于我的文字冒险游戏(我用它来学习,而不是发布它),但我遇到了很大的问题。主菜单有三个选项,播放,显示学分和终止程序。第二个选项,显示学分,需要从credits.txt中获取信息并在屏幕上发布,并在用户按下按钮后,将用户带回主菜单。主菜单是作为头文件(menu.h)构建的,主游戏是在test_project.cpp中。我将在这里给出完整的menu.h和.cpp的一部分,当然还有错误:


Menu.h:

#ifndef MENU_H

#define MENU_H

void displayMenu () {
    int menuItem;
    bool menuRunning = true;

    while ( menuRunning ) {
        cout << "Choose a menu item:\n";

        cout << "1. Play\n2. Credits\n3. Exit\n";
        cin >> menuItem;

        switch ( menuItem ) {
            case 1:
                menuRunning = false;
                break;
            case 2:
                ifstream creditsFile("credits.txt");
                while ( !creditsFile.eof() )
                {
                    string readLine;
                    getline(creditsFile, readLine);
                    cout << readLine;                               
                }

                creditsFile.close();
                break;
            case 3:
                menuRunning = false;
                exit(0);
            default:
                cout << "";
        }

        cout << "\n---\n";
    }
}

#endif

Test_project.cpp:

#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>

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;

以下是错误:

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

2 个答案:

答案 0 :(得分:3)

尝试将案例2重写为:

case 2:{
    ifstream creditsFile("credits.txt");
    while ( !creditsFile.eof() ){
        string readLine;
        getline(creditsFile, readLine);
        cout << readLine;                               
    }
    creditsFile.close();
    break;
}

修改

用以下内容替换Menu.h的内容:

#ifndef MENU_H
#define MENU_H

void displayMenu () {
    int menuItem;
    bool menuRunning = true;

    while ( menuRunning ) {
        cout << "Choose a menu item:\n";
        cout << "1. Play\n2. Credits\n3. Exit\n";
        cin >> menuItem;
        switch ( menuItem ) {
            case 1:{
                menuRunning = false;
                break;
            }
            case 2:{
                ifstream creditsFile("credits.txt");
                while ( !creditsFile.eof() ){
                    string readLine;
                    getline(creditsFile, readLine);
                    cout << readLine;                               
                }
                creditsFile.close();
                break;
            }
            case 3:{
                menuRunning = false;
                exit(0);
            }
            default:
                cout << "";
        }
        cout << "\n---\n";
    }
}
#endif

答案 1 :(得分:1)

尝试将ifstream creditsFile("credits.txt");移到switch语句之前。