Ваши scanf
звонки внутри switch
неверны. Вам нужно &
.
Изменение
scanf("%d", num1);
scanf("%d", num2);
до
scanf("%d", &num1);
scanf("%d", &num2);
Предлагаю вам проверить возврат scanf
.
Попытайтесь понять это:
#include <stdio.h>
#include <stdlib.h>
int Calculator( void );
int main( void )
{
Calculator();
}
int Calculator( void )
{
int num1;
int num2;
int operation;
printf("Hello. I am a calculator.\nChoose any operation you want to carry out- \n 1.Addition \n 2.Subtraction \n 3.Multiplication \n 4.Division \n");
if ( scanf("%d", &operation) != 1 ){
printf( "Error scanf() ==>> on Operation" );
exit( 1 );
}
switch (operation)
{
case 1:
printf("OK. You have chosen addition. \nEnter any 2 numbers to be added \n");
if ( scanf("%d", &num1) != 1 ){
printf( "Error scanf() ==>> switch - case 1 ==>> 1" );
exit( 1 );
}
if ( scanf("%d", &num2) != 1 ){
printf( "Error scanf() ==>> switch - case 1 ==>> 2" );
exit( 1 );
}
printf("%d + %d is = %d. \n Thank you. ", num1, num2, num1 + num2);
break;
case 2:
printf("OK. You have chosen subtraction. \nEnter any 2 numbers to be subtracted \n");
if ( scanf("%d", &num1) != 1 ){
printf( "Error scanf() ==>> switch - case 2 ==>> 1" );
exit( 1 );
}
if ( scanf("%d", &num2) != 1 ){
printf( "Error scanf() ==>> switch - case 2 ==>> 2" );
exit( 1 );
}
printf("%d - %d is = %d. \n Thank you. ", num1, num2, num1 - num2);
break;
case 3:
printf("OK. You have chosen multiplication. \nEnter any 2 numbers to be multiplied \n");
if ( scanf("%d", &num1) != 1 ){
printf( "Error scanf() ==>> switch - case 3 ==>> 1" );
exit( 1 );
}
if ( scanf("%d", &num2) != 1 ){
printf( "Error scanf() ==>> switch - case 3 ==>> 2" );
exit( 1 );
}
printf("%d x %d is = %d. \n Thank you. ", num1, num2, num1 * num2);
break;
case 4:
printf("OK. You have chosen division. \nEnter any 2 numbers to be divided \n");
if ( scanf("%d", &num1) != 1 ){
printf( "Error scanf() ==>> switch - case 4 ==>> 1" );
exit( 1 );
}
if ( scanf("%d", &num2) != 1 ){
printf( "Error scanf() ==>> switch - case 4 ==>> 2" );
exit( 1 );
}
printf("%d ÷ %d is = %d. \n Thank you. ", num1, num2, num1 / num2);
break;
default:
printf("\nHUH?\n\n");
break;
}
return 20;
}
Вам следует пересмотреть свои return
заявления от main
и Calculator