0

I'm trying to get the gets() function to work on a given string. Everything else works properly up to this point.

 char nads[100];

   gets(nads);
   printf("%s", nads);

The rest of the code follows, but I can't see why it would cause an issue.

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

void problema_1 (void);
void problema_2 (void);

int main()
{
    setlocale(LC_ALL, "Portuguese_Brazil");

    INICIO: printf("Para visualizar as respostas dos desafios, digite o número do mesmo : ");
    int problema;
    scanf("%i", &problema);
    if (problema==1)
        {
            problema_1();
        }
    else if (problema==2)
        {
            problema_2();
        }
    else
        {
            printf("Escolha um valor válido e reinicie\n");
            goto INICIO;
        }
    return 0;
}

void problema_1(void)
{    
   char nads[100];

   gets(nads);
   printf("%s", nads);    
}

void problema_2(void)
{
    printf("Resolução problema 2");
    FILE *matriz;

    matriz = fopen("matriz.txt", "r");
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • How do you know it does not work? – klutt Nov 23 '17 at 21:32
  • 2
    [Why `gets()` is too dangerous to be used — ever!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used). – Jonathan Leffler Nov 23 '17 at 21:33
  • Note that `problema_2()` leaks the file stream it opens. How have you determined that it doesn't work? You should have shown that in the question. If you'd entered `1 elephant` as the first input, you'd see what's going on. – Jonathan Leffler Nov 23 '17 at 21:35
  • Well, it runs everything but the {gets()}. It might be the compiler, but turning it off and on againd didn't work. If i get the function {problema_1} and run it as a main(), it works. – Matthias NKR Nov 23 '17 at 21:49
  • I can't find the issue – Matthias NKR Nov 23 '17 at 21:49

0 Answers0