gpt4 book ai didi

C编程(程序不跳出循环)

转载 作者:行者123 更新时间:2023-11-30 21:17:53 25 4
gpt4 key购买 nike

这是我的程序的输出。即使条件已经满足,仍然没有跳出循环enter image description here

这是我的程序。第一个 while 循环询问用户是否拥有汽车。

#include <stdio.h>
#include <conio.h>
#define TRANSPORT 1


int main()
{
char PrivateVehicle;

printf("DRIVING ANF FLYING CARBON FOORPRINT \n");

printf("I. LAND \n");

printf("A. PRIVATE OWNED \n");

while (TRANSPORT==1)
{
do
{
printf("Question 1: \n");
printf("Do you have your own private vehicle? [Y/y]-Yes, [N/n]-No \n");
printf("Note: Only the letters Y, y for yes and N, n for no would be accepted \n");
scanf (" %c", &PrivateVehicle);

if (PrivateVehicle!='Y' && PrivateVehicle!='y' && PrivateVehicle!='N' && PrivateVehicle!='n')
printf("Inavlid \n");
} while (PrivateVehicle!='Y' && PrivateVehicle!='y' && PrivateVehicle!='N' && PrivateVehicle!='n');

if (PrivateVehicle=='Y'|| PrivateVehicle=='y') // should go out of the loop after condition is met
printf("Y/y\n"); //For checking

else if (PrivateVehicle=='N'|| PrivateVehicle=='n') // should go out of the loop after condition is met
printf("N/n\n"); //For checking

TRANSPORT==0;

}

printf("Out"); //For checking

printf("B. PUBLIC TRANSPORT \n");
}

最佳答案

您不能分配给宏标识符

 TRANSPORT == 0;

(即使使用 =“修复”==)。如果您需要更改某些值,例如,您需要使用变量int TRANSPORT = 0; 而不是 #define 指令。

非终止循环的原因是宏展开后,你有

 while (1==1)

这始终是正确的。

关于C编程(程序不跳出循环),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33593159/

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