2

I have a problem with reading strings in c. When I add the gets() function in an if-instruction, the program stops.

Code:

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

int main()
{
    int n,i = 0;
    char sir[2000],ch;

    printf("Press you option: "); scanf("%d",&n);

    if(n == 1)
    {
        printf("text: "); gets(sir);

        printf("\nINPUT: ");
        for(i = 0;i < strlen(sir);i++)
            printf("%c",sir[i]);

    }
    return 0;
}

Any solution?

Legends
  • 21,202
  • 16
  • 97
  • 123
Gradin98
  • 372
  • 2
  • 14

3 Answers3

1

When I add the gets() function in an if-instruction, the program stops.

Look at the preceding code. Maybe you entered 1 Enter

printf("Press you option: "); 
scanf("%d",&n);

scanf("%d",&n); consume the '1', but not the '\n'.
Later code does

printf("text: "); 
gets(sir);

And then gets() reads that '\n' and returns with sir[0] == '\0', an empty string. This causes for(i = 0;i < strlen(sir);i++) to not iterate the body of the for() loop.


What to do?

Read a line of user input fgets() and then process that string. Note that invalid input, EOF and buffer overflow handling not addressed in this simple code example. That would be step 2.

char buf[80];
printf("Press you option: "); 
fgets(buf, sizeof buf, stdin);
sscanf(buf, "%d",&n);

printf("text: "); 
fgets(buf, sizeof buf, stdin);
buf[strcspn(buf, "\n")] = '\0'; // lop off potential \n
strcpy(sir, buf);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

This is due to the C input buffer problem, just add one getchar() as shown, it will work, let me know for any other help.

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

int main()
{
  int n,i = 0;
  char sir[2000],ch;

  printf("Press you option: "); scanf("%d",&n);

  if(n == 1)
  {
    printf("text: ");
    getchar();
    gets(sir);

    printf("\nINPUT: ");
    for(i = 0;i < strlen(sir);i++)
        printf("%c",sir[i]);

 }
return 0;
}
  • 3
    This answer still uses `gets` tough, one should not ignore compiler warnings. (And if your compiler does not warn against `gets` consider upgrading). – Unimportant Jan 03 '17 at 17:48
  • The warning is because gets is very bad at handling overflow errors, one needs to handle it. Else to avoid warning can use scanf("%s", sir); but it will truncate everything after a space. – Shirshendu Bhowmick Jan 03 '17 at 17:53
  • 1
    @ShirshenduBhowmick `gets()` **and** `scanf("%s", sir)` are both equally bad at handling buffer overflows. Neither should be used. – chux - Reinstate Monica Jan 03 '17 at 18:28
  • Yes, but @unimportant was talking about the warning, what i did was just solved the buffer problem by adding `getchar()`, as @Gradin98 asked only about it. – Shirshendu Bhowmick Jan 03 '17 at 19:17
  • @chux is there any reason why one should not use scanf like =>> scanf("%SIZEs", buffer); ? – Michi Jan 03 '17 at 19:49
  • @Michi `scanf("%SIZEs", buffer);` is OK. Weakness: Return value not checked, general complexities/problems about `scanf()`, potential consuming leading white-space when not expected. But yes, that does fix the buffer overflow problem. C's inputs functions, as a whole have weakness, but used as building blocks, can be used well. – chux - Reinstate Monica Jan 03 '17 at 19:56
  • @chux I will not check the return value of scanf in a comment :)), more over when is about scanf itself and not about its return value. I'm agree that I need some other tweaks to make scanf work when it comes to white spaces, I'll probably use fgets here. Scanf is not wrong or bad it is only that almost always used wrong. By the way, happy New Year! – Michi Jan 03 '17 at 20:00
  • @michi Yes `scanf()` is too often used wrong. Perhaps "[Jessica scanf()](http://www.imdb.com/character/ch0008316/quotes)" would say "I'm not bad.. I'm just drawn that way"? – chux - Reinstate Monica Jan 03 '17 at 20:08
  • 1
    @chux you definitely made my day whit that :)) – Michi Jan 03 '17 at 20:13
  • 1
    @Gradin98 What would you want to happen to indicate _no input_ into `sir`? `scanf(" %1999[^\n]", sir);` will not work in that case, it will not return, waiting for non-white-space input. If you wanted to enter 3 spaces into `sir` , that `scanf()` will not work either. Perhaps these matter, perhaps not. The post is unclear. – chux - Reinstate Monica Jan 03 '17 at 20:37
0

gets() is not preferred way to get the input from the user these days. As mentioned above please use fgets to read the input.

Coming back to your problem. Please fflush(stdin) in order to resolve your issue. Trying running the below code. i have just added fflush(stdin) and now the user input is taken by fgets() and no program stopping happens.

Code :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int n, i = 0;
char sir[2000], ch;
printf("Press you option: "); scanf("%d", &n);
if (n == 1)
{
printf("text: ");
fflush(stdin);
gets(sir);
printf("\nINPUT: ");
for (i = 0; i < strlen(sir); i++)
printf("%c", sir[i]);
}
return 0;
}

Also please read and understand why we are using fflush and why we need to avoid gets() function going forward. Hope i have helped you. Thank you :)

Vimal Bhaskar
  • 748
  • 1
  • 5
  • 17