Validation functions

Pages: 12
I'm writing a program to check whether codes from a file are invalid, valid, inactive, or valid and active, but can't get it to work properly. The invalid codes are being found, but the other three are not. I think it may have something to do with my "active" function.

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
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;

struct ActiveCodes
{
	string code;
	bool flag;
};

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

void sort(ActiveCodes 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;
}

bool LLDDLL(string CheckCode)
{
	if(CheckCode.length()!=6)
	{return false;}

	else if(isalpha(CheckCode.at(0)) && isalpha(CheckCode.at(1)) && isdigit(CheckCode.at(2))
		&& isdigit(CheckCode.at(3)) && isalpha(CheckCode.at(4)) && isalpha(CheckCode.at(5)))
		{return true;}

	else
	{return false;}
}

bool active(string CheckCode[], ActiveCodes A[], int size)
{
	if(CheckCode[size]==A[size].code)
	{
		A[size].flag=false;
		
	}
		return true;
}



int main()
{
	ifstream fin1, fin2, fin3;
	ofstream fout1, fout2, fout3, fout4;

	fin1.open("ActiveCodes1.txt");

	ActiveCodes A[100];

	int a=0;

	while(fin1>>A[a].code)  //while reading in data from ActiveCodes1.txt
	{
		A[a].flag=true;
		a++;
	}

	sort(A, a);

	fin2.open("CheckCodes.txt");

	int idNum[100];
	string CheckCode[100];

	int b=0;

	fout1.open("ValidActiveCodes.txt");
	fout2.open("InactiveCodes.txt");
	fout3.open("InvalidCodes.txt");


	while(fin2>>idNum[b]>>CheckCode[b])  //while reading in data from CheckCodes.txt
	{
		 if(LLDDLL(CheckCode[b])) //if valid
		 {
		 	if(active(CheckCode, A, b)) //if active
		 	{
				
		 		fout1<<idNum[b]<<"\t"<<CheckCode[b]<<endl;
		 	}	

		 	else
		 	{
				
		 		fout2<<idNum[b]<<"\t"<<CheckCode[b]<<endl;
		 	}
		 }
		
		 else  //if not valid
		 {
			
		 	fout3<<idNum[b]<<"\t"<<CheckCode[b]<<endl;
		 }

				
		b++;
	}

	
	fout4.open("ActiveCodes2.txt");
	
	for(int c=0; c<b; c++)
	{
		if(A[c].flag = true)
		{
		fout4<<A[c].code<<endl;		

		c++;
		}
	}

	fin1.close();
	fin2.close();
	fin3.close();
	fout1.close();
	fout2.close();
	fout3.close();
	fout4.close();

	return 0;
}
Your active() function will always return true so lines 100-104 will never executes
I tried putting an else statement in the active function that would return false, but that still didn't change anything.
Can you post your changed active() function?
This is the updated active() function:

1
2
3
4
5
6
7
8
9
10
11
12
13
bool active(string CheckCode[], ActiveCodes A[], int size)
{
	
	if(CheckCode[size]==A[size].code)
	{
		A[size].flag=false;
		return true;
	}

	else
		return false;
		
}
If 'size' is the size of those arrays, you are accessing and setting memory out of bounds.
I changed the CheckCode[] array to just a string, and edited the active() function, but it still seems like the function isn't setting the flags to false.

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
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;

struct ActiveCodes
{
	string code;
	bool flag;
};

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

void sort(ActiveCodes 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;
}

bool LLDDLL(string CheckCode)
{
	if(CheckCode.length()!=6)
	{return false;}

	else if(isalpha(CheckCode.at(0)) && isalpha(CheckCode.at(1)) && isdigit(CheckCode.at(2))
		&& isdigit(CheckCode.at(3)) && isalpha(CheckCode.at(4)) && isalpha(CheckCode.at(5)))
		{return true;}

	else
	{return false;}
}

bool active(string CheckCode, ActiveCodes A[], int size)
{
	for(int i=0; i<size; i++)
	{
		if(CheckCode==A[i].code)
		{
			A[i].flag=false;
			return true;
		}

		else
			return false;
	}
}



