Problems scanning strings inside the switch command (C)

0

I am trying to make a program that encodes and decodes strings that are given by the user by the cesar method, using switch-case to select what should be done, however when trying to read a string inside the case of the case this occurs) the program ignores the scanf and goes straight to the next line. I already tried everything I knew and nothing worked, any help would be welcome.
The text and message strings are both size 50, alf is a string that contains the alphabet, key is an int that is the number of houses that are advanced in the given message.
My code looks like this: / p>

        case 1:
        printf("Digite sua Mensagem\n");
        scanf("%[^\n]s",texto);
        strcpy(mensagem,texto);
        printf("Digite a Chave de Codificação\n");
        scanf("%d",&chave);
        for(i=0;i<strlen(texto);i++) //loop p/ cada caractere da mensagem inicial
            for(j=0;j<strlen(alf);j++){ //loop p/ cada caractere da mensagem inicial
                if(texto[i]==alf[j]){
                    mensagem[i]=alf[j+chave];
                    break;
                }else if(texto[i]==toupper(alf[j])){
                    mensagem[i]=toupper(alf[j+chave]);
                    break;
                }else if(isspace(texto[i]))
                    break;
            }
        system("cls");
        printf("A Mensagem Encriptada É:\n");
        printf("%s\n",mensagem);
        system("pause");
    break;
    
asked by anonymous 27.05.2017 / 20:28

1 answer

0

Well, since there's only 1 case in the code presented, I can not suggest that you consider working with other if-elseifs-else.

Anyway, the problem seems to be related to "scanf" and "\ n". Although it is a "kick" that I am giving, it can work because these two elements are often the main reason for various problems.

What happens, scanf does not behave exactly in the way it is apparent to us. This function "reads" from a "buffered" input channel, called stdin . By "buffered", it is understood that this channel does not let its program "scan" while a new line is read, that is, while the user does not press "Enter". However, the scanf family of functions does not look at newlines (does not check), and treats the newline character as a normal character. What may be happening is that scanf may be reading only the \ n , and so it appears that the scanf is called.

I suggest you use fgets to read an entire line of the default entry and then parse that line with sscanf . This frees up the input problem by appearing to be "non-synchronized" and also allows you to "scan" a line multiple times, despite using alternate input syntaxes.

So, I'm going to pass an "example" version, instead of copying all your code, so I'll be able to explicit the #includes and created variables:

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

int main() {

char *texto_constant;
char texto_buf[100];
int whatever[100], i; /* No caso não há necessidade desse array                                        
                         mas deixarei como exemplo adicional */

printf("Introduza um inteiro: ");
fgets(whatever, 100, stdin);
sscanf(whatever, "%d", &i);

switch (i)
{
   case 1:
    printf("Digite sua Mensagem");
    fgets(texto_buf, 100, stdin);
    sscanf(texto_buf, "%100s", texto_constant); /* Aqui, até onde sei é  
                                                necessário explicitar o                                                                                             
                                                No. de chars. Caso contrário, me corrijam por favor. */
    strcpy(mensagem, texto_constant);
          .
          .
          .
}
}
    
28.05.2017 / 05:28