我只是使用CodeBlocks在C语言中构建一个简单的计算器。构建并运行它之后,它一直运行到scanf()语句为止。在我输入2个数字供我的程序扫描并点击输入后,程序崩溃并发送了一条消息“ C.exe已停止工作。”请帮助。这是代码-
#include <stdio.h>
#include <stdlib.h>
int Calculator();
int main()
{
Calculator();
return 7;
}
int Calculator()
{
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");
scanf("%d", &operation);
switch (operation)
{
case 1:
printf("OK. You have chosen addition. \nEnter any 2 numbers to be added \n");
scanf("%d", num1);
scanf("%d", num2);
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");
scanf("%d", num1);
scanf("%d", num2);
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");
scanf("%d", num1);
scanf("%d", num2);
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");
scanf("%d", num1);
scanf("%d", num2);
printf("%d ÷ %d is = %d. \n Thank you. ", num1, num2, num1 / num2);
break;
default:
printf("\nHUH?\n\n");
break;
}
return 20;
}
答案 0 :(得分:2)
您在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
的{{1}}语句
答案 1 :(得分:2)
您要
scanf("%d", &num1);
scanf("%d", &num2);
在每个实例上。
此外,如果要防止其他崩溃,请注意除以零:)