Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - gets not working

Tags:

c

gets

I'm having a problem with gets() working. The thing is it should be working with just a warning from vars I am not using but the first gets just skips to the second.

int addClube(char *fileName)
{

        char qClube2[100], qClubeSimple[100], qEstadio[100], qCompleto[500],qAcronimo[100];
        int option=0;

        printf("\n  Indique o nome completo do clube:\n  (sem acentos)\n  ");
        gets(qClube2);

        printf("\n  Indique o nome simplificado do clube:\n  (sem acentos)\n  ");
        gets(qClubeSimple);

        printf("\n  Indique o acronimo do clube:\n  ");
        scanf("%s",qAcronimo);

        printf("\n  Indique o nome do estadio:\n  ");
        gets(qEstadio);

        return option;
}

The output I get when the function starts:

Indique o nome completo do clube:
(sem acentos)

Indique o nome simplificado do clube:
(sem acentos)    

Once again thanks in advance for the help an if you more info just tell me.

like image 202
Rafael Botas Avatar asked Nov 28 '25 15:11

Rafael Botas


1 Answers

If you need to clear the input stream, fflush(stdin) will not do the job. It is my understanding that it works on Windows, but most other systems (UNIX, Linux) it is undefined behavior.

I wrote this on the fly, so it may or may not work, but essentially you need to discard all data you don't want. The following discards a line from the input buffer.

void clearinline(void)
{
    int c;
    do {
        c = getchar();
    } while(c != '\n' && c != EOF)
}
like image 179
PC Luddite Avatar answered Nov 30 '25 04:11

PC Luddite



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!