Stuck with an infinite loop.
Jul 15, 2015 at 5:27am UTC
If i remove the "if" part, the loop works fine, but for some reason, the validation check converts it into an infinite loop. No idea why its screwing with me.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
std::string c_put (void ){
int check = 1;
std::string pcode;
while (check != 0){
std::cout << "Input Code:" << " " ;
std::cin >> pcode;
std::cout << std::endl;
if ( (pcode != "Mil" ) || (pcode != "You" ) || (pcode != "Cak" ) || (pcode != "DVD" ) || ( pcode != "Weed" )) {
std::cout << "Invalid Code Has Been Entered, Please Enter Correct Code:" << std::endl;
continue ;
}
check = 0 ;
return pcode ;
}
}
Jul 15, 2015 at 5:30am UTC
Check your if statement. Try testing it with some values you think should work.
Jul 15, 2015 at 5:34am UTC
I did, still not working. :(
Jul 15, 2015 at 5:38am UTC
See what happens when you put in "Mil". The first part of your or statement will return false, because it is "Mil", but then all of the other parts will return true.
Jul 15, 2015 at 5:44am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
std::string c_put (void ){
int check = 1;
std::string pcode;
while (check != 0){
std::cout << "Input Code:" << " " ;
std::cin >> pcode;
std::cout << std::endl;
if (pcode != "Mil" ) {
std::cout << "Invalid Code Has Been Entered, Please Enter Correct Code:" << std::endl;
continue ;
}
else if (pcode != "You" ){
std::cout << "Invalid Code Has Been Entered, Please Enter Correct Code:" << std::endl;
continue ;
}
else if (pcode != "Cak" ){
std::cout << "Invalid Code Has Been Entered, Please Enter Correct Code:" << std::endl;
continue ;
}
else if (pcode != "DVD" ){
std::cout << "Invalid Code Has Been Entered, Please Enter Correct Code:" << std::endl;
continue ;
}
else if (pcode != "Weed" ){
std::cout << "Invalid Code Has Been Entered, Please Enter Correct Code:" << std::endl;
continue ;
}
check = 0 ;
return pcode ;
}
}
I Did it this way now, same issue.
Last edited on Jul 15, 2015 at 5:44am UTC
Topic archived. No new replies allowed.