C++-getline()

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int main(){
	int T;
	cin>>T;
	getchar(); //
	while(T--){
		string str;
		getline(cin,str);//cin>>str;錯!注意空格 
		cout<<str<<endl;
	}
	//system("pause");
	return 0;
}

getline()會讀取輸入流的回車(即cin>>T 之後的回車)並直接結束

所以應吞掉回車

之後getline()進行循環時 每次讀取一行內容包括回車 循環時不會對下一個getline產生影響

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章