如代碼,我發現程序在運行時會跳過cin.get,這是怎麼回事啊?另外,關於將未知長度的字符串輸入數組,還有什麼好辦法嗎?
int main()
{
int time;
scanf("%d", &time);
int count=0;
while(count < time)
{
string str;
char temp;
int i;
while((temp=cin.get())!='\n')
{
str +=temp;
}
length =str.length();
char* preorder = new char[length];
char* inorder = new char[length];
for(i = 0;i<length;i++)
{
preorder[i]=str[i];
}
for(i=0;i<length;i++)
{
scanf("%c", inorder[i]);
}
sort(preorder, inorder, length);
count++;
delete preorder;
delete inorder;
}
return 0;
}
scanf("%d", &time);輸入完後,輸入緩沖區還會遺留下一個回車符
然後執行while((temp=cin.get())!='\n'),會直接讀取遺留的回車,temp就等於'\n'了,所以樓主感覺跳過了這句
可以在scanf("%d", &time);後加上fflush(stdin);用來清空輸入緩沖區