I am developing a code to encrypt in the cesar cipher. The decryption is working perfectly, but to encrypt, fgets
does not expect me to type the text. I have tried everything, the only command that works is: scanf ("% s", & cif), but this command only reads a word and I need to read a text.
(% com_with% problem is 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);
}