Cin.get()第二次不使用它

时间:2013-04-04 16:22:13

标签: c++ menu cin

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

3 个答案:

答案 0 :(得分:1)

cin适用于格式化输入。这意味着它会在找到空格或新行之前进行读取。

问题在于你做什么

cin >> choice2;

当您输入数字并按Enter键时,cin将读取到blank space。这意味着newline(来自键enter)仍在那里。您的cin.get会读取该换行符并继续。

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

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

string garbage;
cin >> choice2;
getline(cin, garbage);  // The will take care of any extra inputs.

答案 1 :(得分:0)

会发生什么事情仍有剩余部分(终结点),因此您可能会在用户输入后获得其他一些不需要的字符,请尝试添加:

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

答案 2 :(得分:0)

它读取最后一个/ n 并做:

  

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

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