getline后如何获取输入
How to take input after getline
#include <iostream>
#include <string>
using namespace std;
struct Student
{
int ID;
long phno;
string name;
string depart;
string email;
};
int main ()
{
Student S1 ;
cout << "\n=======================================================\n" ;
cout << "Enter ID no. of student 1 : " ; cin >> S1.ID ;
cout << "Enter name of student 1 : " ; getline(cin, S1.name) ; cin.ignore();
cout << "Enter department of student 1 : " ; getline(cin, S1.depart) ; cin.ignore();
cout << "Enter email adress of student 1 : " ; getline(cin, S1.email) ; cin.ignore();
cout << "Enter phone number of student 1 : " ; cin >> S1.phno ;
return 0;
}
问题是在邮箱地址后不接受输入程序在phno中忽略接受输入,在邮箱地址后直接退出。
我对您的代码做了一些细微的修改。
请注意,在直接对变量(cin >> S1.ID
或 cin >> S1.phno
)使用 cin
之后,我 仅 调用 cin.ignore()
.
这是因为当您在 int 上使用 cin
时,它会将 \n
留在缓冲区中。当您稍后调用 getline(cin,...)
时,您只需吸干剩余的 \n
,这将被视为您的全部 "line."
有可用的示例 here。
#include <iostream>
#include <string>
using namespace std;
struct Student
{
int ID;
long phno;
string name;
string depart;
string email;
};
int main ()
{
Student S1 ;
cout << "\n=======================================================\n" ;
cout << "Enter ID no. of student 1 :\n" ;
cin >> S1.ID ;
cin.ignore();
cout << "Enter name of student 1 :\n" ;
getline(cin, S1.name) ;
cout << "Enter department of student 1 :\n" ;
getline(cin, S1.depart) ;
cout << "Enter phone number of student 1 :\n" ;
cin >> S1.phno ;
cin.ignore();
cout << "Enter email adress of student 1 :\n" ;
getline(cin, S1.email) ;
cout << endl << endl;
cout << S1.ID << endl << S1.name << endl << S1.depart << endl << S1.phno << endl << S1.email << endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
struct Student
{
int ID;
long phno;
string name;
string depart;
string email;
};
int main ()
{
Student S1 ;
cout << "\n=======================================================\n" ;
cout << "Enter ID no. of student 1 : " ; cin >> S1.ID ;
cout << "Enter name of student 1 : " ; getline(cin, S1.name) ; cin.ignore();
cout << "Enter department of student 1 : " ; getline(cin, S1.depart) ; cin.ignore();
cout << "Enter email adress of student 1 : " ; getline(cin, S1.email) ; cin.ignore();
cout << "Enter phone number of student 1 : " ; cin >> S1.phno ;
return 0;
}
问题是在邮箱地址后不接受输入程序在phno中忽略接受输入,在邮箱地址后直接退出。
我对您的代码做了一些细微的修改。
请注意,在直接对变量(cin >> S1.ID
或 cin >> S1.phno
)使用 cin
之后,我 仅 调用 cin.ignore()
.
这是因为当您在 int 上使用 cin
时,它会将 \n
留在缓冲区中。当您稍后调用 getline(cin,...)
时,您只需吸干剩余的 \n
,这将被视为您的全部 "line."
有可用的示例 here。
#include <iostream>
#include <string>
using namespace std;
struct Student
{
int ID;
long phno;
string name;
string depart;
string email;
};
int main ()
{
Student S1 ;
cout << "\n=======================================================\n" ;
cout << "Enter ID no. of student 1 :\n" ;
cin >> S1.ID ;
cin.ignore();
cout << "Enter name of student 1 :\n" ;
getline(cin, S1.name) ;
cout << "Enter department of student 1 :\n" ;
getline(cin, S1.depart) ;
cout << "Enter phone number of student 1 :\n" ;
cin >> S1.phno ;
cin.ignore();
cout << "Enter email adress of student 1 :\n" ;
getline(cin, S1.email) ;
cout << endl << endl;
cout << S1.ID << endl << S1.name << endl << S1.depart << endl << S1.phno << endl << S1.email << endl;
return 0;
}