gpt4 book ai didi

c - 我的程序在到达 C 中的 scanf() 语句后崩溃并停止工作

转载 作者:行者123 更新时间:2023-11-30 20:47:59 24 4
gpt4 key购买 nike

我只是使用 CodeBlocks 用 C 语言构建了一个简单的计算器。构建并运行它之后,一切都很顺利,直到 scanf() 语句为止。在我输入 2 个数字供程序扫描并按 Enter 键后,程序崩溃并发送消息“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;
}

最佳答案

您在 switch 内的 scanf 调用是错误的。您需要&

更改:

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;
}

您应该重新考虑 mainCalculator 中的 return 语句

关于c - 我的程序在到达 C 中的 scanf() 语句后崩溃并停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51174095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com