0

Do you know why eclipse console first executes gets function and then cout? I have this code:

#include <cstdio>
#include <iostream>

using namespace std;

int main() {
  char str[80];
  cout << "Enter any string: ";
  gets(str);
  cout << "Here is your string: ";
  cout << str;
  return 0;
}
It's just a test
Enter any string: Here is your string: It's just a test

P.S. This program works as it should if i'm using DOS console.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
nikoliazekter
  • 757
  • 2
  • 6
  • 23

3 Answers3

2

Add endl after using cout. If will add a newline and flush the stream.

  cout << "Enter any string: " << endl ;
  cout << "Here is your string: " << endl ;

And don't use gets!

2501
  • 25,460
  • 4
  • 47
  • 87
  • 3
    Only use `endl` if you want both a newline and a flush. Use `'\n'` for just a newline or (as wanted here) `flush` for just a flush. – Mike Seymour Oct 30 '14 at 12:15
  • @2501 Thanks. It helped. I use gets() just because i'm reading Shildt and it gives advice that one of easiest way to read string from console is using gets() function. – nikoliazekter Oct 30 '14 at 12:16
  • @MikeSeymour http://stackoverflow.com/posts/26652682/edit There is an easier way. – 2501 Oct 30 '14 at 12:17
  • 3
    @nikoliazekter: You should throw that book away and get one of these: http://stackoverflow.com/questions/388242 – Mike Seymour Oct 30 '14 at 12:17
  • 2
    Yep, the schildt book is infamously bad. – Deduplicator Oct 30 '14 at 12:18
  • @Deduplicator I read Shildt book about Java and it teached me all i should know before learning libGDX. So i decided to read his book about C++. – nikoliazekter Oct 30 '14 at 12:23
  • 1
    @nikoliazekter: Unfortunately his book about C++ is *legendarily* bad. – DevSolar Oct 30 '14 at 12:27
  • @nikoliazekter: Well, I don't know how he is with Java, but no C or C++ programmer will touch his books about those languages with a ten-foot-pole. There are just too many glaring errors, unmentioned assumptions about the implementation (for all the examples he seems to have tested, he certainly only ever used one and the same implementation), and general bad advice in there. One of the famous quotes about it is that the price-difference between his book and the standard equals the value he removed (and that'S charitable). – Deduplicator Oct 30 '14 at 12:27
  • @Deduplicator so can you recommend me good book for beginner? – nikoliazekter Oct 30 '14 at 12:29
  • @nikoliazekter Mike directed you to the SO-question with all the recommendations... Do not forget that man-pages generally are quite good. Though for the final word, download a draft of the C standard. When reading language-specifications and such, be aware that the language is quite deliberate, and when something cannot be proven, it probably does not hold. Thus, always prefer proper explanations with proof. – Deduplicator Oct 30 '14 at 12:33
2

Eclipse IDE is certainly not executing gets (Never use that function, it cannot be used safely and even the C standard banned it), but executing the compiler, linker and later the compiled program.

The problem is Eclipse IDE redirects the standard-handles, thus makeing the standard-library mis-identify those streams as "not an interactive device", which means full buffering is enabled, and automatic flushing of output on input is disabled.

The solution is the same as for C streams:

Explicitly flush your output.

cout.flush(); // Just flush
cout << endl; // Output newline and flush
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • +1 for mentioning that usually reading input _does_ automatically flush output buffers, but this is disabled by the redirection in this case. (In the C++ library this is controlled by `sync_with_stdio` and `basic_ostream::tie`) – Jonathan Wakely Oct 30 '14 at 13:17
1

"Do you know why eclipse console first executes gets function and then cout?"

The reason is that the output buffer isn't immediately flushed, when you call operator<<(). std::endl does so, as mentioned in the other answer, but also puts an extra newline, which you may not want to have there. The clean solution is to call std::ostream::flush() explicitly

  cout << "Enter any string: ";
  cout.flush(); // <<<<
  cin >> str;
  cout << "Here is your string: " << str << std::endl;
Community
  • 1
  • 1
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190