gpt4 book ai didi

c - 请向我解释为什么这个带有 if 语句的简单 C 程序不起作用?

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

我刚刚了解了 if 语句并尝试制作某种计算器,但它不起作用。它要求您输入一个运算(目前只能进行加法),然后要求输入两个整数。 super 简单,但是行不通。这个错误对你们来说可能是显而易见的,但我就是看不到。请帮忙!代码如下:

int main()
{
int operation;
int addition;
float firstNumber;
float secondNumber;
printf("Type in an operation.\n");
scanf(" %s", operation);


if(operation = addition){
printf("Please, enter an integer.\n");
scanf(" %f", &firstNumber);

printf("Please, enter a second integer.\n");
scanf(" %f", &secondNumber);

printf("Answer: %d", firstNumber + secondNumber);
}else{
printf("Sorry, only addition works..");
}
return 0;
}

最佳答案

为什么你的代码应该有效?

int operation; 
/* int for storing a string? huh?
* or were you thinking about function pointers ?
* Normally you would use a char[]
*/


scanf(" %s", operation);
/* Using %s specifier looks weird .
* Also scanf is reading into operation and not &operation
* So is having a space in the in the beginning of the format string.
*/

int addition;
/* Automatic variables are not initialized as per the standard
* But what about the type?
* Were you intending to do something like char addition[]="addition"
*/

if(operation = addition)
/* If you somehow manage to get to this point
* you have another problem you do an assignment in the statement using =
* You should have been using ==
*/

关于c - 请向我解释为什么这个带有 if 语句的简单 C 程序不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36805806/

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