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?
C must have a data type like type func() { }printf("Enter 2 numbers \n");, but in the following line you only take a single inputC implicitly return 0, but you should write it (it may differ from compiler to compiler)scanf() input was successful or not, by checking its return valueputs() 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]#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;
}