Interesting Issue with a nested loop...

Alright, basically the following loop accepts a Card custom data type, the char* that the Card's RFID is being compared to, and the size of the table. (The RFID in the Card data type is just a char*). Basically this loop functions correctly the first time it runs through, and then it does not function correctly. It appears that it can't find the table entry. The Loop itself will be right below, and the source files will follow.

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
int rfidSearch( Card table[], char* goal, int size)
{
	int count = 0;
	
	for(int i = 0; i < size; i ++)
	{
		for(int j = 0; j <= strlen(goal); j ++)
		{
			if(table[i].RFID[j] == goal[j])
			{
				if(j == strlen(goal))
					return count;
			
			}
			else if(table[i].RFID[j] != goal[j])
			{	
				j = strlen(goal) + 1;			
			}
		}
		
		count++; 
	}
	count = -1;
	
	return count;
}


main.cpp
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
//#include <stdio.h>
#include "class.h"
//#include "equipment.h"
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cstring>

using namespace std;

const int nameLength = 10; //Dictates the length of the name.
const int rfidLength = 10; //Dictates how long rfid char* is, can easily be changed to accomadate more info. 

// Allows for sending of data using the extraction operator

ostream& operator<<(ostream& out, Card& other){
	char* rfid;
	rfid  = new char[20];
	
	char* name;
	name  = new char[20];
	
	double balance = 0;
		
	strcpy(rfid, other.getRFID());
	for(int i =0; i < strlen(rfid); i ++)
	{
		 out << rfid[i];
	}
	
	balance = other.checkBal();
	
	out << " " << balance << " ";
	
	strcpy(name, other.getName()); 
	for(int i = 0; i <strlen(name); i ++)
	{
		 out << name[i]; 
	}
	
	out << endl; 
	
	delete[] rfid;
	delete[] name; 

	return out; 
}

int rfidSearch( Card table[], char* goal, int size)
{
	int count = 0;
	
	for(int i = 0; i < size; i ++)
	{
		for(int j = 0; j <= strlen(goal); j ++)
		{
			if(table[i].RFID[j] == goal[j])
			{
				if(j == strlen(goal))
					return count;
			
			}
			else if(table[i].RFID[j] != goal[j])
			{	
				j = strlen(goal) + 1;			
			}
		}
		
		count++; 
	}
	count = -1;
	
	return count;
}


int main()
{
	bool  done = false;
	char* input; //Receives the card number
	char*  temp1;
	char*  temp2; // Holds the information before it is stored in card database.
	double holder; 
	char  c; // Gets 1 letter at a time from account database.
	ifstream inputFile;
	int length = 0; 
	int size   = 0;
	int index  = 0; 
	Card* data;
	
	inputFile.open("accounts.txt", ifstream :: in);		
		
	inputFile.seekg(0L, ios :: end);
		
	length = inputFile.tellg();

	inputFile.seekg(0L, ios :: beg);

	size = length/(sizeof(char)*rfidLength+sizeof(char)*nameLength+sizeof(double)); 
	
	data = new Card[size];
	
	temp1 = new char[rfidLength];
	temp2 = new char[nameLength];

	for(int i = 0; i < size; i ++)
	{	
		temp1[0] = '\0';
		temp2[0] = '\0';
		
		inputFile.get(temp1 , 100, ' '); 
			
		data[i].setRFID(temp1);

		inputFile >> holder;

		data[i].setBal(holder); 
		
		inputFile.get(temp2, 100); 
		
		data[i].setName(temp2);
	}

	while(!done)
	{			
		input = new char[rfidLength];
		
		if(cin >> input)
			index = rfidSearch(data, input, size);
		
		done = true; 
		
	}
	delete[] input;
	return 0;
}

class.h
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
//#include "equipment.h"
#include <cstring>
#include <iostream>
#ifndef CLASS_H_
#define CLASS_H_

using namespace std; 

class Card
{
	private: 
		
		char*    RFID; 
		double   amntAvailable; 
		char*    customerName; 
	
	public:
	
	// Default Constructor
	Card(){
	
		RFID = new char[20]; //Important: must be changed to increase length of RFID; 
		
		amntAvailable = 0;
		
		customerName = new char[50]; // Important: must be changed to increase amount of space available; 
	}
	
	// Constructor with Arguments
	Card(char* ID, double initial, char* a){
		
		RFID = new char[strlen(ID)]; 
		customerName = new char[strlen(a)];
		
		strcpy(RFID, ID);
		amntAvailable = initial;
		strcpy(customerName, a);
			
	}
	
	// Copy Constructor 
	Card(const Card &other)
	{
		RFID = new char[strlen(other.RFID)]; 
		customerName = new char[strlen(other.customerName)];
		
		strcpy(RFID, other.RFID); 
		
		amntAvailable = other.amntAvailable; 
		
		strcpy(customerName, other.customerName); 
	}
	
	// Destructor
	~Card(){
		delete[] RFID; 
		delete[] customerName; 	

	}
	
	// Overloaded assignment operator
	Card operator= (const Card& other){
		
		if(this != &other)
		{
			delete[] RFID;
			delete[] customerName;
			
			amntAvailable = other.amntAvailable; 
			
			RFID = new char[20]; 
			customerName = new char[50];
			
			strcpy(RFID, other.RFID); 
			strcpy(customerName, other.customerName); 

		}
			
		return *this; 	
	}

	char* getRFID(){
		
		return RFID; 
		
	}
	
	char* getName(){
			
		return customerName;
	}
	
	void setBal(double other){
		amntAvailable = other;
		
	}
	
	void setName(char* other){
		strcpy(customerName,other); 
		
	}
	
	void setRFID(char* other){
		strcpy(RFID, other); 
	
	}
	
	double checkBal(){
		
		return amntAvailable;
			
	}
	
	bool raiseBal(int amnt){
		
		if(amntAvailable += amnt)
			return true;
			
		else
			return false; 	
	}
	
	bool reduceBal(double amnt){
		
		if(amntAvailable >= amnt)
		{
			amntAvailable -= amnt; 
			return true; 
		}
			
		else 
			return false;	
	}
	
	
   friend ostream& operator<<(ostream&, Card&);
   friend int      rfidSearch(Card[]  , char*, int);
};


#endif /*CLASS_H_*/ 

accounts.txt
1
2
3
4
5
0010508538 10.00 Generic nAme
0010500654 10.00 Generic naMe
0010509230 10.00 Generic namE
0010454196 10.00 Generic NamE
0010475459 10.00 Generic NAME

Ill expand a little bit, I did some debugging by outing the table entries char and the goal char to check comparisons, and noticed that after the first entry in the table outing table[I].RFID[j] gave nothing , any insight would be greatly appreciated!
while reading the file, you are appending the new line character with the rfid. so its kind of searching:

"\n0010508538" == "0010508538"

which will never match.
Huh, didn't notice that before, thanks dude.
I'm revealing my noobiness with some of the finer points of working with char*'s here, but how would i go about fixing that?
whenever you read any string, always trim them for spaces, new line characters. write a trim function which will do this.

So its like:

read line
make tokens
trim each token.

the problem came because you are reading for a ' ' and not for new line.
Thanks for all your help.
Topic archived. No new replies allowed.