How do I check the second character of this string?

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?

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
using namespace std;

void check(string idcheck){
    unsigned int i;
    for (i = 0; i <= idcheck.size() - 4; i++) {
        if (isalpha(idcheck.at(i))) {
            for (i = 3; i <= idcheck.size() - 1; i++) {
                if (isdigit(idcheck.at(i))) {
                    cout << "The id " << idcheck << " is valid" << endl;
                    break;
                }
                else {
                    cout << "error: digit expected at position " << i + 1 << endl;
                    break;
                }
            }
        }
        else {
            cout << "error: letter expected at position " << i + 1 << endl;
            break;
        }
    }
        

}
int main() {
   string ID;
   cout << "Please enter an id: ";
   cin >> ID;

    if (ID.length() == 6) {
            check(vuID);
        }
    else if (ID.length() < 6) {
            cout << "error: size incorrect (is " << ID.size() << ", should be 6)" << endl;
        }
    else if (ID.length() > 6) {
            cout << "error: size incorrect (is " << ID.size() << ", should be 6)" << endl;
    }

    
    //test program



   return 0;
}
Last edited on
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:

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>

using namespace std;

void check(string idcheck) {
	unsigned int 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 << "The id " << idcheck << " is valid" << endl;
			break;
		}


		else
		{
			cout << "error: digit expected at position " << i + 1 << endl;
			break;
		}
	}


}
int main() {
	string ID;
	cout << "Please enter an id: ";
	cin >> ID;

	if (ID.length() == 6) {
		check(ID); //You had "vuID" instead of ID, changed it to compile
	}
	else if (ID.length() < 6) {
		cout << "error: size incorrect (is " << ID.size() << ", should be 6)" << endl;
	}
	else if (ID.length() > 6) {
		cout << "error: size incorrect (is " << ID.size() << ", should be 6)" << endl;
	}

	//test program

	return 0;
}
Last edited on
You need consecutive loops, not nested loops.
1
2
3
4
5
6
for ( i = 0 ; i < 3 ; i++ ) {
    if (isalpha(idcheck.at(i))) ...
}
for ( i = 3 ; i < 6 ; i++ ) {
    if (isdigit(idcheck.at(i))) ...
}
Wow you're right! Thanks so much!

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?
You have a break statement! Your if statement is making the loop only go once.

This:

1
2
3
4
5
if (isdigit(idcheck.at(i)))
{
	cout << "The id " << idcheck << " is valid" << endl;
	break;
}


Should be This:

1
2
3
4
if (isdigit(idcheck.at(i)))
{
	cout << "The id " << idcheck << " is valid" << endl;
}


In fact, the whole function could be better written like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void check(string idcheck) 
{
	unsigned int 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;
}
Wow, the usage of break instead of return was the biggest issue. Thanks a lot!
<3
Topic archived. No new replies allowed.