Loop problem with switch case

Asked

Viewed 1,393 times

1

I reread the code following the guidelines given here. However, it has had the initial problem again... I choose the desired option, it performs the desired action but soon after it goes to the default of the switch case and prints "invalid information"

My code is like this:

#include <string.h>
#include <stdio.h>
#define TAM 20
#include <ctype.h>

int menu(){
    char opc;

    printf("| A | Ler string\n");
    printf("| B | Tamanho da string1\n");
    printf("| C | Comparar strings\n");
    printf("| D | Concatenar strings\n");
    printf("| E | Imprimir string1 invertida\n");
    printf("| F | Quantidade de ocorrencias de um caractere na string1\n");
    printf("| G | Subsituir o primeiro caractere1 pelo caractere2\n");
    printf("| H | Retornar substring da string1\n");
    printf("| I | Sair\n");
    scanf("%c", &opc);

    return tolower(opc);
}


void pedirstr1(char *str1){

        printf("Entre com a string1: ");
        fflush(stdin);
        fgets(str1, TAM, stdin);
        str1[strlen(str1)-1] = '\0';
}

void pedirstr2(char *str2){

        printf("Entre com a string1: ");
        fflush(stdin);
        fgets(str2, TAM, stdin);
        str2[strlen(str2)-1] = '\0';
}


int main(){
    char str1[TAM] = " NULL ", str2[TAM] = " NULL ", char1, char2, c, opc = 'f', sair=0, c2, junto[TAM];
    int inicio, fim, contchar = 0;


    do {
        opc = menu();
        printf("\n\n");

        switch(opc){

        case 'a':
                //system("CLS");

                pedirstr1(str1);

                break;

        case 'b':
                //system("CLS");

                if (strcmp(str1, " NULL ") == 0){
                    printf("Nao existe nenhum valor na string1.\n");
                    printf("\n\n");
                } else {
                    printf("O tamanho da string1 e: %d\n", strlen(str1));
                    printf("\n\n");
                }

                break;

        case 'c':
                //system("CLS");

                pedirstr1(str1);
                pedirstr2(str2);

                if (strcmpi(str1, str2) == 0){
                    printf("As strings 1 e 2 sao iguais.\n");
                    printf("\n\n");
                } else {
                    printf("As strings 1 e 2 sao diferentes. \n");
                    printf("\n\n");
                }

                break;

        case 'd':
                //system("CLS");

                pedirstr1(str1);

                pedirstr2(str2);

                printf("string1 + string2 = %s%s\n", str1, str2); //posso usar strcat mas da ruim pq fica armazenado na variavel
                printf("\n\n");

                break;

        case 'e':
                //system("CLS");

                pedirstr1(str1);

                printf("String1 invertida: ");
                for (c=strlen(str1); c>=0; c--){
                    printf("%c", str1[c]);
                }

                printf("\n\n");

                break;

        case 'f':
                //system("CLS");

                pedirstr1(str1);

                printf("Letra que deseja contar: ");
                scanf("%c", &char1);

                for (c=0; c<strlen(str1); c++){
                    if (str1[c] == char1){
                        contchar ++;
                    }
                }

                printf("A letra %c aparece %d vezes na string1.\n", char1, contchar);
                printf("\n\n");

                break;

        case 'g':
                //system("CLS");

                pedirstr1(str1);

                printf("Entre com o caractere que deseja substituir: [diferencie maiusculas de minusculas]");
                scanf("%c", &char1);

                printf("Qual caractere deseja colocar no lugar? ");
                scanf("%c", &char2);

                for (c=0; c<strlen(str1); c++){
                    if (str1[c] == char1){
                        str1[c] = char2;
                    }
                }

                printf("Nova string: %s", str1);
                printf("\n\n");

                break;

        case 'h':
                //system("CLS");

                pedirstr1(str1);

                printf("A partir de qual posicao? ");
                scanf("%d", &inicio);

                printf("Até onde? ");
                scanf("%d", &fim);

                while (inicio > fim){
                    printf("A posicao de inicio deve ser menor do que a do fim. \n");

                    printf("A partir de qual posicao? ");
                    scanf("%d", &inicio);

                    printf("Ate onde? ");
                    scanf("%d", &fim);
                }

                printf("Substring: ");
                for (c=inicio; c<fim; c++){
                    printf("%c", str1[c]);
                }

                printf("\n\n");

                break;
        case 'i':
                sair = 1;

                break;

        default:
                //system("CLS");

                printf("Informacao invalida, tente novamente. \n");
                printf("\n\n");

                break;
        }
    } while (!sair);

    return 0;
}
  • 1

    If you put a fflush(stdin); after the scanf of function menu, what happens? What if you put before?

  • AAAAA WORKED

2 answers

2

Add

getchar();

After

scanf("%c", &opc);

When you choose an option that is not read and key ENTER. He perform the operation you chose and understands the ENTER that you gave as an option you typed too, with this it enters the default.

2


You got the opc = menu(); out of your do while. In other words, your variable opc does not update after each cycle stays the same and keeps the first value entered.

Squeal:

do {
    opc = menu();
    switch(opc){

    //resto do codigo

    }
} while (!sair);
  • You are wearing "NULL" or is the string null and not the value NULL and how you’re doing the strcmp() and you have different number of spaces in your string "NULL" will set the wrong example: "NULL " != " NULL". Uses NULL value and not a string with a word, str1[TAM] = NULL.

  • I tried to put str1[TAM] = NULL, but gave the error "invalid initializer". So I decided to take the test to see if there was something in the string, I left it just asking. however continues with the same problem mentioned above. so I tested by already putting a value in opc. for ex opc = 'f'. That way it worked! pq when I ask the user to enter the option it does not work properly?

  • char str1[TAM] str2[TAM], and so? Supposed to be initiated by default to NULL

  • But in what case it doesn’t work specifically?

  • I edited the question, because it is easier to explain by there

  • scanf("%c", &opc); why do you have &opc and not only opc?

  • but if you don’t put & it doesn’t keep in memory

  • I’ve been watching and I can’t find any more mistakes, wait for someone else to come take a look, sorry I can’t help more :(

  • 1

    all right, no problem. anyway, thank you so much

  • Try giving a print to opc, str1, str2 before the case to see what they have as stored values this may help to debug more easily.

  • And by the way ask stupid question of this Compile after you’ve made the changes, you’re not just running exec?

  • kkkk wanted the problem to be this. I even checked here to be 100% sure... no and this ;-;

  • You never know, it wouldn’t be the first time xD, but print the option after selecting the option.

Show 8 more comments

Browser other questions tagged

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