对于此代码-
#include <iostream>
#include <string>
#include <conio.h>
int main(){
std::string a;
char c{};
while(c!='\r'){
c=getch();
a+=c;
}
a+="xyz";
std::cout<<a;
}
输入:
12345
然后我按Enter键输出:
xyz45
如何阻止这种情况发生,我希望输出为
所需输出:
12345xyz
您需要避免在字符串中添加\r
字符,快速修复,类似于:
while ((c = getch()) != '\r')
a += c;