mystic logic error in my double for loop

Hello. I want to do this:

In a record I have IDs of type int but some may repeat. These IDs also got sales of type float. I wish to compare the IDs number of times they appear and of match found accumulate their sales and put it in another struct. However if an ID is found which is new I pass it to a function to initiate sales..but my code never reach second ID, given I am trying with two IDs....
given IDs in transFile

ID Sale

12345 870
90909 100

I enter these two in transFile and match it with masterRecord. 12345 is matched and updated likewise but 90909 is never executed or matched in checkID function...Please help me fix the loop!!

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
void sync_trans_files(unsigned short int count, master masterRecord[], char transFile[]){
	
	const short int weeklyemployee=25;	
	//read number of records from transFile;
	struct trans{
		long int id;
		float sale;
		
		}persons[weeklyemployee];
	unsigned short int loop=0;
	long int ID=1;
	
	float redundantSales=0.0;
	bool answer;
	ifstream read (transFile, ios::in);
	
	if (!read)
		cout<<"Open fail. Retry"<<endl;
		
	while(read.good()){
		read>>persons[loop].id;
		read>>persons[loop].sale;
		loop++;
		}	
	read.close();
	//compare redundant id of transfile 
	
	for(int i=0; i<(loop-1)-1;i++){
		ID=persons[i].id;
		for(int j=0;j<((loop-1)-i)-1;j++){
			
			
			if((persons[i].id==persons[j].id) || (persons[i].id!=persons[j].id)){
				
				answer=checkID(count, ID, masterRecord);
				cout<<answer<<endl;
				if(answer==true){
					redundantSales=masterRecord[i].salesCustomer;
					masterRecord[i].salesCustomer=redundantSales+persons[i].sale;
					cout<<masterRecord[i].salesCustomer<<endl;
					}
				if (answer==false){
					redundantSales=persons[j].sale+persons[i].sale;
					enterNewEntry(answer, ID, count, masterRecord);
					count++;
					cout<<masterRecord[i].salesCustomer<<endl;
					
					}	
				}
			}
					
		}
			
			
	}
Last edited on
Line 33 basically says
if (x==y || x != y) {
Either x==y or it doesn't, so the if statement is always true.
Also, you should replace
1
2
3
4
5
6
if (answer == true) {
   ...
   }
if (answer == false) {
    ...
}

with
1
2
3
4
5
if (answer) {
    ...
} else {
    ...
}

so what should I do for the condition if first if? I need to check if ID matches first...
You're assuming that masterRecord[i] is the master record. Is that really true? I can't tell from what you've shown.

Also, your look at line 30 looks odd. You're looping from 0 to some smallish number, looking at records that you've already seen.

What you really what to do here is this:
for each record
find the master record for this record, adding it if necessary
add the sales for this record to the master record.

This "find or add" logic is pretty common so I always call such methods findAdd():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
for (int i=0; i < loop; ++i) {
    int idx = findAddMaster(persons[i].id, masterRecord);
    masterRecord[idx].salesCustomer += persons[i].sale;
}

...
// Find or add the master record for the given ID. Return the index
int findAddMaster(int ID, master masterRecord[])
{
    int result;
    for (result = 0; result < numMasterRecords; ++result) {
        if (masterRecord[result].id == ID) return result;
    }
    // didn't find it;
    ++numMasterRecords;
    masterRecord[result].id = ID;
    masterRecord[result].salesCustomer = 0;
    return result;
}


Note my numMasterRecords variable here. It represents the number of valid records in masterRecords. You probably have a variable like this already.
What value is result returning if not found? The not found record should be added at end of master record, because it is the latest
What value is result returning if not found? The not found record should be added at end of master record, because it is the latest

That's what I'm doing at lines 15-18 of my second comment. The idea is that result always returns the index of the master record. If it can't find a master record, it creates one.

Without knowing the details of masterRecord, I can't say for sure how this should work, but hopefully you get the idea and can change the code accordingly.
Last question. I have followed you code and it is woring except that I cannot pass the new updated count for new employee to main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool checkID(unsigned short int& count, unsigned short int& index, long int ID, master masterRecord[]){
	bool answer=false;
	for(int i=0; i<count;i++){
		if(masterRecord[i].IDnum==ID){
			index=i;//keep index track to update sale of existing employees
			answer=true;	
			return answer;
		}
		
		else{
			count++;/////HERE i want to pass updated count to main. but it
                                      ///only passes the count before update
			cout<<count<<endl;//////////////
			cout<<count<<endl;////////
			index=count;
			cout<<count<<endl;////////
			return answer;
			}
	}
}
Last edited on
As written, this function will always return during the first iteration through the loop because the function basically says:
1
2
3
4
5
6
7
8
9
10
11
	for(int i=0; i<count;i++){
		if(condition){
			do_stuff;
			return answer;
		}
		
		else{
			do_other_stuff
			return answer;
			}
	}


So whether condition is true of false, the function will return.

You want to else part to happen only if the loop never finds the ID:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool checkID(unsigned short int& count, unsigned short int& index, long int ID, master masterRecord[]){
	bool answer=false;
	for(int i=0; i<count;i++){
		if(masterRecord[i].IDnum==ID){
			index=i;//keep index track to update sale of existing employees
			answer=true;	
			return answer;
		}
	}

	count++;
	cout<<count<<endl;//////////////
	cout<<count<<endl;////////
	index=count;
	cout<<count<<endl;////////
	return answer;
}

Thanks:) I solved it and submitted the program to my tutor as lab task 1
Topic archived. No new replies allowed.