C++ Assistance

Hello all, I am working on a project and am having a bit of an issue with one of the requirements. Basically, I have to read a 6 character string input from my user and error check that the character is in the following format:

Alpha Alpha Alpha Digit Digit Digit (e.g. ABC987)

Here what I have:

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
#include<iostream>
#include<cctype>

using namespace std;

bool doAgain();

int main()
{
	while(doAgain());
	cout << "Valid input.\n";
	return 0;
}

bool doAgain()
{
	char a[6];
	cout << "Enter a 6 digit number: " ;
	for(int i=0;i<6;i++)
	{
		cin >> a[i];
		cout << a[i];
		if((i < 3 && !isalpha(a[i]) || (i > 3 && i < 6) && !isdigit(a[i]))) 
		{
			cout << "Inputs contains non test1.\n";
			
			cin.ignore(); 
			return 1;
		}

	}
	char x = cin.get();
	if(x!='\n') 
	{
		cout << "Input contains more than 6 characters.\n";
		cin.ignore(); 
		return 1;
	}

system("pause");
	return 0;
}


Some of the requirements are working. If a user were to enter ABC123 the program goes as it should. If the user enters ABC1234 the program gives them the message that there input contains more than 6 characters as it should. If the user enters ABCDEF the program prompts them to enter a correctly formatted string. If they enter abca12 the program processes like normal even though it should ask them to re-enter the string. If they enter 123abc the program asks them to re-enter twice (going through the loop). Hopefully this makes sense. Not looking to cheat, just hoping someone can give me some pointers or point me to a better way of accomplishing this task.
Last edited on
In this condition

if((i < 3 && !isalpha(a[i]) || (i > 3 && i < 6) && !isdigit(a[i])))

you missed to check i == 3.
Thanks vlad, I will give that a try when i get home. Cant believe I overlooked that.
If function is bool, if something goes wrong, such as, more than 6 characters, you have to do, type in "return false;" If everything goes as planned, "return true;"

Modify your code
Topic archived. No new replies allowed.