Hello, I'm working on a program that needs to check a 6 character string, and if the characters I've inputted don't match specific conditions then the program ends with an error message. Most of my program works, but I've got 1 issue with it. As you can see, I've done a for-loop for the first 3 characters in the string, and then another one inside of it for the second 3.
The string shouldn't have any characters other than letters and numbers in it, and the moment I enter a '@' or a '!' in the string (eg: i@d123) the program doesn't see it and returns the "id is valid" message. It detects it if it's the first character though (eg: @id123). How do I make it see the '@' in the second position there?
The issue is that you've over-complicated the logic to the point where there's too much happening for you to notice the error. There's no reason for the two loops to be like that.
The issue is that you're going to run through the second loop EVERY time there's a letter in the string - which there's no reason for. Here's the code, better optimized and working with the sample input that was giving you trouble:
Just one more question, because I thought fixing the problem in the first half would fix the other one I had.
What if I have a letter in the second half of the string? (eg: idd1oo) I've tried a few things after you posted this here and it can't detect a letter in the second position of the second half of this string. What is the problem?
void check(string idcheck)
{
unsignedint i;
for (i = 0; i <= idcheck.size() - 4; i++)
{
if (!isalpha(idcheck.at(i)))
{
cout << "error: letter expected at position " << i + 1 << endl;
return;
}
}
for (i = 3; i <= idcheck.size() - 1; i++)
{
if (!isdigit(idcheck.at(i)))
{
cout << "error: digit expected at position " << i + 1 << endl;
return;
}
}
cout << "The id " << idcheck << " is valid" << endl;
}