2010-09-16 57 views
2

這是一個很奇怪的問題,當我的程序會詢問用戶的地址,而不是等待輸入跳過函數getline(),它似乎跳過函數getline()函數完全方案能夠記錄用戶輸入

Answerinput: 

cout << "would you like to add another entry to the archive? (Y/N):"; 

cin >> answer; 

cout << endl; 
cout << endl; 

answer = toupper(answer); 


switch(answer) 
    { 
    case 'Y': 
     Entrynumber++; 

     cout << "began record number " << Entrynumber << "+ 1." << endl; 

     cout << "Enter the last name of the person to be entered" << endl; 

     cin >> stringentry; 
     cout << endl; 

     stringlength = stringentry.length(); 

     strcpy(Record[Entrynumber].Last_Name, stringentry.c_str()); 


     Record[Entrynumber].Last_Name[stringlength] = '*'; 



     cout << "Enter the first name of the person" << endl; 

     cin >> stringentry; 
     cout << endl; 

     stringlength = stringentry.length(); 

     strcpy(Record[Entrynumber].First_Name, stringentry.c_str()); 

     Record[Entrynumber].First_Name[stringlength] = '*'; 

     cout << "Enter the SSN of the person" << endl; 
     cin >> Record[Entrynumber].SSN; 
     cout << endl; 

     cout << "Enter the age of the person" << endl; 
     cin >> Record[Entrynumber].Age; 
     cout << endl; 

     cout << "Enter the address of the person" << endl; 


     cin.getline(Record[Entrynumber].Address,70); 


     cout << endl; 


     stringentry = Record[Entrynumber].Address; 

     stringlength = stringentry.length(); 



     Record[Entrynumber].Address[stringlength] = '*'; 

     cout << "you entered:" << endl; 



     for(jim = 0 ; Record[Entrynumber].Last_Name[jim + 1] != '*' ; jim++) 
     { 
      cout << Record[Entrynumber].Last_Name[jim]; 
     } 

     cout << ',' ; 


     for(jim = 0 ; Record[Entrynumber].First_Name[jim + 1] != '*' ; jim++) 
     { 
      cout << Record[Entrynumber].First_Name[jim]; 
     } 

     cout << endl; 

     cout << Record[Entrynumber].SSN << endl; 
     cout << Record[Entrynumber].Age << endl; 

     for(jim = 0 ; Record[Entrynumber].Address[jim + 1] != '*' ; jim++) 
     { 
      cout << Record[Entrynumber].Address[jim]; 
     } 
     cout << endl; 
     cout << endl; 


     goto Answerinput; 
    case 'N': 
     cout << "ok" << endl; 
     break; 
    default: 
     cout << "invalid answer" << endl; 
     goto Answerinput; 
    } 

輸出到控制檯

would you like to add another entry to 
the archive? (Y/N):Y 

began record number 6+ 1. 


Enter the last name of the person to be entered 
John 


Enter the first name of the person 
John 

Enter the SSN of the person 22222222 

Enter the age of the person 22 

Enter the address of the person 

you entered: 
Joh,Joh 
22222222 
22 
*¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦ 
//////////////22 more lines of'|'////////////////////////////////////////////// 
... 
¦¦¦¦¦¦¦¦l3-j 

would you like to add another entry to the archive? (Y/N): 

兩個cin.getline()和函數getline()做同樣的事情。

我使用MVC++ 2008

所有記錄陣列中的字段是結構,記錄[Entrynumber]。地址是一個字符數組。

回答

3

Cin可能將回車放在getline獲取的緩衝區中。嘗試

cin.ignore(1000, '\n'); 

cin.getline(Record[Entrynumber].Address,70); 

檢索數據後,>>操作不會刪除換行符,但忽略檢索數據之前前導空格,而函數getline只是檢索無論是在那裏,作爲閱讀後刪除「\ n」它是「獲得」的一部分。

+0

它的工作原理!謝謝!爲什麼剩餘的輸入變成getline()函數? – superlazyname 2010-09-16 23:16:29

+0

@jwaffe查看更新後的答案 – 2010-09-16 23:20:47

3

看到如何有可能是輸入在你的函數getline首先讀取緩衝區遺留下來的,我建議你清除緩衝區試圖輸入下一個數據之前:

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

這對我很好。在我的情況下,我提示用戶將數據輸入到ADT數組中。非常令人沮喪,因爲在該函數之前沒有其他任何東西 - 它只是跳過第一個輸入。我有它作爲一個菜單選項使用開關 - 所以它採用輸入按需要輸入選擇0.o似乎是一個明顯的缺陷給我... 更新:雖然在我的情況下,似乎所有是必需的是 cin.ignore(); – 2015-05-19 04:34:42