[英]Why does my loop dont terminate when I wanted to?
我輸入了正確的值,但循環仍在繼續! 幫我
int N;
int B;
float A;
bool condition = (N < 1 || N > 1000) || (A <= 0 || A >= 1) || (B < 1 || B > 1000000000);
do
{
printf("Enter N: ", "Enter A: ", "Enter B: ");
scanf("%s %d %s %.3f %s %d", &N, &A, &B);
}
while(condition);
我嘗試了正確的值,我希望它停止循環
5 行許多未定義的行為。
scanf
之前僅設置一次condition
。 條件中的變量未初始化。scanf
有 6 種格式,但你只傳遞 3 個參數(錯誤的 BTW) int N;
int B;
float A;
bool condition;
do
{
printf("Enter N: ", "Enter A: ", "Enter B: ");
if(scanf("%d %f %d", &N, &A, &B) != 3) {/* handle error */}
condition = (N < 1 || N > 1000) || (A <= 0 || A >= 1) || (B < 1 || B > 1000000000);
}
while(condition);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.