Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

121
Visualizações
program in c with switch command about two numbers and character

Write a program that asks for two numbers and a character respectively with the operation he will perform with the two numbers. If any other character is typed then the message is displayed "Wrong act". To be implemented using the switch-case command.

    #include <stdio.h>
main ()
{
int selection;
printf("Enter 2 numbers \n");
scanf("%d",&selection);
switch(selection)
{
case 1:
printf("You entered 1");
break;
case 2:
printf("You entered 2");
break;
case 3:
printf("You entered 3");
break;
default:
printf("Out of range, Try again");
}
}

Can anyone give me some advice about what to do in the switch funtcion on this particular problem?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Some Tips:

  • Every function in C must have a data type like type func() { }
  • formatting of your source code is very important
  • printf("Enter 2 numbers \n");, but in the following line you only take a single input
  • Well, C implicitly return 0, but you should write it (it may differ from compiler to compiler)
  • Must check whether scanf() input was successful or not, by checking its return value
  • Use puts() to print non-formatted text on terminal (just a block of text), and it will also print a new line [GCC and CLang automatically does that if optimization flags are used]

Final Code:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int selection;
    puts("Enter a number:");
    if(scanf("%d", &selection) !=  1)
    {
        perror("bad input");
        return EXIT_FAILURE;
    }
    switch (selection) {
    case 1:
        puts("You entered 1");
        break;
    case 2:
        puts("You entered 2");
        break;
    case 3:
        puts("You entered 3");
        break;
    default:
        puts("Out of range, Try again");
    }
    return EXIT_SUCCESS;
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda