4
#include <iostream>
#include <strings.h>

using namespace std;

void encyclopedia()
{
int choice;
int choice2;

system("CLS");
cout << "Content Menu\n\n" 
     << "1. Gore\n\n"
     << "2. Wilson\n\n"
     << "3. Costa\n\n"
     << "Selection: ";
cin >> choice;
if (choice == 1)
{
           system("CLS");
           cout << "Al Gore's Book Summary of:\n\n"
                << "1. Introduction\n\n"
                << "2. \n\n"
                << "3. \n\n";
           cin >> choice2;
           if (choice2 == 1)
           {
                       system("CLS");
                      cout << "text here\n\n";
                      cout << "Press enter to continue\n";
                      cin.get();
                      encyclopedia();               
           }
           else if (choice2 == 2)
           {
                system("CLS");
                      cout << "text here\n\n";
                      cout << "Press enter to continue\n";
                      cin.get();
                      encyclopedia();               
           }
           else if (choice2 == 3)
           {
                system("CLS");
                      cout << "text here\n\n";
                      cout << "Press enter to continue\n";
                      cin.get();
                      encyclopedia();               
           }  
}
if (choice == 2)
{
           system("CLS");
           cout << "Wilson's Book Summary of:\n\n"
                << "1. Introduction\n\n"
                << "2. \n\n"
                << "3. \n\n";
           cin >> choice2;
           if (choice2 == 1)
           {
                       system("CLS");
                      cout << "text here\n\n";
                      cout << "Press enter to continue\n";
                      cin.get();
                      encyclopedia();               
           }
           else if (choice2 == 2)
           {
                system("CLS");
                      cout << "text here\n\n";
                      cout << "Press enter to continue\n";
                      cin.get();
                      encyclopedia();               
           }
           else if (choice2 == 3)
           {
                system("CLS");
                      cout << "text here\n\n";
                      cout << "Press enter to continue\n";
                      cin.get();
                      encyclopedia();               
           }  
}
if (choice == 3)
{
           system("CLS");
           cout << "Rebeca Costa's Book Summary of:\n\n"
                << "1. Introduction\n\n"
                << "2. \n\n"
                << "3. \n\n";
           cin >> choice;
           if (choice2 == 1)
           {
                       system("CLS");
                       cout << "text here\n\n";
                       cout << "Press enter to continue\n";
                       cin.get();
                       encyclopedia();               
           }
           else if (choice2 == 2)
           {
                       system("CLS");
                       cout << "text here\n\n";
                       cout << "Press enter to continue\n";
                       cin.get();
                       encyclopedia();               
           }
           else if (choice2 == 3)
           {
                      system("CLS");
                      cout << "text here\n\n";
                      cout << "Press enter to continue\n";
                      cin.get();
                      encyclopedia();               
           }  
}
}

int main()
{
cout << "2013 Written Task #2\n\nBy: Skye Leis\n\n";
cout << "Press enter to continue\n";
cin.get();
encyclopedia();   
}

当我的第一个 cin.get() 工作时,我无法在 encyclopedia() 工作之前让 cin.get() 工作。当它运行时,第一个屏幕工作,然后内容菜单工作,子菜单工作,但在显示实际文本的部分上,它不会等待输入键,然后重新启动百科全书功能。

4

3 回答 3

1

cin适用于格式化输入。这意味着它会一直读下去,直到找到一个空格或一个新行。

问题是当你这样做

cin >> choice2;

当您输入一个数字并按回车键时,cin将读取到一个blank space. 这意味着newline(from key enter) 仍然在那里。您cin.get将阅读该换行符并继续。

此外,如果我输入两个用空格分隔的数字,您的实现将采用第二个数字并将其用于任何下一个菜单输入。

为了确保在您继续下一个菜单项之前读取输入中留下的任何内容,您可以使用getline()

string garbage;
cin >> choice2;
getline(cin, garbage);  // The will take care of any extra inputs.
于 2013-04-04T16:36:37.730 回答
0

它读取 last /n 并执行以下操作:

cin.clear(); cin.get();

或者只写 cin.get(); 两次

于 2014-01-02T12:25:35.417 回答
0

会发生什么仍然有剩菜(endlines)所以你可能会在用户输入后得到一些其他不需要的字符,请尝试添加:

cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

辛后

于 2013-04-04T16:28:36.163 回答