【问题标题】:C++ How to select option from the menu after typing a string before the menu?C ++如何在菜单前输入字符串后从菜单中选择选项?
【发布时间】:2014-01-20 06:19:45
【问题描述】:

我是 C++ 新手,我正在开发一个程序,学生可以在其中键入他们的主题代码,然后会有一个菜单可供选择。

问题是,在键入并输入主题代码后,将出现菜单及其选项,但您退出系统而不是选择继续的选项..

这是我试图让它们工作的部分的代码

    void ProcessStudent(char option, char subjCode)
    {
        fstream binFile;
const char *fileName;

cout << "Welcome to Priya's UOW Student File Processing\n" << endl;
cout << "Enter the module: ";
cin >> subjCode;

cout << "\n1. Creation of binary file\n";
cout << "2. Append a record\n";
cout << "3. Update a record\n";
cout << "4. Delete a record\n";
cout << "5. Produce final file (text file)\n";
cout << "9. Quit\n";

cout << "Your option: ";
cin >> option;

cout << "\n" <<setw(60)<<setfill('-')<<'-'<<endl;

switch(option)
{
    case '1':
        CreateBinary(binFile,subjCode,fileName);
    break;

    case '2':
        AppendRec(binFile,option,fileName);
    break;

    case '3':
        UpdateRec(binFile,option,fileName);
    break;

    case '4':
        DeleteRec(binFile,option,fileName);
    break;

    case '5':
        FinalFile(binFile,option,fileName);
    break;

    case '9':
        MainMenu(option);
}
    }

    void CreateBinary(fstream& binFile, char subjCode, const char *fileName)
    {
    //    Student s;

srand(time(NULL));

        binFile.open(fileName, ios::out | ios::binary);

    //    binFile >> s.id;
    //    binFile.getline(s.name,MAX);

if(!binFile)
{
    cout << fileName << " opened for creation failed" << endl;
    exit(-1);
}

cout << "Begin creation of " << fileName << endl;

fstream txtFile;
txtFile.open(fileName, ios::in);

if(!txtFile)
{
    cout << fileName << " opened for reading failed" << endl;
    exit(-1);
}

cout << "Text file " << fileName << "opened for reading" << endl;



binFile.close();
txtFile.close();

cout << "Binary file " << fileName << " created" << endl;
cout << "Text file " << fileName << " closed for reading" << endl;
}

我要问的是,我输入主题代码或任何其他字符串后如何选择选项?我错过了代码中的任何内容吗?

希望你们明白我在问什么,如果没有,请告诉我。

感谢您提供任何帮助。非常感谢:)。

【问题讨论】:

  • 哪个编译器,你是怎么编译的,哪个操作系统?并展示更多代码,并说明你是如何调试它的。
  • 我使用 Quincy 编译。遗憾的是,由于声誉问题,我无法截取该程序的屏幕截图……抱歉。
  • Quincy 不是编译器(只是一个 IDE)。显然是在使用Gccgdb;你可以在命令行上编译(你应该做几次)

标签: c++ string selection iostream submenu


【解决方案1】:

您需要刷新cout 流:

 cout << "Enter the module: " << flush;

以后

 cout << "Your option: "  << flush;

最后用cout &lt;&lt; endl替换换行符的输出

你应该编译所有警告和调试信息(例如g++ -Wall -g)并学习如何使用调试器(也许gdb)。也许某些功能,例如UpdateRec 崩溃了……

【讨论】:

  • 刷新 cout 流 是什么意思?
  • GIYF:参见std::ostream 中的flush 成员函数和flush 操纵器。
猜你喜欢
  • 2017-01-26
  • 2017-05-03
  • 2018-06-27
  • 2014-04-16
  • 2011-02-18
  • 1970-01-01
  • 2020-09-18
  • 2016-01-28
  • 2014-09-08
相关资源
最近更新 更多