循环不提示用户输入(C )
while loop not prompting for user input (c++)
我的循环效果很好,直到输入" true"时,输入" true"时,循环忽略了cin的剩余循环,然后将文本打印在cout中,直到循环结束了,如何使循环要求在每个循环上要求输入,或者我在哪里犯了一个错误?PS:这是一项功课,所以我无法更改给出的结构。感谢您的建议。代码片段:
int main()
{
struct Patient
{
double height;
double weight;
int age;
bool isMale;
};
Patient ListOfPatients[4];
int iii = 0;
while (iii < 4)
{
cout << "enter the height (eg. 1.80 metres) of patient number " << iii + 1 << " :" << endl;
cin >> ListOfPatients[iii].height;
cout << "enter the weight (eg. 80kg) of patient number " << iii + 1 << " :" << endl;
cin >> ListOfPatients[iii].weight;
cout << "enter the age of patient number " << iii + 1 << " :" << endl;
cin >> ListOfPatients[iii].age;
cout << "is the patient a male? (true = male or false = female) " << endl;
cin >> ListOfPatients[iii].isMale;
iii++;
}
return 0;
}
问题是当您阅读isMale
时。bool
确实是一个数字,false
的0
,1
的CC_5。您无法读取字符串。
发生的事情是,您最终会在流中有很多字符,而>>
操作中的任何一个都无法读取,因此它们都只是连续失败。尝试在命令行上传递1
或0
,一切都很好。
如果您希望用户能够通过字符串,然后将其视为bool
,则可以完全使用为此提供的实用程序,或者手动使用它只是为了帮助您理解。
要使用标准实用程序,称为std::boolalpha
,并在<iomanip>
标头中:
std::cin >> std::boolalpha >> ListOfPatients[i].isMale;
要手动执行此操作,将其阅读到std::string
对象中,然后自己进行一些比较,例如:
std::string tmpIsMale;
std::cin >> tmpIsMale;
ListOfPatients[i].isMale = (tmpIsMale == "true");
出于此原因,最好在尝试从中读取流程后检查流的状态。您可以将流直接放入if
中:
std::cin >> someVariable;
if (!cin) { // cin is not in a good state
std::cerr << "Failed to read someVariable, please try again with a proper value!n";
return 1;
}
您可能要避免的一些助理和不良实践:
-
using namespace std;
被广泛认为是不良练习。 -
std::endl
更具争议性,但是您使用的方式表明您不了解它。 - 性别不是布尔的选择。我知道这不是严格的技术问题,如果您的老师提供的结构有点卡住,但是来吧,这是2019年!学会编写好的程序不仅意味着语言的技术性,而且还意味着如何编写对用户有益的程序,因此很早就养成良好的习惯是一个好主意。
您可以使用流操作器boolalpha
不进行任何比较:
cin >> std::boolalpha >> cListOfPatients[iii].isMale >> std::noboolalpha;
您无法将字符串true
分配到布尔字段中。请检查更正的示例。
int main()
{
struct Patient
{
double height;
double weight;
int age;
bool isMale;
};
Patient ListOfPatients[4];
int iii = 0;
while (iii < 4)
{
string sIsMale = "";
cout << "enter the height (eg. 1.80 metres) of patient number " << iii + 1 << " :" << endl;
cin >> ListOfPatients[iii].height;
cout << "enter the weight (eg. 80kg) of patient number " << iii + 1 << " :" << endl;
cin >> ListOfPatients[iii].weight;
cout << "enter the age of patient number " << iii + 1 << " :" << endl;
cin >> ListOfPatients[iii].age;
cout << "is the patient a male? (true = male or false = female) " << endl;
cin >> sIsMale;
ListOfPatients[iii].isMale = sIsMale == "true";
iii++;
}
return 0;
}
这里的问题是isMale
是boolean
,字符串"true"
与boolean
不同!之所以不会崩溃,而只是简单地贯穿程序的其余部分是因为您的cin
仍在读取输入流的旧字符。一个简单的解决方案是将True或False转换为Booleans,然后使用这些将其设置为isMale
。这可以通过:
String toBeConverted;
cin >> toBeConverted;
ListOfPatients[iii].isMale = toBeConverted == "true";
相关文章:
- C++如何通过用户输入删除列表元素
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 用户定义函数中的指针和输入
- 如何只允许用户输入正整数
- C++如何计算用户输入的数字中的偶数位数
- 通过for循环使用用户输入填充列表
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- 在C++中,我如何接受不同于同一行的用户输入
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- 当用户键入分隔符时,停止getline()输入
- 通过错误处理,在C++中可靠地获得用户十六进制输入
- 当用户在qtablewidget中输入单元格时,如何获得信号?C++
- 显示基于用户输入的整数的字符
- 根据用户输入用字母填充矢量,并将"开始"和"结束"放在四肢
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 如果用户没有输入任何内容或输入错误,如何重新输入用户的输入?
- 为什么在代码块中执行时会跳过输入用户输入的行?
- 使用 for 循环在数组中输入用户