Unsure how to use cctype

I am trying to get my program to check if a code is valid or not. If the code is invalid, it will be output into a file. The code is valid if in the format: letter/letter/digit/digit/letter/letter(w/o slashes). I've tried to incorporate cctype in some way but I cannot figure out how to get it to work.

[code]

#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;

struct ActiveCodes1
{
string code;
bool flag;
};

struct CheckCodes
{
int idNum;
string code;
};

void swap(ActiveCodes1 A[], int i, int j)
{
ActiveCodes1 temp;
temp=A[i];
A[i]=A[j];
A[j]=temp;
return;
}

void sort(ActiveCodes1 A[], int size)
{
for(int p=1; p<size; p++)
{
for(int c=0; c<size-p; c++)
{
if(A[c].code>A[c+1].code) swap(A, c, c+1);
}
}
return;
}

int main()
{
ifstream fin;
ofstream fout;

fin.open("ActiveCodes1.txt");

ActiveCodes1 A[100];

int a=0;

while(fin>>A[a].code)
{
a++;
}

sort(A, a);

fin.close();

fin.open("CheckCodes.txt");

CheckCodes B[100];

int b=0;
int isalpha();
int isdigit();

while(fin>>B[b].idNum>>B[b].code)
{
if(B[b].code.length()!=6 || (isalpha(B[b].code.at(1)) && B[b].code.at(2)!=isalpha && B[b].code.at(3)!=isdigit

&& B[b].code.at(4)!=isdigit && B[b].code.at(5)!=isalpha && B[b].code.at(6)!=isalpha))

{
fout.open("InvalidCodes.txt");
fout<<B[b].idNum<<"\t"<<B[b].code<<endl;
fout.close();
}
}




return 0;
}

[code]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cctype>

int main()
{
    std::cout << "enter a char " ;
    char c ;
    std::cin >> c ;

    std::cout << "char '" << c << "' is " ;

    if( std::isdigit(c) ) std::cout << "a decimal digit\n" ;

    else if( std::isalpha(c) ) std::cout << "alphabetic\n" ;

    else if( std::ispunct(c) ) std::cout << "punctuation\n" ;

    else std::cout << "something else\n" ;
}
I appreciate your response, but I am just learning c++ and really don't understand your example. What is "std::"? And, I'm not sure how I can apply this to my program.
"std" is a namespace, which is why you are using the line "using namespace std." The scope operator - "::" - allows you to access a member of the std namespace. You don't have to use the scope if you prefer using "using namespace std;" So you would have this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cctype>

using namespace std;

int main()
{
    cout << "enter a char " ;
    char c ;
    cin >> c ;

    cout << "char '" << c << "' is " ;

    if( isdigit(c) ) cout << "a decimal digit\n" ;

    else if( isalpha(c) ) cout << "alphabetic\n" ;

    else if( ispunct(c) ) cout << "punctuation\n" ;

    else cout << "something else\n" ;
}


As you learn more, you'll find out why "using namespace std;" isn't preferrable by a lot of programmers.
I see what you're doing and I tried changing my code but it doesn't work. I'm not outputing anything to the console so I don't know how to use this with my program. I just need the program to check a list of strings from a file to see if they are of the format "letter letter digit digit letter letter". And, if not, they will be sent to an "invalid" file. This is what i have changed so far:
[code]

char c;

while(fin>>B[b].idNum>>B[b].code)
{
if(B[b].code.length()!=6 || (B[b].code.at(1)!=isalpha(c) && B[b].code.at(2)!=isalpha(c) && B[b].code.at(3)!=isdigit(c)

&& B[b].code.at(4)!=isdigit(c) && B[b].code.at(5)!=isalpha(c) && B[b].code.at(6)!=isalpha(c)))

{
fout.open("InvalidCodes.txt");
fout<<B[b].idNum<<"\t"<<B[b].code<<endl;
fout.close();
}
}

[code]
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
#include <string>
#include <cctype>

int main()
{
    std::string code ;
    std::cin >> code ;

    using namespace std ;
    const bool is_letter_letter_digit_digit_letter_letter =
                                                code.size() == 6
                                                && isalpha( code[0] )
                                                && isalpha( code[1] )
                                                && isdigit( code[2] )
                                                && isdigit( code[3] )
                                                && isalpha( code[4] )
                                                && isalpha( code[5] ) ;

    if( is_letter_letter_digit_digit_letter_letter )
    {
        // code is valid
        // ...
    }
    else
    {
        // code is invalid
        // ...
    }
}


Note: end your code snippet with [/code] instead of [code]
I changed the code but it still doesn't seem to work.

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
while(fin>>B[b].idNum>>B[b].code)
	{
		const bool valid=B[b].code.size()==6 && isalpha(B[b].code[1]) && isalpha(B[b].code[2]) && isdigit(B[b].code[3])
		&& isdigit(B[b].code[4]) && isalpha(B[b].code[5]) && isalpha(B[b].code[6]);
		
		//if(B[b].code.length()==6 && isalpha(B[b].code.at(1)) && isalpha(B[b].code.at(2)) && isdigit(B[b].code.at(3)) 
			//&& isdigit(B[b].code.at(4)) && isalpha(B[b].code.at(5)) && isalpha(B[b].code.at(6)))

		if(valid)
		{
			//code is valid
			b++;
		}
		else
		{
			fout.open("InvalidCodes.txt");
			fout<<B[b].idNum<<"\t"<<B[b].code<<endl;
			fout.close();
		}
		b++;
	}

	
	
	
	return 0;
}
> B[b].code.size()==6 && isalpha(B[b].code[1])

The first character is at position 0, and the last character is at position 5.
Oh, I just changed it back to that after I tried starting the first character at position 0 and it didn't work.
Is there any way I could use this, but have it evaluate the codes for the opposite answer(such as putting a "!" somewhere in the statement)?
Topic archived. No new replies allowed.