Problems scanning strings within the switch (C) command

Asked

Viewed 180 times

1

I’m trying to make a program that encodes and decodes strings that are given by the user by the César cipher method, using switch-case to select what should be done, but when trying to read a string within the case (only within the case does this occur) 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 of 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 is like this:

        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;
  • 1

    Are you sure the input buffer is really empty? There’s no ' n' left from any previous reading there?

1 answer

0


Well, since there is only 1 case in the presented code, I cannot suggest you consider working with other conditional operators (if-elseifs-Else).

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

What happens is that scanf does not exactly behave in the way that is apparent to us. This "reads" function of a "bufferized" input channel, called stdin. "Bufferized" means that this channel does not let its program "scan" as long as a new line is read, that is, as long as the user does not press "Enter". However, the function family scanf does not look for new lines (does not check), and treats the new-line character as a normal character. What may be happening, is that the scanf may be reading only the \n, and so it appears that the function (scanf) is not called.

I suggest you use fgets to read an entire row of the standard input and then analyze such a line with sscanf. With this, it gets rid of the problem of the input appearing to be "non-synchronized" and also allows "Scaneie" a line several times, despite using alternative input syntax.

So, I will pass an "example" version, instead of copying all your code, so I will be able to explain 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);
          .
          .
          .
}
}

Browser other questions tagged

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