첫 번째 출력 줄에서 첫 번째 문자열로 null 문자를 사용하고있는 것으로 보입니다. 또한이 때문에 입력으로 가져온 마지막 문자열이 누락되었습니다. 나는 getline의 사용법을 놓치고 있을지 모르지만 나는 확신 할 수 없으며 어떤 도움도 인정 될 것이다. 당신이 cin>>t;
할 후C++의 Getline 함수가 첫 번째 입력을 null 문자로 사용하고 있습니다. 그렇게해야합니까?
int main() {
short int t,i;
cin>>t;
string a;
while(t–)
{
getline(cin,a);
cout<<"length of string is "<<a.length()<<endl;
for(i=0;i<a.length()/2;i+=2)
{
cout<<a[i];
}
cout<<endl;
}
return 0;
}
INPUT
4 hello understand think programming
OUTPUT
length of string is 0 length of string is 5 h length of string is 10 udr length of string is 5 t
'cin >> t;'를 실행 한 후에 버퍼는 여전히'getline()'에 의해 읽히는 개행 문자 시퀀스를 포함합니다. –
오른쪽에있는 관련 질문을보십시오. – chris