-1

I wanted to limit the number of characters the user enters for a char array name using fgets, but it still allows the user to enter more than the number of characters it is allowed to enter. How I correct this problem? Below is my snippet code:

char name[15];
printf("Enter your name: ");
getchar();
fgets(name);
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
Noal99
  • 1
  • 1
  • I had to look up the definition of remediate (root word is remedy). – Fiddling Bits Oct 10 '22 at 19:14
  • Why `getchar();`? If I enter `Fiddling` won't name be `iddling`? – Fiddling Bits Oct 10 '22 at 19:15
  • 2
    The `fgets` function takes more arguments than that. – Cheatah Oct 10 '22 at 19:15
  • 2
    Your compiler should have rejected this. `fgets(name, sizeof name, stdin);` – Weather Vane Oct 10 '22 at 19:15
  • 2
    Anyway don't use such penny pinching array sizes. It's not your lunch. `char name[100];` – Weather Vane Oct 10 '22 at 19:17
  • That code should not compile with `#include `. If you did not include that header, you should have gotten some warnings, and honestly that is just asking for trouble. – Cheatah Oct 10 '22 at 19:20
  • by not using dangerous functions. Use safer ones – 0___________ Oct 10 '22 at 19:22
  • 1
    You should never, ever use `gets()`. See the duplicate for details of why and how to avoid it. The code is curious — presumably the `getchar()` is supposed to read a newline left over by some previous use of `scanf()`. But there needs to be more checking than is shown, and you never use `gets()`. – Jonathan Leffler Oct 10 '22 at 19:23
  • I rolled it back to the original question. Please don't post "moving targets", this isn't an interactive tutorial. – Weather Vane Oct 10 '22 at 19:24
  • Oh, bother! The rolled-back question is not using `gets()` but is misusing `fgets()` instead. Is the duplicate still appropriate enough, or should it be reopened? – Jonathan Leffler Oct 10 '22 at 19:26
  • @JonathanLeffler don't you think there are enough `gets()` questions? An answer has appeared anyway. – Weather Vane Oct 10 '22 at 19:27
  • 1
    @WeatherVane edit made question nonsense – 0___________ Oct 10 '22 at 19:29
  • @WeatherVane: there are too many teachers who still mention `gets()` and don't emphasize that using it is grounds for failing the assignment immediately. There are a myriad questions using `gets()` on SO, which is a nuisance. I'm not sure what's best to do? Closing and deleting the question because of the malformed call to `fgets()` is harsh, too. – Jonathan Leffler Oct 10 '22 at 19:29
  • @JonathanLeffler the whole thing seems to have gone pear-shaped. The answer was addressing the use of `gets()`. It's just a poor question, with no MCVE, and no research. – Weather Vane Oct 10 '22 at 19:31
  • @Noal99: You have an array of 15 characters. If you call `fgets()` accurately, you can store up to 13 characters for the actual name, a newline, and a null byte to terminate the string. If the user types a 14-character name, then the newline will be left in the buffer for the next I/O operation to use. If the user types a longer name, then the first 14 characters typed will be stored and the rest will be left for the next I/O operation. You will probably want to zap the newline to a null byte: `name[strcspn(name, "\n")] = '\0';`. _[…continued…]_ – Jonathan Leffler Oct 10 '22 at 19:38
  • _[…continuation…]_ You likely need to check whether there was a newline to zap — and you probably need to do something about any extraneous characters entered: `int c; while ((c = getchar()) != EOF && c != '\n') ;` will read and discard characters up to the newline or EOF, but you only do that if there wasn't a newline in the `name` string returned by `fgets()`. The duplicate question ([Why is `gets()` to dangerous to be used, ever?](https://stackoverflow.com/q/1694036/15168)) covers a lot of what I've just said. – Jonathan Leffler Oct 10 '22 at 19:41
  • Note that the first version of the question specified `fgets()` in the text and used a mal-formed call to `fgets()` in the code — the length and file stream arguments were missing. The question was updated by the OP to mention and use `gets()`. The question was then set back to use `fgets()`, as in the original. It's a mess! – Jonathan Leffler Oct 24 '22 at 17:30

1 Answers1

0

I wanted to limit the number of characters the user enters for a char array name using gets

You can't. Simply do not use this function. It depreciated and not safe. Use fgets instead. Also, check the return value to check if the function did not fail. Basically, you should always check for possible errors.

result = fgets(name, sizeof(name), stdin);
if(result) {/* do something */}
else  {/* error handling */}
0___________
  • 60,014
  • 4
  • 34
  • 74
  • Sorry to have rolled the question back to the original version before I registered this answer. – Weather Vane Oct 10 '22 at 19:36
  • 1
    @WeatherVane. My first time using stack overflow. I appreciate all of you for all the answers and recommendations. – Noal99 Oct 10 '22 at 19:52
  • @Noal99 you are welcome, please ask more questions. Happy to see you have now read the introductory material, and perhaps [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Weather Vane Oct 10 '22 at 19:57