check if an array of char is filled correctly

I have to create a data member named number inside a class named account. The variable number is an array of char and the valid values are any string that starts with 3 characters and ends with 4
numbers. Ex. SAV4567, CHK5892, etc…(Defaults to Empty String)
I wrote a code (see below)but it seems that my problem is in number[i] == nbr[i] (lines 5 and 10). So what can i replace these two lines with, without changing the for loop ? note that the array number[8] is a private data member inside the class and nbr[ ] is its equivalent in the constructor.

1
2
3
4
5
6
7
8
9
10
11
12
13
 void set_number(char nbr[]) // set function to input check the characters entered
	{
		for(int i=0; i<=2; i++) { // check if the first three characters are letters
			if(nbr[i] >= 'A' && nbr[i] <= 'Z'){
				number[i] == nbr[i];
			}
		}
		for(int i=3; i<=6; i++) { // check if the last 4 characters are digits
			if(nbr[i] >= '0' && nbr[i] <= '9'){
				number[i] == nbr[i];
			}
		}
	}
Last edited on
If I understand your question properly, I think what you are trying to do is number[i] = nbr[i]. However, I'm confused by the statement
nbr[ ] is its equivalent in the constructor.


However I think you have a bigger problem. You're checking for valid characters/digits, but if you run into a bad one, you never flag the error. All you do is skip the index and move on to the next one. So, let's say that number contains the (valid) string "ABC1234" and you call set_number with the value "9876543", you will end up with number containing "ABC6543". I you then called set_number with the string "asdfghj", number would not change.

Not knowing exactly what you are trying to accomplish, I suggest you consider a validation function (essentially lines 3/4 and 8/9) which returns true/false and then copy nbr into number if it passes.
Topic archived. No new replies allowed.