get line read carriage return

get line read carriage return

Recently, I encountered a getline problem with PAT:
Simplify the problem as follows, if you don’t add cin.get(). Input n will output immediately, and n is just a number.
The reason is that get line reads the carriage return of the buffer.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
using namespace std;
//这里都是13 第一个都是tret
//建表 必须用init for用不了 字符数组记得大括号
//map<a,b> x[]错误 不能写map数组
int main() {
    
    
	
	int n; 
	cin>>n;
	while(n--){
    
    
		string s;
		cin.get();
		//getline(cin,s);
		cin>>s;
		if(s[0]>='0'&&s[0]<='9'){
    
    
			cout<<"a"<<endl;
		}else{
    
    
			cout<<"b"<<endl;
		}
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43152520/article/details/109559640