當我使用getline(cin,variablehere)函數時,我的程序跳過了一些代碼。我不知道代碼有什麼問題。見下面String getline(cin,variable)函數跳過一些代碼行
#include <iostream>
#include <string>
using namespace std;
int main()
{
string getfirstname;
string lastname;
string address;
int contactnumber;
cout << "Enter First name : ";
getline(cin, getfirstname);
cin.ignore();
cout << "Enter Last name : ";
getline(cin, lastname);
cin.ignore();
cout << "Enter Address : ";
getline(cin, address);
cin.ignore();
cout << "Enter Contact number : ";
cin >> contactnumber;
cin.ignore();
CurrentNumberOfContacts += 1;
cout << "Successfully added to contact list!" << endl << endl;
cout << "Would you like to add another contact ? [Y/N] ";
cin >> response;
//more lines of codes below
return 0;
}
輸出我已經inputed「詮釋」爲數據類型,因爲它包含的數字僅
刪除屏幕截圖並在此輸出 – 2014-10-08 02:25:58
電話號碼不是整數。 – 2014-10-08 02:27:32
你正在使用cin >>聯繫號碼,而不是getline(cin,contactnumber) – 2014-10-08 02:28:38