-3
printf("\nWhich minute to query?");
gets(query);
val = strcmp (query, out);

return 0;

I tried to use gets and it did not compile and it had a runtime error. What can I substitute to make it compile.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 4
    `fgets` is a safer, non-deprecated option. – SGeorgiades Mar 22 '22 at 15:37
  • 8
    `it did not compile and it had a runtime error` What? You can't have both. – tkausl Mar 22 '22 at 15:37
  • 2
    Yes, [`fgets`](https://en.cppreference.com/w/c/io/fgets). – Fred Larson Mar 22 '22 at 15:37
  • 1
    _It did not compile_: please show the error log. – Jabberwocky Mar 22 '22 at 15:38
  • there is already a query instance variable declared stack overflow just would'nt let me post entire code – Timothy Mar 22 '22 at 15:38
  • what is an example of fgets thank you fred! – Timothy Mar 22 '22 at 15:39
  • 1
    @Timothy: Click the link. – Ry- Mar 22 '22 at 15:39
  • @Timothy google _c fgets_, and you should find what you need – Jabberwocky Mar 22 '22 at 15:39
  • 1
    1. Throw away anything that tells you to use `gets` and read a modern book: [The Definitive C Book Guide and List](https://stackoverflow.com/q/562303/995714). 2. Learn how to research before asking and learn how to read documentation. Read [ask] and [tour] to know how this site works. You don't post the whole code here but a [mcve] – phuclv Mar 22 '22 at 15:42
  • @phuclv The very first book on that list teaches you to use `gets`... It's not a list of _recommended_ books, it's a list of _existing_ books (and other things) of diverse quality. – Lundin Mar 22 '22 at 15:44
  • @Lundin that's why I said *modern*. There are good old books but it's not for everyone – phuclv Mar 22 '22 at 15:45
  • 1
    A corner problem with `fgets()` over `gets()` is that `fgets()` requires a 1 byte larger buffer to cope with the same size maximal input as `gets()` does not need space for the `'\n'`, `fgets()` does. – chux - Reinstate Monica Mar 22 '22 at 15:53

2 Answers2

0

Here is an example using fgets:

char query[50];

printf("\nWhich minute to query?");
fgets(query,sizeof query,stdin);
val = strcmp (query, out);

return 0;
SGeorgiades
  • 1,771
  • 1
  • 11
  • 11
0

Another example of fgets(), from my read_stdin_fgets_string_and_arrow_keys.c file in my eRCaGuy_hello_world repo:

    printf("Press any key followed by Enter, or just Enter alone, to continue: ");
    char buf[1024];
    char* retval = fgets(buf, sizeof(buf), stdin);
    if (retval == NULL)
    {
        printf("fgets() failed!\n");
    }
    // `buf` now contains the contents of what you typed in
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265