Code Validation Problems

Hey everyone,

I'm working on a homework problem for my computer science class, and I'm running into a few issues. These issues exist primarily within my function responsible for validating codes that are read in from a text file. Additionally, I'm having issues calling my validation function within my mainline logic.

I would greatly appreciate any help/clarification/guidance you guys could provide. Thanks in advance! I apologize in advance for the big block of code!

P.S. I'm including the codes that I am using in my input file for your convenience.

The Codes to be Validated

33new88
33new88
61AmA16
10gnu00
94ok78
34codes61
87gibberish74
111ddd333
321rTe123
543zzz345
999random999
8876the987
77twa007
Def456
Yjh890
Abc123
ATT000
pQR22


My Program
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
Objectives:
1. To determine if codes from an input file are valid or invalid.

Steps:
1. The codes are first read in and sorted from an array of strings using a swap function
2. The codes are then checked for validity given the following conditions:
	a. digit/digit/letter/letter/letter/digit/digit (length=7) 
	b. digit/digit/digit/letter/letter/letter/digit/digit/digit (length=6)
	c. uppercaseletter/lowercaseletter/lowercaseletter/digit/digit/digit (length=9)
3. If the codes are valid, they are output to a file called "valid.txt"
4. If the codes are invalid, they are output to a file called "errors.txt"
5. If duplicates exist, they are simply skipped over and not output to any file.
*/

#include <iostream>
#include <string>
#include <fstream>
#include <cctype>

using namespace std;

void swap(string A[], int i, int j);
void sort_the_codes(string A[], int size);
int validate (string A[], int i, ofstream outdata, ofstream outdata2);

int main()
{
string code[1000];

ifstream indata;
indata.open("codes.txt");

ofstream outdata;
outdata.open("valid.txt");

ofstream outdata2;
outdata.open("errors.txt");

int i=0;
indata >> code[i];
while(!indata.eof())
{
  i++;
  indata >> code[i];
}

sort_the_codes(code,i);
validate (code, i, outdata, outdata2);
for(int j=0; j <= i; j++)  // determine condition
{	
  outdata << code[i] << endl;	// print to file
}


indata.close();
outdata.close();
outdata2.close();
return 0;
}


void swap(string A[], int i, int j)
{
  string temp;
  temp=A[i];
  A[i]=A[j];              
  A[j]=temp;        
  return;
				
// code that defines the terms of the swap
}



void sort_the_codes(string A[], int size)
{
	for (int x=0; x<size; x++) //controlling passes
				  //prime loop at 0
	{
	for (int j=0; j<size-x; j++)  //controlling comparisons within each pass
		{
			if (A[j]>A[j+1]) swap (A, j, j+1);
		}
	}
	
// sorts the codes within the array
return;
}

