有了这段代码,我试图用用户输入的整数值构建一个数组。变量“int selection”是一个int,所以如果输入的值是一个int,while循环应该继续,但值0似乎结束它,我不明白为什么。谢谢你的帮助。为什么选择0会在循环结束时结束?
int main()
{
//data to be entered
int selection;
//array size
int const array_size = 100;
//array
int integers[array_size];
//array index
int index = 0;
//prompt
std::cout << "Enter integer ('x' to quit): " << std::endl;
//get the data
std::cin >> selection;
//while data is int
while (selection)
{
//put it in the array
integers[index] = selection;
//increment index
index += 1;
//get new data point
std::cin >> selection;
}
return 0;
}
'0'等价于'false'。 – 2014-09-06 17:03:39
,因为值“0”可以转换为_bool_“false”。因为值'!= 0'可以转换成_bool_'true'。 – NetVipeC 2014-09-06 17:03:47
将'selection'设置为0会结束你的循环,因为你已经这么说了:'while(selection)'。 – usr2564301 2014-09-06 17:04:01