1

Whenever the code is executed, the contents inside the for loop are not executing for the first time, i.e., when i=0. But the loop executes after i=0, i.e., for i=1,2,3,..n-1. Can anyone explain what's wrong here?

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
    char string[30][100];
    int n,i;
    scanf("%d",&n);
    for(i=0;i<n;i++){
       gets(string[i]);
       printf("%s\n",string[i]);
    }
    getch();
    return (EXIT_SUCCESS);
}
nsane
  • 1,715
  • 4
  • 21
  • 31
  • 2
    `scanf("%d",&n);getchar();//for eat newline` – BLUEPIXY Apr 06 '14 at 12:22
  • 4
    Or just add a space after the `scanf` format code (i.e. `"%d "`) – Some programmer dude Apr 06 '14 at 12:23
  • @JoachimPileborg `scanf("%d\n",&n);` will do the same ? – Dabo Apr 06 '14 at 12:24
  • 3
    Oh, and stop using `gets`. The function has even been removed from the latest C standard. Use [`fgets`](http://en.cppreference.com/w/c/io/fgets) instead. – Some programmer dude Apr 06 '14 at 12:24
  • 1
    @Dabo Yes, `scanf` consider any whitespace characters as a whitespace character. It doesn't matter if it's a newline, a tab, a space (or anything else that `isspace` returns non-zero for). – Some programmer dude Apr 06 '14 at 12:25
  • If a program is interactive, do **not** leave a trailing white space in a format such as `"%d "` because that trailing space won't allow `scanf()` to return until the hapless user types a non-space (non-tab, non-newline) character after the number. – Jonathan Leffler Apr 06 '14 at 12:37
  • @JoachimPileborg then `"%d\n"` is more general solution ? – Dabo Apr 06 '14 at 12:52
  • 1
    @Dabo No not really. A space is a space is a space... *Any* space is good to use and all spaces are equal. – Some programmer dude Apr 06 '14 at 12:53
  • No; `"%d\n"` is isomorphic with `"%d "`; any white space in a `scanf()` format is equivalent to optional white space in the input stream. The difficulty with trailing white space in the format is that `scanf()` can't tell it's reached the end of the white space until it reads something that is not white space. Either use `fgets()` to read lines and `sscanf()` to parse them, or gobble the remainder of the line with some sort of loop (reading a single character isn't guaranteed to be sufficient; the user might have typed a space after the number). – Jonathan Leffler Apr 06 '14 at 12:55

2 Answers2

0

you can either try leaving a space after the scanf so the system pauses there instead of automatic increment the reason for this is after you press enter for the value of n , theres no system pause and one of the i values from loop is automatically considered as entered by the system :

scanf("%d ", &n);

or make the loop be from 0 till equal to n which will still skip a number but at the same time add 1 just to keep the loop equal to the input "n" u give

for(i=0;i<=n;i++)

bear in mind that the first option is much wiser to use since you might need to use the "n" in some other part of the program as well.

Rage91
  • 87
  • 1
  • 9
  • If a program is interactive, do **not** leave a trailing white space in a format such as `"%d "` because that trailing space won't allow `scanf()` to return until the hapless user types a non-space (non-tab, non-newline) character after the number. – Jonathan Leffler Apr 06 '14 at 12:42
  • @JonathanLeffler you are right about that thanks for mentioning it, do you know if theres any other way than clearing the buffer? – Rage91 Apr 06 '14 at 12:45
  • If worst comes to worst: `void gobble_to_newline(FILE *fp) { int c; while ((c = getc(fp)) != EOF && c != '\n') ; }`, maybe written as an inline function tuned to standard input only: `static inline void gobble_stdin_to_newline(void) { int c; while ((c = getchar()) != EOF && c != '\n') ; }`. In my code, the semicolon after the `while` condition would be on a line of its own to emphasize that the loop body is empty. It's hard to show that in a comment. – Jonathan Leffler Apr 06 '14 at 12:51
-1
for(i=0;i<n;i++)
{
   fflush(stdin);   //Clears the buffer.insert this here.
   gets(string[i]);
   printf("%s\n",string[i]);
}

After execution of scanf("%d",&n); you are leaving '\n'(when you press Enter key at the end) character in buffer.Which should be cleared before execution of any other input operation.fflush(stdin) will clear the keyboard buffer. There are many other ways to do it.But using fflush(stdin) is easy for beginners.

Mysterious Jack
  • 621
  • 6
  • 18
  • 2
    You can only use `fflush(stdin)` reliably on Windows. Elsewhere, it is undefined behaviour — the C standard says (ISO/IEC 9899:2011 §7.21.5.2 The `fflush` function): _If stream points to an output stream or an update stream in which the most recent operation was not input, the `fflush` function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined._ – Jonathan Leffler Apr 06 '14 at 12:41
  • Also, a program can ***never*** use `gets()` safely. It cannot protect itself from misuse or abuse. One of the attacks used by the first Internet Worm (search on Google for 'morris internet worm'; it happened in 1988) exploited code that used `gets()`. It is no longer standard C (it was removed from C11, for good reason). – Jonathan Leffler Apr 06 '14 at 12:48