2013-04-04 12 views
3
#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()的。運行時,第一個屏幕工作,然後內容菜單工作,子菜單工作,但在顯示實際文本的部分,它不會等待輸入密鑰,然後重新啓動百科全書式功能。Cin.get()不工作我第二次使用它

回答

0

發生的是還有剩菜(endlines),所以你可能會在用戶類型後,可以得到一些其他不需要的字符,試着加入:

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

後CIN

1

cin作品上被格式化的輸入。這意味着它會讀取,直到找到空白區域或新行。

的問題是,當你做

cin >> choice2; 

當你輸入一個號碼,然後按回車,cin將讀取高達一blank space。這意味着newline(來自鍵enter)仍然在那裏。您的cin.get將讀取該新行字符並繼續。

此外,如果我輸入由空格分隔的兩個數字,您的實現將採用第二個數字,並將其用於任何下一個菜單輸入。

爲了確保您繼續下一菜單項,然後才能使用任何東西留在讀取輸入getline()

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

它讀取最後/ N 做:

CIN 。明確(); cin.get();

或者只寫cin.get();兩次