因此,我环顾四周,但显然不是在正确的地方看,否则我误会了。

给出的说明是,该程序只能接受库高度的2.0到5.0之间的值。我觉得这与我的if条件有关,但是我不确定如何纠正它。

当我在调试过程中输入高度时,它跳到我的else语句,并且没有给我重新输入新输入的机会。

//Retrieving name and first vault and date
cout << "Please enter the name of the pole vaulter.\n";
getline(cin, name);
cout << "Please enter the height of the first vault attempt.\n";
cin >> vault1;
if(5.0 >= vault1 >= 2.0) {
    cout << "What date was this vault accomplished?\n";
    getline(cin, date1);
} else {
    cout << "Only jumps between 2.0 meters and 5.0 meters are valid inputs.       Please enter the height of the first vault attempt.\n";
    cin >> vault1;
    cout << "What date was this vault accomplished?\n";
    getline(cin, date1);
}

最佳答案

表达方式

5.0>=vault1>=2.0


应该可以在Python中正常工作,但是在C ++中,它解析为

(5.0>=vault1) >= 2.0


将第一个比较的结果与2.0进行比较,得出无意义的结果。

在C ++中将其写为

2.0 <= vault1 && vault1 <= 5.0


如果您在多个位置执行此检查,则可以帮助提高清晰度和可维护性,定义一个适当命名的布尔函数。

10-07 14:27