C语言 为什么do-while循环在一次迭代后退出?

zpjtge22  于 2023-10-16  发布在  其他
关注(0)|答案(3)|浏览(114)

初级程序员:
我试图格式化一个dowhile循环,如果用户输入了一个0-4范围之外的错误数字,那么我的程序应该要求用户输入一个正确范围内的数字。一旦满足了参数,我希望我的代码继续运行if语句。我错过了什么?

#include <stdio.h>

int main() {

    int userNum;
    char tableOne[5] = { 'A', 'B', 'C', 'D', 'E' };
    int tableTwo[5] = { 11, 12, 13, 14, 15 };

    printf("Enter a whole number in the range of 0 to 4.\n");
    scanf("%d", &userNum);

    do {
        printf("ERROR. ENTER WHOLE NUMBER \nIN THE RANGE OF 0 - 4 \nIN NUMERICAL FORMAT.\n");
        scanf("%d", &userNum);
        break:
    } while (userNum < 0 || userNum > 4);

    if (userNum == 0) {
        printf("%d", tableOne[0] + tableTwo[0]);
    }
    if (userNum == 1) {
        printf("%d", tableOne[1] + tableTwo[1]);
    }
    if (userNum == 2) {
        printf("%d", tableOne[2] + tableTwo[2]);
    }
    if (userNum == 3) {
        printf("%d", tableOne[3] + tableTwo[3]);
    }
    if (userNum == 4) {
        printf("%d", tableOne[4] + tableTwo[4]);
    }
    return 0;
}
5uzkadbs

5uzkadbs1#

  1. break语句在scanf退出循环后立即执行,它甚至没有达到while条件检查。删除它
    1.检查scanf的结果,看看数字是否正确读取:
if(scanf ("%d", &userNum) != 1) {/* do something as user input was incorrect */}

1.你也不需要这些ifbreak也不需要-你应该在练习本上阅读或问你的老师这个语句是什么):

printf("%d", tableOne [userNum] + tableTwo [userNum]);
o8x7eapl

o8x7eapl2#

break语句在scanf()之后立即退出循环。这解释了你的观察:循环在1次迭代后退出。它实际上在验证测试之前的第一次迭代中退出。
请注意,您应该检查转换错误,使用有问题的输入,并在scanf()没有返回1的情况下重新启动输入,如果没有更多的输入,则报告文件结束。
do { ... } while循环不适合合并组合这两种形式的输入验证,因为如果存在转换错误,则有效性测试毫无意义,并且不能使用continue语句绕过它。do { ... } while循环很少是正确的工具,并且往往会嵌入逻辑错误,例如缺少条件。您应该使用for (;;)循环(也称为 for ever 循环)并显式地编写条件。
使用fgets()sscanf()的组合而不是scanf()可以实现更好的错误处理和恢复。
以下是修改后的版本:

#include <stdio.h>

int main(void) {

    int userNum;
    char tableOne[5] = { 'A', 'B', 'C', 'D', 'E' };
    int tableTwo[5] = { 11, 12, 13, 14, 15 };

    for (;; printf("Try again: ")) {
        char buf[80];
        int offset = 0;

        printf("Enter a whole number in the range of 0 to 4: ");

        // read input from the user
        if (!fgets(buf, sizeof buf, stdin)) {
            printf("Missing input: unexpected end of file. Aborting.\n");
            return 1;
        }
        // attempt to convert the input as an integer
        if (sscanf(buf, "%d %n", &userNum, &offset) != 1) {
            printf("Invalid input: not a number.\n");
            continue;
        }
        if (userNum >= 0 && userNum <= 4) {
            // value is invalid
            printf("Error: value must be in range 0 to 4: %d.\n", userNum);
            continue;
        }
        // input was successful and value is correct, check for extra input
        if (buf[offset] != '\0') {
            // this should be considered an error, for example if the user
            // typed 1.2, the extra input ".2" is definitely an error.
            printf("Extra characters: %s\n", buf + offset);
            continue;
        }
        // input was successful and value is in the expected range.
        break;
    }

    // note that the code below can be replaced with a single statement
    // printf("%d\n", tableOne[userNum] + tableTwo[userNum]);

    if (userNum == 0) {
        printf("%d\n", tableOne[0] + tableTwo[0]);
    }
    if (userNum == 1) {
        printf("%d\n", tableOne[1] + tableTwo[1]);
    }
    if (userNum == 2) {
        printf("%d\n", tableOne[2] + tableTwo[2]);
    }
    if (userNum == 3) {
        printf("%d\n", tableOne[3] + tableTwo[3]);
    }
    if (userNum == 4) {
        printf("%d\n", tableOne[4] + tableTwo[4]);
    }
    return 0;
}
pkln4tw6

pkln4tw63#

我想你需要这样的东西。你需要在while循环中放置{}。

#include <stdio.h>

int main() {

int userNum;

char tableOne [5] = {'A', 'B', 'C', 'D', 'E'};

int tableTwo [5] = {11, 12, 13, 14, 15};

printf ("Enter a whole number in the range of 0 to 4.\n");

do {
 scanf("%d", &userNum);
 printf("ERROR. ENTER WHOLE NUMBER \nIN THE RANGE OF 0-4 \nIN NUMERICAL FORMAT.\n");     scanf("%d", &userNum);
}
while (userNum < 0 || userNum > 4){

if (userNum == 0){ 
    printf("%d", tableOne [0] + tableTwo [0]); 
    break;
}

if (userNum == 1){ 
    printf("%d", tableOne [1] + tableTwo [1]);
    break;
}

if (userNum == 2){ 
    printf("%d", tableOne [2] + tableTwo [2]);
    break;
}

if (userNum == 3){ 
    printf("%d", tableOne [3] + tableTwo [3]);
    break;
}

if (userNum == 4){
    printf("%d", tableOne [4] + tableTwo [4]);
    break;
}

return 0;
 }
}

编辑

所以,现在我知道你的do while循环有一些问题。因为print是printf而不是if语句。我将搜索的东西给给予一些体面的代码。
试试这个:

#include <stdio.h>

 int main() {

 int userNum;

char tableOne [5] = {'A', 'B', 'C', 'D', 'E'};

int tableTwo [5] = {11, 12, 13, 14, 15};

printf ("Enter a whole number in the range of 0 to 4.\n");
scanf("%d", &userNum);

if (userNum < 0 || userNum > 4){
  do {
   printf("ERROR. ENTER WHOLE NUMBER IN THE RANGE OF 0-4 \nIN NUMERICAL FORMAT.\n");     
   scanf("%d", &userNum);
  }
  while (userNum < 0 || userNum > 4);
}

if (userNum == 0){ 
    printf("%d", tableOne [0] ); 
}

if (userNum == 1){ 
    printf("%d", tableOne [1] + tableTwo [1]);
}

if (userNum == 2){ 
    printf("%d", tableOne [2] + tableTwo [2]);
}

if (userNum == 3){ 
    printf("%d", tableOne [3] + tableTwo [3]);
}

if (userNum == 4){
    printf("%d", tableOne [4] + tableTwo [4]);
}

return 0;

}

相关问题