I'm trying to catch a user error in the try block my first try works correctly but my second one keeps me in the loop and won't break me out.
the second if statement in this procedure always throws when i walk to it in the debugger. I can't seem to figure out what the problem is. It's like its comparing the wrong values or something. It's suppose to only go into the if statement if my string doesn't contain a value between 1-5. The string is stored in a struct.
void userGuess()
{
bool loop = true;
//start of loop for user-end error handling
do{
cout << "Please write what 5 number code" << endl;
cout << "Only use values 1-5" << endl;
cin >> player.a;
//start of nested loop
// This nested do try-catches user handler issues
do{
try{
if (player.a.length() != 5)
{//if user put more than 5 chars
throw player.a;
}
for (int i = 3; i < player.a.length() + 1; i++)
{
if (player.a[i] != '1' || '2' || '3' || '4' || '5')
{// throws if user doesnt input a correct value
throw player.a;
}
}
}
catch (...)
{
system("CLS");
cout << "please make sure you type in 5 numbers between 1-5 with no commas or spaces." << endl;
cin.clear();
break;
}
loop = false;
} while (loop = true);
//end of nested loop
}while (loop = true);
// end of main loop
player.a;
return;
}
if (player.a.length() != 5)
{//if user put more than 5 chars
throw player.a;
}
to:
1 2 3 4
if (player.a.length() > 5)
{//if user put more than 5 chars
throw player.a;
}
2. Change:
1 2 3 4
if (player.a[i] != '1' || '2' || '3' || '4' || '5')
{// throws if user doesnt input a correct value
throw player.a;
}
to:
1 2 3 4
if (player.a[i] != '1' && player.a[i] != '2' && player.a[i] != '3' && player.a[i] != '4' && player.a[i] != '5')
{// throws if user doesnt input a correct value
throw player.a;
}
To 1.: In your original code, you tested to see if the length was anything but 5, in other words, the length had to be 5.
In the changed code, I made sure to test if the the length was above 5
To 2.
Here, you tested if the current char wasn't equal to 1, correct.
After that, you went on to test if ('2') which is basically the same as testing if(true)
In the changed code, I tested for (player.a[i] != '[insert char]') x times
hmmm so you can't use the OR operator for that if has to be an AND? I must be on lack of sleep. How come OR does not work in this situation? wouldn't AND mean it would have to equal 12345? I'll try this out Thanks.
edit: Just saw "while(loop = true)" should be "while(loop == true)"
void userGuess()
{
bool loop = true;
do{
cout << "Please write what 5 number code" << endl;
cout << "Only use values 1-5" << endl;
cin >> player.a;
do{
try{
if (a.length() > 5)
{
throw player.a;
}
for (int i = 0; i < player.a.length(); i++) // This line was the issue mostly, because a[a.length()] doesn't exist, only a[a.length()-1], so i < a.length() +1 won't really work the way you expect it to.
{
if (player.a[i] != '1' && player.a[i] != '2' && player.a[i] != '3' && player.a[i] != '4' && player.a[i] != '5')
{
throw player.a;
}
}
}
catch (...)
{
system("CLS"); // you should remove this line to prevent ranting from other forum members
cout << "please make sure you type in 5 numbers between 1-5 with no commas or spaces." << endl;
cin.clear();
break;
}
loop = false;
} while (loop == true); // changed to ==
} while (loop == true); // changed to ==
// removed unnecessary player.a; and return; lines
}
OR doesn't work because only ONE test has to return true. So, if for example, I want the user to input 1 or 2 and nothing else, I couldn't test for (input != 1 || input !=2), because if the user typed 2, input != 1 would return true and the if statement would be executed. That is why you should use AND in this case, because (input != 1 && input != 2) where input is 2 doesn't execute the if statement because both tests have to be true.
ohhhh, thanks guys. This is actually the first time I used exceptions was just trying to figure them out before my class actually got to it. So I shouldn't use a try/catch if its to control the user input?
See if this makes sense to you: When and How to Use Exceptions
By Herb Sutter, August 01, 2004
http://www.drdobbs.com/when-and-how-to-use-exceptions/184401836