Would someone please look at the below code and see why it works most of the time but not always?
It works when I input something like "7 1000 1002 896 897 1004 987 960"
, it shows Unallowed value(s)
like it's supposed to.
But if I input "7 896 1003 1004 899 897 898 906"
, it should say Unallowed value(s)
, but it works periodically.
int main() {
int inputCount;
bool allAllowed;
int range;
cin >> inputCount;
cin >> range;
if ((range >= 900) && (range <= 1000)) {
allAllowed = 0;
for (int i = 1; i < inputCount; i) {
cin >> range;
if ((range >= 900) && (range <= 1000)) {
allAllowed = 0;
}
else if ((range < 900) || (range > 1000)) {
// i = inputCount 1;
allAllowed = 1;
}
}
}
else if ((range < 900) || (range > 1000)) {
allAllowed = 1;
}
if (allAllowed) {
cout << "Only allowed values" << endl;
}
else {
cout << "Unallowed value(s)" << endl;
}
return 0;
}
CodePudding user response:
Your initial value of range, 896, isn't between 900 and 1000 and satisfies the condition
else if ((range < 900) || (range > 1000))
, making allAllowed = 1
,
meaning it will always return "Only allowed values".
Something else you should consider is that your program will only consider the last inputted number to determine allAllowed
within the condition if ((range >= 900) && (range <= 1000))