【问题标题】:Infinite loop on checking character in a do while在do while中检查字符的无限循环
【发布时间】:2020-10-13 10:30:44
【问题描述】:

目标是计算分数超过 10 的百分比。分数介于 0 和 20 之间。当我单击“N”时,while 循环出现问题。我得到一个无限循环。

#include <iostream>

int main() {
    float MARK, PERCENTAGE;
    int NBR_MARK, NBR_MARK_10;
    MARK = 0;
    NBR_MARK = 0;
    NBR_MARK_10 = 0;
    PERCENTAGE = 0;
    char R = 'N';

    std::cout << "Enter a mark ?" << std::endl;
    std::cin >> MARK;

    while (MARK < 0 || MARK > 20) {
        std::cout << " Please, enter a mark between 0 and 20" << std::endl;
        std::cin >> MARK;
    }

    std::cout << "Do you want to enter a new mark ?" << std::endl;
    std::cout << "Click on 'O' to continue and on 'N' to stop" << std::endl;
    std::cin >> R;

    if ((R != 'N') || (R != 'n'))
        std::cout << "You will continue" << std::endl;

    do {
        std::cout << "Enter a new mark" << std::endl;
        std::cin >> MARK;
        std::cout << std::endl;

        while ((MARK < 0) || (MARK > 20)) {
            std::cout << "Please, enter a mark between 0 and 20" << std::endl;
            std::cin >> MARK;
            std::cout << std::endl;
        }

        NBR_MARK++;

        if (MARK > 10) NBR_MARK_10++;

        std::cout << "To stop press 'N'" << std::endl;
        std::cin >> R;

    }
    while ((R != 'N') || (R <= 'n'));

    PERCENTAGE = NBR_MARK_10 / NBR_MARK * 100;
    std::cout << "Le % de notes > 10 est de: " << PERCENTAGE << " %" << std::endl;
    return 0;
}

【问题讨论】:

  • 错字? (R &lt;= 'n')
  • 您的代码中向用户提供的说明让我有些困惑。您说“单击...”或“按 N”,但您使用 cin 并且用户需要按下键然后按 Enter。输入到底是什么?
  • while ((R != 'N') || (R &lt;= 'n')); 这可能是导致您的问题的原因
  • @Caleth 非常感谢您的反馈。我已经更正了错字,但是当我想停止添加标记时,我仍然有一个无限循环。
  • @ColdCerberus 非常感谢您的反馈。我已经纠正了。正如我刚刚向 Caleth 提到的那样,当我选择布尔值“N”时,我找不到解决无限循环的方法。

标签: c++ while-loop boolean infinite-loop


【解决方案1】:
#include<iostream>
using namespace std;
int main() {
    float MARK=0.0, PERCENTAGE=0.0;
    int NBR_MARK_10;
    float NBR_MARK = 0.0;
    NBR_MARK_10 = 0;
    char R = 'a';
    while(R!='N')
    {
        std::cout << "Enter a mark ?" << std::endl;
        std::cin >> MARK;

        while (MARK < 0 || MARK > 20) {
            std::cout << " Please, enter a mark between 0 and 20" << std::endl;
            std::cin >> MARK;
        }
        NBR_MARK+=1.0;
    
        if (MARK > 10) NBR_MARK_10++;
        
        std::cout << "Do you want to enter a new mark ?" << std::endl;
        std::cout << "Click on 'O' to continue and on 'N' to stop" << std::endl;
        cin >> R;
    
    }
    PERCENTAGE = (NBR_MARK_10 / NBR_MARK) * 100;
    std::cout << "Le % de notes > 10 est de: " << PERCENTAGE << " %" << std::endl;
    return 0;
}

在检查 R 的下一个输入时使用 while 循环,并确保将其中一个计数器变量设为浮动,否则对于少数测试用例,您最终将百分比为零。

【讨论】:

  • 非常感谢您的帮助。
【解决方案2】:

您的代码有两个问题。正如 Sharath Chander P 指出的那样,代码流中显然存在逻辑问题。即使你第一次按下'N',do..while 中的代码块也会执行,因为只有在执行完 do..while 循环中的所有语句后才会检查条件。

现在回答您关于无限循环的问题。这是因为当您按下“N”键时,表达式 while ((R != 'N') || (R &lt;= 'n')); 将为“真”。当您按下“N”时,R 的值为 78(“N”的 ASCII 值)。由于 'n' 的 ASCII 值为 110,因此检查 R&lt;='n' 将返回 true。 (显然 78 小于 110。)

由于(R!='N') 为假且R&lt;='n' 为真,表达式while ((R != 'N') || (R &lt;= 'n')); 将被计算为while(false || true)

由于结果为“真”,循环将继续。

现在,如果您按“n”,则表达式将被评估为while(true || true),这也将导致“真”并且循环将继续;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-31
    • 2011-12-16
    • 1970-01-01
    相关资源
    最近更新 更多