如何在使用 getch 后在另一个字符串的末尾连接一个字符串

How to concatenate a string at the end of another string after using getch

本文关键字:字符串 连接 一个 getch 另一个      更新时间:2023-10-16

对于此代码:

#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,然后回车


输出:xyz45


如何阻止这种情况发生?


所需输出:12345xyz


您需要避免在字符串中添加r字符,例如:

while ((c = getch()) != 'r')
a += c;