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

问题描述 投票:0回答:1

对于此代码-

#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

c++ string conio
1个回答
1
投票

您需要避免在字符串中添加\r字符,快速修复,类似于:

while ((c = getch()) != '\r')
    a += c;
© www.soinside.com 2019 - 2024. All rights reserved.