【发布时间】: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