作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在为我的文字冒险游戏开发一个主菜单(我用它来学习,而不是发布它),但我遇到了一个大问题。主菜单有播放、显示演职员表和终止程序三个选项。第二个选项,显示演职员表,需要从 credits.txt 中获取信息并将其发布在屏幕上,在用户按下按钮后,将用户带回主菜单。主菜单构建为头文件 (menu.h),主游戏位于 test_project.cpp 中。我将在这里给出完整的 menu.h 和一部分 .cpp,当然还有错误:
菜单.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
测试项目.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 重写为:
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
关于c++ - 在 C++ 中包含 .txt 文件中的文本以获得学分的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9534333/
我正在 Facebook 上开发应用程序,我需要能够将我的应用程序集成到其他站点(通过 Facebook JavaScript API 连接到 Facebook)。 在 Facebook 上,我可以使
我正在制作一个学生数据库,其中包含类(class) ID、类(class)名称、每门类(class)的学分、通过/失败声明和成绩。我的目标是让按钮显示总和(成绩)/总和(学分) 这是我编写的代码,它给
我是一名优秀的程序员,十分优秀!