int main()
{
	ifstream fin1, fin2;
	ofstream fout1, fout2, fout3, fout4;

	fin1.open("ActiveCodes1.txt");

	ActiveCodes A[100];

	int a=0;

	while(fin1>>A[a].code)
	{
		A[a].flag=true;
		a++;
	}

	sort(A, a);

	fin2.open("CheckCodes.txt");

	int idNum;
	string CheckCode;

	int b=0;

	fout1.open("ValidActiveCodes.txt");
	fout2.open("InactiveCodes.txt");
	fout3.open("InvalidCodes.txt");


	while(fin2>>idNum>>CheckCode)  //while reading in data from CheckCodes.txt
	{
		if(LLDDLL(CheckCode)) //if valid
		{
			if(active(CheckCode, A, a))  //if active
			{				
				fout1<<idNum<<"\t"<<CheckCode<<endl;
			}	

			else  //not active
			{
				fout2<<idNum<<"\t"<<CheckCode<<endl;
			}
		}
		
		else  //if not valid
		{			
			fout3<<idNum<<"\t"<<CheckCode<<endl;
		}

				
		b++;
	}

	
	fout4.open("ActiveCodes2.txt");
	
	for(int c=0; c<a; c++)
	{
		if(A[c].flag=true)
		{
			fout4<<A[c].code<<endl;		
		}
	}

	fin1.close();
	fin2.close();
	fout1.close();
	fout2.close();
	fout3.close();
	fout4.close();

	return 0;
}
Alex91 wrote:
47
48
49
50
51
52
53
54
55
56
57
58
59
60
bool active(string CheckCode, ActiveCodes A[], int size)
{
	for(int i=0; i<size; i++)
	{
		if(CheckCode==A[i].code)
		{
			A[i].flag=false;
			return true;
		}

		else
			return false;
	}
}
The loop will only execute one time because you return true or false immediately.
I don't know what I could change it to. Any suggestions?
You could try not returning true or false on the first iteration through the loop.

Think a bit more carefully about the logic of what you're trying to do. Don't worry about the C++ code at first - just try and find a way of expressing the logic naturally in your normal language. Then, once you've worked out the logic, write C++ code that performs that logic.
I need the loop to return true or false each time it runs. I thought with i++ in the loop, i would update and cause the loop to run again. I'm not understanding how that is the problem.
You return from the function when i is equal to zero without fail. So, the loop will never be executed with i greater than zero.
So maybe I should take out the else statement?
So maybe I should take out the else statement?

That will stop the function always returning on thr first iteration of the loop. But what will happen if the loop reaches the end, and CheckCode==A[i].code is never true?
Yeah, so removing else wouldn't work. I don't know if an if statement can be written differently. Would a while statement be any different?
Yeah, so removing else wouldn't work.

Wouldn't it?

I think the problem is that you haven't worked out what your function should actually do. Forget writing code for the moment. Just think about the logic of the task you're trying to achieve. Figure it out in your own language first of all. Write it down on paper, if it helps.

Once you have a clear understanding of the logic of what you're trying to do, then you can start thinking about which C/C++ control structures would best implement that logic.
I tried removing the else statement and that does not work. I know that I simply need the function to compare a code from two different files and I've tried changing just about everything I could think of, but nothing seems to change the output much if at all. The active() function runs inside of a while loop.
I tried removing the else statement and that does not work.

I assume that, when you did so, your compiler issued some warnings? Did you pay attention to them? Did you fix the code to remove the warnings?

I know that I simply need the function to compare a code from two different files and I've tried changing just about everything I could think of, but nothing seems to change the output much if at all. The active() function runs inside of a while loop.

I can only re-iterate my previous advice. Stop thinking about code. Instead, concentrate on the logic of what you want your function to do. Work out, in detail, step-by-step, what things it should be doing, in your natural language.

Once you are absolutely certain you understand what logical actions your code needs to perform, then you should start translating it into C++ code.
Last edited on
There were no warnings when I removed the else statement. I understand what you're saying about figuring out the logic first; I did that before I started the program. The problem seems to be that the flag portion of the struct does not register as false.
There were no warnings when I removed the else statement.

Then you need a better compiler. Or you're somehow suppressing important warnings. Because if you just remove the "else" clause from that function, then there is something very obviously and dangerously wrong with it, that any compiler that's any good should warn you about.

Can you see what it is?

I understand what you're saying about figuring out the logic first; I did that before I started the program. The problem seems to be that the flag portion of the struct does not register as false.

I don't mean to be rude, but I don't think you can have done. Not in enough detail, anyway. Because if you had worked out in fine detail what the step-by-step logic of the process for checking the codes is, it would be fairly clear what the appropriate C++ code would be.

I'm not being cryptic just to mess with your head. The art of programming is not in learning the syntax of a language, it's in breaking down any problem into logical steps that can then be translated into code statements. That's something that's important to practise and improve your skills in, if it doesn't come naturally to you.



Pages: 12