Doubt written in code

Asked

Viewed 37 times

-1

//Help, I need to make a menu with the C language switch option, but when I choose one of the options it //ends automatically, is to give the option to choose other dishes and consequently adding the // value and writing at the end..... I appreciate the help

#include <stdio.h>

int main()
{
    int opcao = 0;
    float totalPedidos = 0.0;
    
    
        printf("\n--------------------------------\n");
        printf("\n--       Lanches Outlast      --\n");
        printf("\n-- Queremos o melhor de você  --\n");
        printf("\n--   1 - DEDO DE MOÇA - R$ 15,00  --\n");
        printf("\n--   2 - PASTEL DE FRANGO - R$ 5,00  --\n");
        printf("\n--   3 - SUCO SABOR LARANJA - R$ 4,00      --\n");
        printf("\n--   4 - HIENA MAL PASSADA - R$ 20,00      --\n");
        printf("\n--   5 - JAVALI NA BRASA - R$ 40,00      --\n");
        printf("\n--   0 - TENTAR SAIR                 --\n");  
        scanf("%i", &opcao);
        
        switch( opcao )
    {
    
        case 1:
            totalPedidos = 15.00 + totalPedidos;
        break;
        case 2:
            totalPedidos = 5.00 + totalPedidos;
        break;
        case 3:
            totalPedidos = 4.00 + totalPedidos;
        break;
        case 4:
            totalPedidos = 20.00 + totalPedidos;
        break;
        case 5:
            totalPedidos = 40.00 + totalPedidos;
        break;
        case 0: 
            printf:("\nSeu pedido ficou em %f\n", totalPedidos);
        break;
        default:
            printf("\nNão temos essa oferta\n");    
        break;
        

    } 
    return = 0;
}
  • 3

    The program ends because you didn’t put it in a loop, put him on a loop do{}while() and remove the case 0 leave it to the while

  • Could you give me an example?... if possible obeviamente

  • See @Paulo Martins' reply

1 answer

0


The switch is a conditional structure, just like the if and Else. All it does is test the displayed code and return a value. If you want your code to run several times you need to run a repeat structure. In c you have the option of For and While. In the example below the of runs the action for the first time, and then runs again until you type 0. But if you’re new to programming and haven’t learned structure repeats don’t worry about it now, you’ll get there!

do{
    printf("\n--------------------------------\n");
    printf("\n--       Lanches Outlast      --\n");
    printf("\n-- Queremos o melhor de você  --\n");
    printf("\n--   1 - DEDO DE MOÇA - R$ 15,00  --\n");
    printf("\n--   2 - PASTEL DE FRANGO - R$ 5,00  --\n");
    printf("\n--   3 - SUCO SABOR LARANJA - R$ 4,00      --\n");
    printf("\n--   4 - HIENA MAL PASSADA - R$ 20,00      --\n");
    printf("\n--   5 - JAVALI NA BRASA - R$ 40,00      --\n");
    printf("\n--   0 - TENTAR SAIR                 --\n");  
    scanf("%i", &opcao);
        switch(opcao)
        {
    
            case 1:
                totalPedidos = 15.00 + totalPedidos;
            break;
            case 2:
                totalPedidos = 5.00 + totalPedidos;
            break;
            case 3:
                totalPedidos = 4.00 + totalPedidos;
            break;
            case 4:
                totalPedidos = 20.00 + totalPedidos;
            break;
            case 5:
                totalPedidos = 40.00 + totalPedidos;
            break;
            case 0: 
                printf("\nSeu pedido ficou em %f\n", totalPedidos);
            break;
            default:
                printf("\nNão temos essa oferta\n");    
            break;
            
    
        } 
    } while(opcao!=0);//repita enquanto a opcao for diferente de 0
  • opa obrg.... i learned the repeating structures...the switch for me is a new matter...but I’m having a hard time organizing it correctly...again obrg

  • The switch is nothing more than multiple if, Else if written in a different way! You can replace case 1: to if(option=1){} for example. Just remember she needs the break at the end

Browser other questions tagged

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