1

I have a piece of code here:

Tree *rangeprint(Tree *t) {
  char first[20];
  char last[20];
  int f = 0;
  int l = 0;

  printf("First Entry?\n");

  while(1) {
    first[f] = getchar();
    if(first[f] == '\n') {
      first[f] = '\0';
      break;
    }
    f++;
  }

  printf("Last Entry?\n");

  while(1) {
    last[l] = getchar();
    if(last[l] == '\n') {
      last[l] = '\0';
      break;
    }
    l++;
  }

  printf("%s %s\n", first, last);
}  

When I run this code, the output I get in the console is:

First Entry?
Last Entry?

Why is it skipping over the while loops and printing everything before executing them?

UPDATE - I changed the termination condition to 'x' instead of '\n' and sure enough it prints properly.

Adding a random getchar() before the loop starts fixes the problem, since the '\n' is read into there.

Joe
  • 173
  • 3
  • 10
  • There's no code formatter in this site... – m0skit0 Jan 08 '13 at 16:53
  • 2
    It should be easy to indent your code correctly; just copy-and-paste your code into the editor, select it, and then hit the **{ }** button. – Oliver Charlesworth Jan 08 '13 at 16:53
  • It was already formatted by someone else, you just made it worse – Tim Jan 08 '13 at 16:59
  • Did you give a CRLF(\n) or press enter as soon as you get First Entry? on your console? – sr01853 Jan 08 '13 at 17:00
  • What does your debugger say? – n. m. could be an AI Jan 08 '13 at 17:03
  • You most likely have a `\n` left over in your buffer from previous operations before entering this routine. The first `getchar()` reads that, breaks out of the while loop and continues, with the result that both prompts get printed. – twalberg Jan 08 '13 at 17:10
  • I get the feeling that it is to do with \n being left over, since before first entry last entry, I have another bit of code which asks if you want to print all entries or just a range. However surely adding that fflush(stdin); would get rid of that left over \n? – Joe Jan 08 '13 at 17:20
  • I just tried compiling with gcc and running the code, and had no problems (blocking read on getchar(), intended output). Are you sure you've recompiled properly after making the changes people have suggested here and are running the correct executable? It seems like this should work, so maybe you're running an old binary. – Haz Jan 08 '13 at 17:26
  • In the function leading to this one, there is a '\n' being read at the end so that's why when running my whole program I get the error, but if I ran the function alone then there would be no previous '\n', and therefore no error. – Joe Jan 08 '13 at 17:32

4 Answers4

2

It is printing them, it's just that they are empty strings! (hint: f and l doesn't change - I assume the last loop using f is a typo!)

Nim
  • 33,299
  • 2
  • 62
  • 101
  • Sorry I don't think I explained very well. After printf("First entry") I am supposed to have time to input something, but when running the program it skips through that and just prints Last entry right after first entry. And I always forget the f++ in loops, I've fixed my code up a bit now. – Joe Jan 08 '13 at 17:04
  • That doesn't seem to change anything unfortunately. It seems like that should fix the problem, for some reason it doesn't – Joe Jan 08 '13 at 17:23
  • @Joe, sorry the `fflush` idea is nonsense - it only works on output streams, you need to manually call `getchar()` to remove any redundant characters (which in this case worked for you because there was a stray `\n`) – Nim Jan 08 '13 at 21:58
0

You are not advancing the pointer f while reading characters from the input. You are overwriting chatacter at location 0 all the times and in the end location 0 contains \0

hmatar
  • 2,437
  • 2
  • 17
  • 27
  • Yeah sorry, hadn't checked the code properly. Unfortunately fixing this doesn't fix the problem I have. – Joe Jan 08 '13 at 17:24
0

try to put an \n at the end

 printf("First Entry?\n");
 printf("Last Entry?\n");
Buenjy
  • 300
  • 2
  • 6
0

The getchar reads from the std in. If there is any newline character in previous input, It can cause the issue. Just put,

fflush(stdin);

after

printf("First Entry?");
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • yeah I added fflush and I still get the same problem – Joe Jan 08 '13 at 17:16
  • @Joe: where did you added that ? before first while loop ? – Midhun MP Jan 08 '13 at 17:19
  • yep, I added it before both while loops and it still prints both first and last entry. – Joe Jan 08 '13 at 17:21
  • I added a getchar() before the first printf and it has fixed the problem, since that spare '\n' is now read into there instead of the loop. Thanks for pointing me in the right direction. Any idea why fflush didn't work though? – Joe Jan 08 '13 at 17:30
  • @Joe: I'm also wondering why fflush not worked ! Thanks for your comment – Midhun MP Jan 08 '13 at 17:32
  • @JoeL I got a link about the fflush issue, please take a look when you get time http://stackoverflow.com/questions/9122550/c-fflush-function-not-working :) – Midhun MP Jan 08 '13 at 17:37