Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

122
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda