fgets does not work

Asked

Viewed 924 times

-1

I’m developing a code to encrypt in the cesar cipher. The decryption is working perfectly, but to encrypt, the fgets Don’t wait for me to type in the text. I’ve tried everything, the only command that works is: scanf("%s", &cipher), but this command only reads a word and I need to read a text.

(the fgets with problem this pointed with an arrow "--->").

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

void menu (void);
void cript(void);
void descript (void);


int numero;
char cifra[50];
int i,tam;
FILE *arq1,*arq2;

int main()
{
    menu();
    return 1;
}

void menu (void)
{
    do
    {
        system("cls");
        printf("\n\n     Critografia utilizando Cifra de Cesar\n\n");
        printf("     1-Critografar Texto\n");
        printf("     2-Descriptografar mensagem\n");
        printf("     3-Sair\n\n");
        printf("     Escolha a opcao desejada: ");
        scanf("%d", &numero);

        switch(numero)
        {
            case 1: 
                cript();
            break;

            case 2:
                descript();
            break;

            case 3:
                system("cls");
                printf("\n\n     Saindo...\n\n");
                system("pause");
                exit(0);
            break;

            default:
                printf("Opcao Invalida!");
            break;

        }

    }while(numero!=3);
}

void cript (void)
{
    int numero;
    char cifra[50];
    int i,tam;
    FILE *arq1,*arq2;

    system("cls");
    printf("\n\n     Informe o texto: ");
    **--->fgets(cifra,50,stdin);**

    tam = strlen(cifra);

    arq1 = fopen("cript.txt","w");

    for(i=0; i<tam; i++)
    {
        if(cifra[i] == 'z')
        {
            cifra[i] = 'c';
            putc(cifra[i],arq1);
        }
        else
            if(cifra[i] == 'y')
            {
                cifra[i] = 'b';
                putc(cifra[i],arq1);
            }
            else
                if(cifra[i] == 'x')
                {
                    cifra[i] = 'a';
                    putc(cifra[i],arq1);
                }
                else
                    if(cifra[i] == 'Z')
                    {
                        cifra[i] = 'C';
                        putc(cifra[i],arq1);
                    }
                    else
                        if(cifra[i] == 'Y')
                        {
                            cifra[i] = 'B';
                            putc(cifra[i],arq1);
                        }
                        else
                            if(cifra[i] == 'X')
                            {
                                cifra[i] = 'A';
                                putc(cifra[i],arq1);
                            }
                            else
                                if(cifra[i] == ' ' || cifra[i] == '\n')
                                    putc(cifra[i],arq1);
                                else
                                    putc(cifra[i] +3,arq1);
    } 
    fclose(arq1);
}



void descript (void)
{
    int numero;
    char cifra[50];
    int a,tam;
    FILE *arq1,*arq2;

    system("cls");
    arq1 = fopen("cript.txt","r");
    fgets(cifra,50,arq1);

    tam = strlen(cifra);

    arq2 = fopen("descript.txt","w");

    for(a=0; a<tam; a++)
    {
        if(cifra[a] == 'c')
        {
            cifra[a] = 'z';
            putc(cifra[a],arq2);
        }
        else
            if(cifra[a] == 'b')
            {
                cifra[a] = 'y';
                putc(cifra[a],arq2);
            }
            else
                if(cifra[a] == 'a')
                {
                    cifra[a] = 'x';
                    putc(cifra[a],arq2);
                }
                else
                    if(cifra[a] == 'C')
                    {
                        cifra[a] = 'Z';
                        putc(cifra[a],arq2);
                    }
                    else
                        if(cifra[a] == 'B')
                        {
                            cifra[a] = 'Y';
                            putc(cifra[a],arq2);
                        }
                        else
                            if(cifra[a] == 'A')
                            {
                                cifra[a] = 'X';
                                putc(cifra[a],arq2);
                            }
                            else
                                if(cifra[a] == ' ' || cifra[a] == '\n')
                                    putc(cifra[a],arq2);
                                else
                                    putc(cifra[a] -3,arq2);
    } 
        fclose(arq2);
}
  • And what’s the problem? Be more specific.

2 answers

1

Add a getchar() to read Enter from the keyboard.

    ...
    printf("     Escolha a opcao desejada: ");
    scanf("%d", &numero);
    getchar();

    switch(numero)
    {
        case 1:
    ...
  • Thanks man, that’s exactly what I needed.

-3

It can be something locked in memory, try using the "fflush(stdin)" command before using the fgets command.

  • fflush(stdin) has undefined behavior and is not guaranteed to always work, everywhere, for any environment (see more here, here e - why not? - here :-D)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.