【发布时间】:2021-06-01 17:02:16
【问题描述】:
我正在尝试一个简单的 C 代码并发现奇怪的错误。这是代码。只要第一个
scanf()
遇到函数,编译器会自动跳过它并继续前进。为什么会这样?
#include<stdio.h>
#include<stdlib.h>
void main()
{
float base_cost, refresh_cost;
int ticket;
char coupon, refresh, circle;
printf("Enter number of tickets: ");
scanf("%d", &ticket);
if(ticket<5 ||ticket>40)
{
printf("Minimum of 5 and Maximum of 40 Tickets");
exit(0);
}
printf("Do you want refreshment?(Y/N): "); //Here compiler skips
scanf("%c", &refresh);
printf("Do you have coupon?(Y/N): ");
scanf("%c", &coupon);
printf("Enter circle: ");
scanf("%c", &circle);
if(circle!='k'||circle!='q' || circle!='K' || circle!='Q')
{
printf("Invalid Input");
exit(0);
}
else if(circle == 'k' || circle == 'K')
{
if(ticket > 20)
base_cost = (ticket*75.00) - 0.1*(ticket*75.00);
else if(coupon == 'y' || coupon == 'Y')
base_cost = (ticket*75.00) - 0.02*(ticket*75.00);
else
base_cost = ticket*75.00;
}
else if (circle == 'q' || circle == 'Q')
{
if(ticket > 20)
base_cost = (ticket*150.00) - 0.1*(ticket*150.00);
else if(coupon == 'y' || coupon == 'Y')
base_cost = (ticket*150.00) - 0.02*(ticket*150.00);
else
base_cost = ticket*150.00;
}
if (refresh == 'y' || refresh == 'Y')
{
refresh_cost = ticket * 50.00;
}
else
refresh_cost = 0.00;
printf("\nTotal cost: %0.2f", base_cost + refresh_cost);
}
我使用变量 refresh 是为了让用户可以选择是否插入元素,即 Y 或 N。 此外,看起来其他以 char 作为输入的 scanf 函数也被跳过了。 但是编译器基本上跳过了第三个 scanf 函数,即接受 char 的函数,以及 while 循环。
【问题讨论】:
-
为什么这个标签是
c++17? -
编译时,始终启用警告,然后修复这些警告。
标签: c