在菜单功能中调用和显示文本文件
Summoning and Displaying a text file, inside a menu function
我是 C++ 的新手,我的任务非常艰巨。我必须创建一个菜单(我在其中挣扎得很厉害),不仅如此,我还应该阅读和显示一个文本文件。到目前为止,我使用的唯一方法只显示文本文件的第一行。你能帮我吗?提前致谢。
添加到第一个案例中的中断功能也使循环成为无限的文本墙!不知道怎么办..
// Assignment 1.cpp : Tally Ho Generator
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int runMenu;
int main()
{
int menuInput;
bool menu = true;
ifstream inFile;
string ABOUT;
// Menu Interface
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout << " The Tally Ho Probability Generator (MCD4720_Assignment 1)\n";
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout << " [1] End Testing the Program\n";
cout << " [2] Display About Information\n";
cout << " [3] Read and store data from files\n";
cout << " [4] Generate a Dice Tally Table\n";
cout << " [5] Save Tally Statistics to a file\n";
cout << " [6] Load Tally Statistics from a file\n";
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout << "Which Option would you like (1-6)\n";
cin >> runMenu;
// Menu Input Function
while (menu != false) {
switch (runMenu)
{
case 1:
{
inFile.open("C:\Users\William\Documents\MCD Assignment 4720 1\TallyAbout.txt");
while (getline(inFile, ABOUT)) {
cout << ABOUT << endl;
inFile.close();
}
break;
}
case 2:
{}
case 3:
{}
case 4:
{}
case 5:
{}
case 6:
{}
default: {
cout << " Input unrecognized, please choose again \n";
cin >> runMenu;
break;
}
}
}
cin.ignore();
cin.get();
return 1;
}
试试这个:
case 1:
{
inFile.open("C:\Users\William\Documents\MCD Assignment 4720 1\TallyAbout.txt");
while (getline(inFile, ABOUT)) {
cout << ABOUT << endl;
}
inFile.close();
break;
}
您正在阅读第一行后关闭文件
现在的区别是您在阅读所有行后关闭文件
So far, the only method I used only displays the first lines of the text file
那是因为你读完第一行就关闭了文件:
while (getline(inFile, ABOUT)) {
cout << ABOUT << endl;
inFile.close(); // <------ here !!
}
您不需要明确关闭文件。该文件在 inFile
.
的析构函数中关闭
The break function when added to the first case also makes the loop an infinite wall of texts! I dont know what to do..
那是因为break
跳出了开关。不会处理其他情况。特别是,您要求用户输入的 default
案例将不会执行。实际上你永远不会修改 menu
的值并且 while(menu != false)
是一个无限循环。将任何情况下必须完成的部分移出开关:
while (menu != false) {
switch (runMenu)
{
case 1:
{
inFile.open("C:\Users\William\Documents\MCD Assignment 4720 1\TallyAbout.txt");
while (getline(inFile, ABOUT)) {
cout << ABOUT << endl;
inFile.close();
}
break;
}
//...
default: {
cout << " Input unrecognized, please choose again \n";
break;
}
}
cin >> runMenu;
// put logic here, for example:
menu = (runMenu == 1);
}
您应该相应地修改输出。目前它说“输入无法识别,请重新选择”,但如果我理解正确,用户必须在每次迭代中选择继续或不继续。 menu
和 runMenu
使用一个变量而不是两个变量会更不容易出错。
我是 C++ 的新手,我的任务非常艰巨。我必须创建一个菜单(我在其中挣扎得很厉害),不仅如此,我还应该阅读和显示一个文本文件。到目前为止,我使用的唯一方法只显示文本文件的第一行。你能帮我吗?提前致谢。
添加到第一个案例中的中断功能也使循环成为无限的文本墙!不知道怎么办..
// Assignment 1.cpp : Tally Ho Generator
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int runMenu;
int main()
{
int menuInput;
bool menu = true;
ifstream inFile;
string ABOUT;
// Menu Interface
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout << " The Tally Ho Probability Generator (MCD4720_Assignment 1)\n";
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout << " [1] End Testing the Program\n";
cout << " [2] Display About Information\n";
cout << " [3] Read and store data from files\n";
cout << " [4] Generate a Dice Tally Table\n";
cout << " [5] Save Tally Statistics to a file\n";
cout << " [6] Load Tally Statistics from a file\n";
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout << "Which Option would you like (1-6)\n";
cin >> runMenu;
// Menu Input Function
while (menu != false) {
switch (runMenu)
{
case 1:
{
inFile.open("C:\Users\William\Documents\MCD Assignment 4720 1\TallyAbout.txt");
while (getline(inFile, ABOUT)) {
cout << ABOUT << endl;
inFile.close();
}
break;
}
case 2:
{}
case 3:
{}
case 4:
{}
case 5:
{}
case 6:
{}
default: {
cout << " Input unrecognized, please choose again \n";
cin >> runMenu;
break;
}
}
}
cin.ignore();
cin.get();
return 1;
}
试试这个:
case 1:
{
inFile.open("C:\Users\William\Documents\MCD Assignment 4720 1\TallyAbout.txt");
while (getline(inFile, ABOUT)) {
cout << ABOUT << endl;
}
inFile.close();
break;
}
您正在阅读第一行后关闭文件 现在的区别是您在阅读所有行后关闭文件
So far, the only method I used only displays the first lines of the text file
那是因为你读完第一行就关闭了文件:
while (getline(inFile, ABOUT)) { cout << ABOUT << endl; inFile.close(); // <------ here !! }
您不需要明确关闭文件。该文件在 inFile
.
The break function when added to the first case also makes the loop an infinite wall of texts! I dont know what to do..
那是因为break
跳出了开关。不会处理其他情况。特别是,您要求用户输入的 default
案例将不会执行。实际上你永远不会修改 menu
的值并且 while(menu != false)
是一个无限循环。将任何情况下必须完成的部分移出开关:
while (menu != false) {
switch (runMenu)
{
case 1:
{
inFile.open("C:\Users\William\Documents\MCD Assignment 4720 1\TallyAbout.txt");
while (getline(inFile, ABOUT)) {
cout << ABOUT << endl;
inFile.close();
}
break;
}
//...
default: {
cout << " Input unrecognized, please choose again \n";
break;
}
}
cin >> runMenu;
// put logic here, for example:
menu = (runMenu == 1);
}
您应该相应地修改输出。目前它说“输入无法识别,请重新选择”,但如果我理解正确,用户必须在每次迭代中选择继续或不继续。 menu
和 runMenu
使用一个变量而不是两个变量会更不容易出错。