Problem with isalnum,isdigit,isalpha etc conditionals

First of all, i apologize if this sort of question has already been asked and if i'm being a nuisance but i just don't know what to do.

Alright, so i've been trying to make a car number registration program. I will confess, this is my homework, and i don't expect solutions right away, just some tips and a general direction to what i should do.
It's basically like this:

if the number length is 5/6 where the first 2/3 letters are digits and the rest are letters, it's a valid code (for example "123ABC" or "12ABC") (Estonian car number).

And then there are specially manufactured car numbers with a length limit of 9. And it has to have at least 1 number. (for example "ABCDE12")

Everything else is undefined and are non-compliant with current car number rules etc.


I tried using isalpha, isnum conditionals but i just can't seem to tell the program that i want just the first particular digits etc.

I also tried doing it with for loops, but that didn't work so well.
Then i tried doing it with a while function, that didn't work so well either.

Here's an example of what i tried to do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <stdio.h>
#include <string>
#include <ctype.h>

using namespace std;

int main()
{
    int i=0;
    const int j=3;


    cout << "Please enter a car number";
    string carnum;
    getline(cin, carnum);

    if ( (i<carnum.length()) && (carnum.length()<=6) && (isdigit(carnum[i<=3])&&(isalpha(carnum[i>3]))))
    printf("This car number is valid (Traditional car number)");
    
    return 0;
}





All and any help is appreciated
What exactly you are trying in the above code ? let me try the specially manufactured car function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool isSMCN(string num)
{
	if(num.length() != 9)
	{
		return false;
	}

	bool flag = false;
	for(int i = 0; i < num.length();i++)
	{
		if(isdigit(num[i])
		{
			return true;		//if any number is digit, its true
		}
	}
	
	return false;			//we are here which means there were no digits.
}


hope you can try to write other functions like this.
Last edited on
Thank you for responding so quickly, i am currently trying to use this boolean function you posted.

Though i would really like to know, how would i go about telling my if statement to check only 3 first digits and then 3 last digits.
For example:

1
2
3
4
5
6
7
8
9
10
11
12

string carnumber=123ABC;
for(int i=0;i<carnumber.length;i++){
if( (carnumber.length<=6) && (isdigit(carnumber[i<=3]) && (isalpha(carnumber[i>3])

 // here i'm trying to tell the if function 
 // check the first letters of carnumber and then check the last 3 letters of 
 //Though i don't know if this is the right way to go about it

printf("Valid car number");}

i <= 3 will give either 1 or 0. So, you are actually doing this:

isdigit(carnumber[0]) or isdigit(carnumber[1])

do the same like I did. do two loops, one will run for first three index and other will run for last three index's. See if both the loops satisfy your conditions.
Topic archived. No new replies allowed.