1

For this code:

#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;
}

Input: 12345, then Enter key


Output: xyz45


How do I stop this from happening?


Desired output: 12345xyz


anastaciu
  • 23,467
  • 7
  • 28
  • 53
anonymous38653
  • 393
  • 4
  • 16

1 Answers1

3

You need to avoid adding \r character to the string, something like:

while ((c = getch()) != '\r')
    a += c;
anastaciu
  • 23,467
  • 7
  • 28
  • 53