2

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.

  • That's surpising. Is it the same when you change the first output line to: `std::cout << "Enter your favorite number between 1 and 100: " << std::flush;`? – Scheff's Cat Aug 30 '19 at 13:31
  • It might have something to do with buffered/unbuffered output. I found the following Q/As about this: [SO: Buffered vs unbuffered IO](https://stackoverflow.com/q/1450551/7478597) and (more specifical for `std::cout`) [SO: Is std::cout buffered?](https://stackoverflow.com/q/26975876/7478597). Though, I still find surprising that the last space may be buffered until after `std::cin`... – Scheff's Cat Aug 30 '19 at 13:40
  • @Scheff i tried using ```<< std::flush;``` but it still does that. – Roee Weiss Lipshitz Aug 30 '19 at 14:33
  • Really crazy. What OS? What compiler? What IDE (in case)? OK, I see - CLion. – Scheff's Cat Aug 30 '19 at 14:35
  • If it's an IDE issue (even then it's strange), you could start your binary (executable) directly from a console (command window) and see whether this changes anything. – Scheff's Cat Aug 30 '19 at 14:37
  • 1
    My last desperate idea: localization issue? If you have hebrew as localization there might be extra magic to combine LtoR and RtoL output... (Actually, I don't have any experience except having heard that some localizations force right-to-left layout...) ;-) – Scheff's Cat Aug 30 '19 at 14:41
  • `CLinon` is just IDE it doesn't have any impact on your issue. The problem must be terminal application on which your application is run. – Marek R Aug 30 '19 at 14:42
  • @MarekR currently i'm using the Clion terminal. I tried compiling and running through PowerShell and it worked. what could be the reason for it not to work properly on the Clion terminal? I'm on windows 10. – Roee Weiss Lipshitz Aug 30 '19 at 14:53
  • @Scheff as I commented, it worked while running on powershell, but I have no idea why it wouldn't work on the clion terminal. – Roee Weiss Lipshitz Aug 30 '19 at 14:55
  • So try to run your application from system terminal (or other) and see if problem persists. If it is not present then ignore the issue since this would be CLion issue and end user will not run your application from CLion. – Marek R Aug 30 '19 at 14:55
  • The actual question is: What does CLion use for console I/O? (I don't know, sorry.) – Scheff's Cat Aug 30 '19 at 14:59
  • @Scheff thanks, I'll check that. – Roee Weiss Lipshitz Aug 30 '19 at 15:00
  • @MarekR thanks, that's a valid point. – Roee Weiss Lipshitz Aug 30 '19 at 15:01
  • Note that you should not depend on graphical relation between standard input and standard output. Usually it works but in general this streams should be threaded independently. Anyway problem you are observing is quite unusual. – Marek R Aug 30 '19 at 15:07
  • @MarekR I don't really understand what you mean by "graphical relation" and "standard input and standard output". – Roee Weiss Lipshitz Aug 30 '19 at 15:41

5 Answers5

2

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

marcos assis
  • 388
  • 2
  • 14
2

When you are using CLion, You can try disabling PTY (Help | Find Action > type "Registry" > open Registry > find and disable the run.processes.with.pty option)

CLion moving space into new line

dokcn
  • 29
  • 4
1

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

Daniel
  • 2,320
  • 1
  • 14
  • 27
  • Yeah, could be a problem with my settings. I'm using Clion, could it be connected? – Roee Weiss Lipshitz Aug 30 '19 at 14:34
  • from their documentation it is unclear whether they use the default bash or have their custom, it also highly depends what OS you are using since the cmd from windows has different features compared to the one on linux. – Daniel Aug 30 '19 at 14:41
  • try to change your default terminal to powershell https://stackoverflow.com/questions/20949892/using-powershell-as-terminal-in-intellij-idea-ides-like-pycharm-phpstorm-or-rub – Daniel Aug 30 '19 at 15:01
  • Turns out it's not a terminal problem but rather a problem with the Run terminal. Not really sure what I can do with that – Roee Weiss Lipshitz Aug 30 '19 at 15:38
1

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.

0

This problem is only applicable for the second line, if you leave that line empty then the problem will be fixed. Don't know about C++ but for C before the print statement you have to add: printf("\n");