Stuck with an infinite loop.

closed account (Dj654iN6)
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 ; 
	}
}
Check your if statement. Try testing it with some values you think should work.
closed account (Dj654iN6)
I did, still not working. :(
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.
closed account (Dj654iN6)
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
Topic archived. No new replies allowed.