int validate (string code[], int size, ofstream &outdata, ofstream &outdata2)
{
  for (int x=0; x<size; x++)
    {
      if (code[x]!=(code [x+1]) //Determines if the codes are duplicates
	{
	if (code.length()==6||code.length()==7||code.length()==9) 
   //If the code.length matches these size parameters, proceed to the next steps

        {
	  if (code.length()==9) 
//When the code length equals nine, the function checks the conditions necessary for a nine character code to be valid
	     {
	      if ((isdigit(code[x].at(0)) > 0) && (isdigit(code[x].at(1)) > 0) && (isdigit(code[x].at(2)) > 0) && (isalpha(code[x].at(3)) > 0) && (isalpha(code[x].at(4)) > 0) && (isalpha(code[x].at(5)) > 0) && (isdigit(code[x].at(6)) > 0) && (isdigit(code[x].at(7)) > 0) && (isdigit(code[x].at(8)) > 0))
				
		{
		  outdata << code[x]; //If the code meets the stated conditions, it is output to "valid.txt" 
		}
              else outdata2<<code [x]; //If the code is length nine but it does not meet the stated conditions, the code will then be sent to the "errors.txt" file
	     }

         else if (code.length()==7) //When the code length equals seven, the function checks the conditions necessary for a seven character code to be valid
	    {
	      if ((isdigit(code[x].at(0)) > 0) && (isdigit(code[x].at(1)) > 0) && (isalpha(code[x].at(2)) > 0) && (isalpha(code[x].at(3)) > 0) && (isalpha(code[x].at(4)) > 0) && (isdigit(code[x].at(5)) > 0) && (isdigit(code[x].at(6)) > 0)))
								
		{
	          outdata << code[x]; //If the code meets the stated conditions, it is output to "valid.txt" 
		}
	else outdata2<<code[x]; //If the code is length seven but it does not meet the stated conditions, the code will then be sent to the "errors.txt" file
	    }

else if (code.length()==6) //When the code length equals six, the function checks the conditions necessary for a six character code to be valid
	  {
	   if (((isupper(code[x].at(0)) > 0) && (islower(code[x].at(1)) > 0) && (islower(code[x].at(2)) > 0) && (isdigit(code[x].at(3)) > 0) && (isdigit(code[x].at(4)) > 0) && (isdigit(code[x].at(5)) > 0) )))
								
	     {
		outdata << code[x]; //If the code meets the stated conditions, it is output to "valid.txt" 
	     }
	else outdata2 <<code[x] << endl; //If the code is length six but it does not meet the stated conditions, the code will then be sent to the "errors.txt" file
	     }


//else code[x]<<outdata2; //If the codes are errors, it goes to "errors.txt"
	   }
        } 
	else code[x]<<outdata2; //If the codes are errors, it goes to "errors.txt"
   }
}











Last edited on
You may want to think about using functions to simplify your code. Possibly a function that validates each of your three valid lengths.
Okay, I've taken your advice, and I've now created 4 separate functions that handle my validations. However, within my IDE, I keep getting errors in terms of my brackets saying that it is "expecting a declaration." I would appreciate it if I could get some clarification. Thanks!

P.S. Please note that my mainline logic has not changed at this point. If you'd like to look at that, it's in the original post.

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
void sort_the_codes(string A[], int size)
{
  for (int x=1; x<size; x++) //controlling passes
					//prime loop at 0
	{
		for (int j=0; j<size-x; j++)  //controlling comparisons within each pass
			{
				if (A[j]>A[j+1]) swap (A, j, j+1);
			}
	}
	
// sorts the codes within the array
return;
}

bool validate_length_nine (string code[], int size, ofstream& outdata, ofstream& outdata2);

{
for (int x=0; x<size; x++)
				
{
if (code[x].length()==9) //When the code length equals nine, the function checks the conditions necessary for a nine character code to be valid
{	
if ((isdigit(code[x].at(0)) > 0) && (isdigit(code[x].at(1)) > 0) && (isdigit(code[x].at(2)) > 0) && (isalpha(code[x].at(3)) > 0) && (isalpha(code[x].at(4)) > 0) && (isalpha(code[x].at(5)) > 0) && (isdigit(code[x].at(6)) > 0) && (isdigit(code[x].at(7)) > 0) && (isdigit(code[x].at(8)) > 0));
{
outdata << code[x]<<endl;//If the code meets the stated conditions, it is output to "valid.txt" 
}
else outdata2<<code[x]<<endl; //If the code is length nine but it does not meet the stated conditions, the code will then be sent to the "errors.txt" file
}	
}
}

bool validate_length_seven (string code[], int size, ofstream& outdata, ofstream& outdata2);
{
for (int x=0; x<size; x++)
{
if (code[x].length()==7) 
{
if ((isdigit(code[x].at(0)) > 0) && (isdigit(code[x].at(1)) > 0) && (isalpha(code[x].at(2)) > 0) && (isalpha(code[x].at(3)) > 0) && (isalpha(code[x].at(4)) > 0) && (isdigit(code[x].at(5)) > 0) && (isdigit(code[x].at(6)) > 0))
								
{
outdata << code[x] << endl;  
}
else outdata2<<code[x] << endl; 
							
}
}
}				

bool validate_length_six (string code[], int size, ofstream& outdata, ofstream& outdata2);
{		
for (int x=0; x<size; x++)

{
if (code[x].length()==6) 
{
if ((isupper(code[x].at(0)) > 0) && (islower(code[x].at(1)) > 0) && (islower(code[x].at(2)) > 0) && (isdigit(code[x].at(3)) > 0) && (isdigit(code[x].at(4)) > 0) && (isdigit(code[x].at(5)) > 0))
{
outdata << code[x] << endl; 
}
else outdata2 << code[x] << endl; 
}
			
}
}

bool validate_overall_length (string code[], int size, ofstream& outdata, ofstream& outdata2)
{ 
	
//declare any variables that are local to function

for (int x=0; x<size; x++)

{
if (code[x+1]!=code[x]) //Determines if the codes are duplicates
{
				
{
if (code[x].length()==9)				
validate_length_nine (code, size, outdata, outdata2);
}
				
{
if (code[x].length()==7)
{
validate_length_seven (code, size, outdata, outdata2);
}
}

{
if (code[x].length()==6) 
validate_length_six (code, size, outdata, outdata2);
}
if (code[x].length()!=9 || code[x].length()!=7 || code[x].length()!=6)
outdata2<<code[x]<<endl;
}



else if (code[x+1]==code[x])
{
outdata<<endl;
}
}
		

} 
Last edited on
closed account (3qX21hU5)
Dont mean to sound rude or anything either but you might also want to reconsider your indentation on your code. I know everyone has there own style that works for them, but when other read your code and it is to long or to many space/indentations it makes it hard to work on and or debug. It is almost never a good thing to have your code go off the page horizontally.
Well, not just indentation, but also the insertion of line breaks would be beneficial, to keep the code within the width of the screen.
Also after you fix your indentation, and line breaks, if you are still getting errors, post the complete error messages, exactly as they appear in your development environment. Make sure these errors match the code you post.
@Brandon
Sorry about the formatting issues! I've tried to make it look more legible.
Everyone,

Sorry about the formatting issues! I've tried to make it look more legible. Please note that the code for my validation does occur as a long chain, so it will probably need to be put back into one long statement. The code is now as follows:

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
zeta4321
Date
Program Name
Objectives:
	1. To determine if codes from an input file are valid or invalid.
Steps:
	1. The codes are first read in and sorted from an array of strings using a swap function
	2. The codes are then checked for validity given the following conditions:
		a. digit/digit/letter/letter/letter/digit/digit (length=7) 
		b. digit/digit/digit/letter/letter/letter/digit/digit/digit (length=6)
		c. uppercaseletter/lowercaseletter/lowercaseletter/digit/digit/digit (length=9)
	3. If the codes are valid, they are output to a file called "valid.txt"
	4. If the codes are invalid, they are output to a file called "errors.txt"
	5. If duplicates exist, they are simply skipped over and not output to any file.
*/

#include <iostream>
#include <string>
#include <fstream>
#include <cctype>

using namespace std;

void swap(string A[], int i, int j);
void sort_the_codes(string A[], int size);
bool validate (string A[], int i, ofstream& outdata, ofstream& outdata2);
bool validate_length_nine (string A[], int i, ofstream& outdata, ofstream& outdata2);
bool validate_length_seven (string A[], int i, ofstream& outdata, ofstream& outdata2);
bool validate_length_six (string A[], int i, ofstream& outdata, ofstream& outdata2);


int main()
{
string code[1000];

ifstream indata;
indata.open("codes.txt");
ofstream outdata;
outdata.open("valid.txt");
ofstream outdata2;	
outdata2.open("errors.txt");
	


int i=0;
indata >> code[i];
while(!indata.eof())
{
	//if validate_length_nine (string code[], int size, ofstream& outdata, ofstream& outdata2);
	//outdata << code[i] << endl;
	//else outdata2 << code[i] <<endl;
i++;
indata >> code[i];
}

sort_the_codes(code,i);
validate (code, i, outdata, outdata2);
validate_length_nine (code, i, outdata, outdata2);
validate_length_seven (code, i, outdata, outdata2);
validate_length_six (code, i, outdata, outdata2);

/*
for(int j=0; j < i; j++)  
{
	
cout << code[j] << endl;	
}
*/
//Used for validation purposes

indata.close();
outdata.close();
outdata2.close();
return 0;
}



void swap(string A[], int i, int j)
{
string temp;
temp=A[i];
A[i]=A[j];              
A[j]=temp;        
return;
				
// code that defines the terms of the swap

}

void sort_the_codes(string A[], int size)
{
for (int x=1; x<size; x++) //controlling passes
					//prime loop at 0
{
for (int j=0; j<size-x; j++)  //controlling comparisons within each pass
{
if (A[j]>A[j+1]) swap (A, j, j+1);
}
}
	
// sorts the codes within the array
return;
}

bool validate_length_nine (string code[], int size, ofstream& outdata, ofstream& outdata2);

{
for (int x=0; x<size; x++)
				
{
if (code[x].length()==9) //When the code length equals nine, the function checks the conditions necessary for a nine character code to be valid
{	
if ((isdigit(code[x].at(0)) > 0) && (isdigit(code[x].at(1)) > 0) 
&& (isdigit(code[x].at(2)) > 0) && (isalpha(code[x].at(3)) > 0) 
&& (isalpha(code[x].at(4)) > 0) && (isalpha(code[x].at(5)) > 0) 
&& (isdigit(code[x].at(6)) > 0) && (isdigit(code[x].at(7)) > 0) 
&& (isdigit(code[x].at(8)) > 0));
{
outdata << code[x]<<endl;//If the code meets the stated conditions, it is output to "valid.txt" 
}
else outdata2<<code[x]<<endl; //If the code is length nine but it does not meet the stated conditions, the code will then be sent to the "errors.txt" file
}	
				
}
}
bool validate_length_seven (string code[], int size, ofstream& outdata, ofstream& outdata2);
{
for (int x=0; x<size; x++)
{
if (code[x].length()==7) 
{
if ((isdigit(code[x].at(0)) > 0) && (isdigit(code[x].at(1)) > 0) 
&& (isalpha(code[x].at(2)) > 0) && (isalpha(code[x].at(3)) > 0) 
&& (isalpha(code[x].at(4)) > 0) && (isdigit(code[x].at(5)) > 0) 
&& (isdigit(code[x].at(6)) > 0))
								
{
outdata << code[x] << endl;  
}
else outdata2<<code[x] << endl; 
							
}
}
}				

bool validate_length_six (string code[], int size, ofstream& outdata, ofstream& outdata2);
{		
for (int x=0; x<size; x++)

{
if (code[x].length()==6) 
{
if ((isupper(code[x].at(0)) > 0) && (islower(code[x].at(1)) > 0) 
&& (islower(code[x].at(2)) > 0) && (isdigit(code[x].at(3)) > 0) 
&& (isdigit(code[x].at(4)) > 0) && (isdigit(code[x].at(5)) > 0))
{
outdata << code[x] << endl; 
}
else outdata2 << code[x] << endl; 
}
			
}
}

bool validate_overall_length (string code[], int size, ofstream& outdata, ofstream& outdata2)
{ 
	
//declare any variables that are local to function

for (int x=0; x<size; x++)

{
if (code[x+1]!=code[x]) //Determines if the codes are duplicates
{
				
{
if (code[x].length()==9)				
validate_length_nine (code, size, outdata, outdata2);
}
				
{
if (code[x].length()==7)
{
validate_length_seven (code, size, outdata, outdata2);
}
}

{
if (code[x].length()==6) 
validate_length_six (code, size, outdata, outdata2);
}
if (code[x].length()!=9 || code[x].length()!=7 || code[x].length()!=6)
outdata2<<code[x]<<endl;
}



else if (code[x+1]==code[x])
{
outdata<<endl;
}
}
		

} 


For my program, I am using Visual Studio 2010. I have 6 total errors, all of them stating "IntelliSense expected a declaration." These errors occur on lines 109, 114, 122, 124, 129, and 149.

For the error on line 109, check line 107. It appears you have an errant semicolon at the end of this line.

Also "IntelliSense" errors are not compiler errors. You need to post the complete compiler errors exactly as they appear in your development environment.

For the rest of your errors you may want to see if you have more errant semicolons.

You might consider the following:

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

typedef int (*isxxx_t)(int) ;

isxxx_t valid_6[] = { isupper, islower, islower, isdigit, isdigit, isdigit } ;
isxxx_t valid_7[] = { isdigit, isdigit, isalpha, isalpha, isalpha, isdigit, isdigit } ;
isxxx_t valid_9[] = { isdigit, isdigit, isdigit, isalpha, isalpha, isalpha, isdigit, isdigit, isdigit } ;


bool is_valid( const std::string& code )
{
    isxxx_t* is_func ;

    switch ( code.length() )
    {
    case 6: is_func = valid_6 ; break ;
    case 7: is_func = valid_7 ; break ;
    case 9: is_func = valid_9 ; break ;
    default:
        return false ;
    }

    for ( unsigned i = 0 ; i< code.length(); ++i )
        if ( !is_func[i](code[i]) )
            return false ;

    return true ;
}

int main()
{
    std::ifstream in("codes.txt") ;

    std::string input ;

    while ( std::getline(in, input) )
    {
        if ( is_valid(input) )
            std::cout << input << " is a valid size " << input.length() << " code.\n" ;
        else
            std::cout << input << " is not a valid code.\n" ;
    }
}
@cire

You are awesome! Thank you so much! I ran your code, and it worked flawlessly!

One last question: how would I integrate outputting to a file within the code you have written? Unfortunately, I am only familiar with the "using namespace std;" method, and I'm not exactly sure how to integrate ifstream, ofstream, .open, and .close. I would be most grateful if you could please explain how I would need to integrate those capabilities.

Thanks again!

Here is my attempt at getting the valid codes to output to a file called "valid.txt" and the erroneous codes to output to a file called "errors.txt"

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

typedef int (*isxxx_t)(int) ;

isxxx_t valid_6[] = { isupper, islower, islower, isdigit, isdigit, isdigit } ;
isxxx_t valid_7[] = { isdigit, isdigit, isalpha, isalpha, isalpha, isdigit, isdigit } ;
isxxx_t valid_9[] = { isdigit, isdigit, isdigit, isalpha, isalpha, isalpha, isdigit, isdigit, isdigit } ;


bool is_valid( const std::string& code )
{
    isxxx_t* is_func ;

    switch ( code.length() )
    {
    case 6: is_func = valid_6 ; break ;
    case 7: is_func = valid_7 ; break ;
    case 9: is_func = valid_9 ; break ;
    default:
        return false ;
    }

    for ( unsigned i = 0 ; i< code.length(); ++i )
        if ( !is_func[i](code[i]) )
            return false ;

    return true ;
}

int main()
{
    std::ifstream in("codes.txt") ;

	std::ofstream out ("valid.txt") ;

	std::ofstream out2 ("errors.txt") ;

    std::string input ;

    while ( std::getline(in, input) )
    {
        if ( is_valid(input) )
            std::ofstream out << input << " is a valid size " << input.length() << " code.\n" ;
        else
            std::ofstream out2 << input << " is not a valid code.\n" ;
    }
}



I receive errors on lines 46 and 48 occur at the "<<" between ofstream and input. I get an error from my compiler that reads "Error: expected a ';'"

How do I go about correcting this problem?
Last edited on
out and out2 are defined on lines 37 and 39.

On lines 46 and 48 you don't need the type (std::ofstream,) just the object name.
Awesome! The code works beautifully! Thanks again!

Topic archived. No new replies allowed.