0

My for loop is not working fine in this code.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int n;
    char name[100];
    int number;
    printf("Enter the value of n\n");
    scanf("%d", &n);
    printf("Enter %d values\n", n);
    for(int i=0; i<n; i++)
    {
        gets(name);
    }
}

Here I am taking names as string inputs n number of times (as per given value of n by the user), but it is only taking n-1 values as input and not n. For example, if n is taken as 3 then it is taking only 2 inputs.

Akshat kant
  • 125
  • 6
  • 2
    `gets` (which you shouldn't even be using at it is no longer part of the standard library) reads chars until the next newline in the input stream. Think about *everything* you touched on your keyboard when entering `n`. : *everything* (most important, immediately after the last digit of `n`). Running a debugger and seeing the value of memory behind `name` would be conclusive evidence of what is going on. – WhozCraig Jul 26 '21 at 08:00
  • When you give the input for `n`, do you end it with the `Enter` key? That key will be added to the input buffer as a newline. And this newline will be the very first thing that the first call to `gets` reads, and it's like `gets` have read an empty line. – Some programmer dude Jul 26 '21 at 08:04
  • the problem is that `scanf("%d")` *stops* consuming input *before* it would get the newline – Antti Haapala -- Слава Україні Jul 26 '21 at 08:06
  • In short: Your loop is fine, it's your expectation of the input which is messing things up. All this would have been very easy to find out (much quicker than writing this question I'll bet) if you just learn how to use a *debugger* to step through your code statement by statement while monitoring variables and their values. – Some programmer dude Jul 26 '21 at 08:07
  • [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/q/1694036/995714) – phuclv Jul 26 '21 at 09:17

1 Answers1

1

By using gets you're using a deprecated function, which reads characters from standard input until a newline character is met. It means that the previous input you did, which is n, generates a newline in the stdin whenever you press Enter, and that newline is red by the gets which causes to iterate to the next index of the loop. Use something else to read input from command line, like fgets.

Nastor
  • 638
  • 4
  • 15
  • Thanks, can you give an example of where would be the best place to use gets()? – Akshat kant Jul 26 '21 at 08:05
  • Whenever you know exactly how many characters you're going to read, you can use gets, but I would recommend against using it. – Nastor Jul 26 '21 at 08:07
  • 3
    @Akshatkant there is no "best place" for `gets` besides the trash bin. That function is evil and was removed from the standard library ten years ago. `fgets` is a much better option (your code will suffer the same problem of failing to consume the leftover newline prior to string reading as you're doing now, but at least your code is not longer a breeding ground for buffer overflow attacks). – WhozCraig Jul 26 '21 at 08:08