Simple IF statement scanf

``````#include <stdio.h>

int main()
{
float cm;
float inches;
int operation;

printf("Hello and welcome to the Inches to Centimetres converter. \n");
printf("Choose from the options below by entering the corresponding number:\n");
printf("Inches to CM converter (1)\n");
printf("CM to Inches converter (2)\n");
scanf("&d", &operation);

if (operation == 1)
{
printf("Please enter the amount of Inches you wish to convert : \n");
scanf("%f", &inches);

cm = inches * 2.54;

if (inches <= 0)
{
printf("Invalid number");
}
else
printf("%f inches is equal to %f centimetres.", inches, cm);
}
else if (operation == 2);
{
printf("Please enter the amount of Centimetres you wish to convert : ");
scanf("%f", &cm);

inches = cm / 2.54;

if (cm <= 0)
{
printf("Invalid number");
}
else
printf("%f centimetres is equal to %f inches.", cm, inches);
}

}
``````

1 个回复

``````scanf("&d", &operation);
``````

``````scanf("%d", &operation);
``````

``````}
else if (operation == 2);
{
``````

`;` 立即结束`else`块。 因此，大括号中的块将始终运行。 摆脱`;`

``````}
else if (operation == 2)
{
``````

``````} else if (operation == 2) {
``````

1 scanf语句的参数

2011-04-27 08:46:58 1 470   c
2 C错误，简单的scanf函数，结果为0

2017-09-28 22:49:08 3 71   c/ scanf
3 嵌套的If语句忽略scanf函数

4 scanf在此简单程序中如何工作？

2014-01-28 18:48:08 2 159   c/ scanf
5 简单的scanf不会设置变量值

2015-05-19 23:18:23 1 155   c/ scanf
6 用简单的等式替换scanf会使程序崩溃

2015-06-07 21:22:49 2 547   c/ atoi
7 scanf语句后我的C代码没有响应

2014-02-19 04:04:01 1 3374   c/ scanf
8 C ++，其他变量受scanf语句的影响

2014-10-04 19:52:04 1 118   c++/ scanf
9 我将如何编写只接受某些字符的scanf（）语句？

10 我的程序在 scanf 语句之后不起作用

2018-02-15 17:11:13 1 96   c++