0

I'm new to programming in c++ and I'm facing a problem following lynda.com tutorials. It seems okay on the tutorial video but this isnt working with me.

#include <stdio.h>

enum { max_string = 127 };

static char string[max_string + 1 ] = "";

int main( int argc, char ** argv ) {
    printf("Type a string: ");
    fgets(string, max_string, stdin);
    printf("The string is %s", string);
    return 0;
}

And when I run this something blank appears and I need when I right something like "hey" in the blanket space, this happens:

hey
Type a string: The string is hey

This is completely strange for me and I have no idea what Im doing wrong tbh. I'm using Eclipse btw.

Could someone help me out?

Alexey Frunze
  • 58,178
  • 10
  • 71
  • 163
Verance
  • 67
  • 2
  • 10
  • 1
    Probably not the canonical solution but add `fflush(stdout);` after the first `printf` statement, that should do it. – Matt Phillips Feb 15 '13 at 01:44
  • 3
    A C++ tutorial that tells you to use a static char array for user input and printf/fgets? *Run!* – us2012 Feb 15 '13 at 01:45
  • http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin – jogojapan Feb 15 '13 at 01:47
  • 1
    @us2012 - at least it isn't using `gets(string)`! – D.Shawley Feb 15 '13 at 01:50
  • May I humbly suggest http://www.cplusplus.com/ and their excellent tutorials? – Charles Burns Feb 15 '13 at 02:42
  • @CharlesBurns : I really hope that was intended to be ironic... – ildjarn Feb 15 '13 at 02:44
  • @ildjarn: It wasn't. Their tutorials do not cover the finer points of , which user2074085 might find useful in 5 years, but for a basic ANSI C++ tutorial, it explains things well and has nicely formatted, easy-to-understand, step-by-step tutorials. – Charles Burns Feb 15 '13 at 03:56
  • @CharlesBurns : http://stackroulette.com/programmers/88241/undefined – ildjarn Feb 15 '13 at 04:23
  • @ildjarn: We're in a question involving `"Enter a string"` --> `"Here's your string"` and criticize a tutorial website that "omits vital information [about] iterator invalidation"? I consider the paucity of errors in so large a site and by so pedantic an analysis to be confirmation of its quality, not evidence to the contrary. – Charles Burns Feb 15 '13 at 07:10

1 Answers1

2

It appears that your standard output stream is line-buffered, meaning that text you print doesn't appear until you've printed a complete line. It should be unbuffered if you're writing to an interactive device; perhaps something is preventing the system from being aware that the output device is interactive.

Adding

fflush(stdout);

after your first printf should force the "Type a string: " prompt to appear immediately (and even if your output is unbuffered, fflush(stdout) is harmless).

I was about to suggest changing your second printf from:

printf("The string is %s", string);

to:

printf("The string is %s\n", string);

to ensure that your program's output ends with a newline (some systems can misbehave if it isn't) -- but fgets() actually leaves the newline in your string (unless the input line was very long). Eventually you'll want to be able to deal with that kind of thing.

Keith Thompson
  • 230,326
  • 38
  • 368
  • 578