White space on the end of output string not printing with the string, but rather with the next printed line after it

巧了我就是萌 提交于 2020-12-06 07:06:09

问题


I tried to print a line that asks for input from the user, get the input, then print again some line. The problem is that the white space at the end of the first printed line is printed not at the end of the line, but rather at the beginning of second printed line, after i get the input.

I'm completely new to C++ so I couldn't really try much, but i tried printing the code without the part that prompts the input from the user, and it prints the space just fine, but when i add std::cin << input; the space get's sent to the beginning of the second line.

My code:

    int input;
    std::cout << "Enter your favorite number between 1 and 100: ";
    std::cin >> input;
    std::cout << "Amazing... That's my favorite number too... wow..." << std::endl;

I want the output to be

Enter your favorite number between 1 and 100: //some input
Amazing... That's my favorite number too... wow...

(note the space before //some input)

Instead i get

Enter your favorite number between 1 and 100://some input
 Amazing... That's my favorite number too... wow...

(note the space before Amazing)

Edit: I'm using Clion if it could be connected. Also, I tried to run the executable on powershell and it worked as expected, without the problem, so this has something to do with the Clion terminal. Also, i'm using windows 10 as my OS.

Second Edit: add my findings on my answer.


回答1:


This seems to be a problem with buffered input of Clion. See this issue: https://youtrack.jetbrains.com/issue/CPP-7437




回答2:


Are you sure about that? I tried both in online shell and on local machine and it works as expected.




回答3:


After checking I found out that this occurs only on the Clion Run terminal, so this has something to do with it exclusively. I'm currently trying to mess around with the settings. I will post a solution and an explanation here if I find it.

Edit: as mentioned in one comment, it could be the issue mentioned here https://youtrack.jetbrains.com/issue/CPP-7437.

In any case it is a Clion related problem exclusively, and not a C++ problem.



来源:https://stackoverflow.com/questions/57727656/white-space-on-the-end-of-output-string-not-printing-with-the-string-but-rather

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!