【问题标题】:C++ While/ do while loop not pausing on a cinC++ While/do while 循环不在 cin 上暂停
【发布时间】:2021-03-04 14:16:09
【问题描述】:

无论我做什么,我都无法让代码在 cin 上暂停英尺或英寸。我尝试过使用 cin.clear 和 cin.ignore 但之前的 cin 是一个字符串

double inches = 0.0;
double feet = 0.0;

do 
{
    cout << "Please enter your height in feet:" << endl;
    cin >> feet;

    if  (feet > 2 && feet < 8)
    {
        cout << "Thank you!" << endl;
        break;
    }
    else if (feet < 2 || feet > 8)
    {
        cout << "You must be between 2 and 7 feet" << endl;
        
    }
} while (heightB);

while (heightB)
{
    cout << "\nPlease enter the inches:" << endl;
    cin >> inches;

    if (inches < 0 || inches > 11)
    {
        cout << "\nInches must be between 0 and 11" << endl;
    }
    else
    {
        cout << "Thank you!" << endl;
        break;
    }

}

total_inches = feet * 12 + inches;
cout << "Your total height in inches is: " << total_inches << endl;

【问题讨论】:

  • ... } while (heightB); while (heightB) { ... 您如何期望在这些条件下进入第二个循环?
  • 不管第二个循环有什么缺陷。这里有更多代码,但试图复制它们只是给格式化带来了问题,而且我没有使用足够的堆栈溢出来修复它。我遇到的主要问题是,即使我使用断点在第二个循环处停止代码,cin 仍然会被跳过。我是编程新手,我可以解决很多问题的唯一方法是一次启动它们,而我的第一个可识别问题是我的 cin.同样关于 heightB,它是一个当前设置为 true 的全局布尔值。这正是大学告诉我的方式。
  • !我是编程新手,我可以解决很多问题的唯一方法是一步一步地开始它们” - 这听起来像是有经验的程序员会做的。如果您已经以这种方式处理问题,请坚持下去。
  • @Mr_Basal 请发minimal reproducible example 重现问题,否则很难帮助您诊断问题。因此,当在第一个循环中调用 break; 时,应该进入第二个循环。但是,最好复制最少的可重现问题的可编译代码。代码格式化提示here
  • Ty 试图帮助 @πάνταῥεῖ 和 ty 建议 Ted。我想出了我哪里出错了,甚至在我发送的代码中也没有哈哈

标签: c++ debugging while-loop do-while


【解决方案1】:

我稍微修改了您的代码以使其可执行,并且它按预期工作,没有任何问题。

    #include<iostream>
    using namespace std;
    
    int main(){ 
        bool heightB = 1;
        double inches = 0.0;
        double feet = 0.0;
        double total_inches = 0.0;
        do 
        {
            cout << "Please enter your height in feet:" << endl;
            cin >> feet;
    
            if  (feet > 2 && feet < 8)
            {
                cout << "Thank you!" << endl;
                break;
            }
            else if (feet < 2 || feet > 8)
            {
                cout << "You must be between 2 and 7 feet" << endl;
                
            }
        } while (heightB);
    
        while (heightB)
        {
            cout << "\nPlease enter the inches:" << endl;
            cin >> inches;
    
            if (inches < 0 || inches > 11)
            {
                cout << "\nInches must be between 0 and 11" << endl;
            }
            else
            {
                cout << "Thank you!" << endl;
                break;
            }
    
        }
        
        total_inches = feet * 12 + inches;
        cout << "Your total height in inches is: " << total_inches << endl;
        
        return 0;
    }

【讨论】:

    【解决方案2】:

    解决了我自己的问题,结果问题从来没有出现在这个问题上。只是由于缺乏弦乐经验,我犯了一个菜鸟错误。在此之前,我使用 cin >> name 作为字符串。相反,我需要使用 getline(cin, name) 来获取所有文本。

    因为我没有这样做,所以我的循环只是无休止地循环。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-23
      • 1970-01-01
      • 2018-05-09
      • 2014-07-04
      • 2013-06-05
      • 2017-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多