problems comparing 2 iterators

Oct 23, 2010 at 1:12am
I need to compare the a string from codes.dat to a string in sales.dat and if they are equal do nothing for now and i will implement it later, if they don't equal I want to go through the whole sales.dat set to find if they equal. If I find one that doesn't I want to remove that from the set.

I am having issues with the compare at the moment and don't really have a lot of experience with sets so I was wondering if ya'll could help me out.

The error occurs at line 90, and there is most likely an error at line 92 as well


codes.dat:
NWL269
BBM904
BHC869
ARZ410
WKK497
HID115
QSC023
XRJ586
OWF193
XSJ179
BLD381
EFS894
RCB515
NEC865
YGG914

sales.dat:
NWL269
BHC869
ARZ410
QSC023
XRJ586
OWF193
BLD381
NEC865
YGG914



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
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <set>

using namespace std;


//--------------------------------------------------
//------------Main Function-------------------------
//--------------------------------------------------

int main() {
   
	int saleCodesNum = 0;	
	int numCodes = 0;
	
	set<string> codes1;		
	set<string> saleCodes1;	
	set<string>::iterator it;	
	set<string>::iterator it2;	
	
	string codes;	
	string saleCodes;  
	
	ifstream codesFile;		//the file that codes come from.
	ifstream salesFile;		//the file that sales codes come from.
	
	int breakFromSearch = 0;

//--------------------------------------------------
//------------read from files-----------------------
//--------------------------------------------------	
	codesFile.open("codes.dat");
	salesFile.open("sales.dat");

	
	if(codesFile){
		while(codesFile >> codes){
			numCodes++;
			codes1.insert(codes);
		}
	}
	else 
	  cout << "The file did not open correctly." <<endl;
	
	if(salesFile){
		while(salesFile >> saleCodes){
			saleCodesNum++;
			saleCodes1.insert(saleCodes);
		}
		
	}else
		cout << "The file did not open correctly." << endl;

	
	codesFile.close();
	salesFile.close();
	
//--------------------------------------------------
//--------end read from files-----------------------
//--------------------------------------------------
//--------------------------------------------------
//------------check input---------------------------
//--------------------------------------------------

	cout << "codes inserted " << endl;
	  for (it=codes1.begin(); it!=codes1.end(); it++){
		  cout << " " << *it;
	  }
	  cout << endl;
  
	cout << "sale codes " << endl;
	  for (it=saleCodes1.begin(); it!=saleCodes1.end(); it++){
		cout << " " << *it;
	  }
	  cout << endl;

//--------------------------------------------------
//------------errors start here---------------------
//--------------------------------------------------
	  

	for(it=codes1.begin(); it!=codes1.end(); it++){
		 for(it2=saleCodes1.begin(); it2!=saleCodes1.end(); it2++)
		 {
			 if(it==it2){
				 cout << "it == it2...it worked!!" <<endl;
			 }else if(breakFromSearch==saleCodes1.end()){
				 codes1.erase(it);
			 }else{
				 breakFromSearch++;
			 }
		 }
	 }

	return 0;
}




Also in a realated problem when I put the the sets in a function ie...


And I include everything I get a boat load of errors/warnings and I was wondering if anyone could help me out with that have the function set up like..


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

void readDataIn(ifstream codesFile, ifstream salesFile, set<string> codes1, set <string> saleCodes1, 
		string codes, string saleCodes,int numCodes, int saleCodesNum){

	if(codesFile){
		while(codesFile >> codes){
			numCodes++;
			codes1.insert(codes);
		}
	}
	else 
	  cout << "The file did not open correctly." <<endl;
	
	if(salesFile){
		while(salesFile >> saleCodes){
			saleCodesNum++;
			saleCodes1.insert(saleCodes);
		}
		
	}else
		cout << "The file did not open correctly." << endl;
Oct 23, 2010 at 8:09am
You can't compare iterators into different containers. It's like thinking the first child one one family is same as the first child in a different family.

You probably what to compare the item the iterator indicates, in which you have to dereference them:
 
    if (*it == *it2)

Last edited on Oct 23, 2010 at 8:27am
Oct 23, 2010 at 5:02pm
Hey thanks that worked.

Umm now I run into issues when my sales.dat has more than 1 sales code in it and it have to control C to end it. I get this when there more than one sale codes in that data file:

1
2
3
4
5
6
7
8
9
10
11
12
codes inserted
 ARZ410 BBM904 BHC869 BLD381 EFS894 HID115 NEC865 NWL269 OWF193 QSC023 RCB515 WKK497 XRJ586 XSJ179 YGG914
sale codes
 OWF193 XRJ586 XSJ179
printing *it: BBM904
printing set after removed.
 ARZ410 BHC869 BLD381 EFS894 HID115 NEC865 NWL269 OWF193 QSC023 RCB515 WKK497 XRJ586 XSJ179 YGG914
printing *it:
printing set after removed.
 ARZ410 NWL269 OWF193 QSC023 RCB515 WKK497 XRJ586 XSJ179 YGG914
printing *it: BBM9041à`Ð`Ð`¸`1BHC8691p``1ARZ4101à`ð``x`1WKK4971Ð``Ø`1HID11510`°`0`8`1QSC0231``1XRJ5861ð`ø`1OWF19310`P`ð`X`1XSJ1791Ð`¸`1BLD3811p`p```1EFS8941ð`x`1RCB5151`Ø`1NEC861XSJ179¡Ä^C931`X5861H^°`



I have modified my code a little bit but not much. I was also wondering if there is a way to get the last integer of the codes through the iterators, I've been working with that for a while but to no avail. The commented out section at the bottom is where I am trying to work with that.


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 <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <set>

using namespace std;


//--------------------------------------------------
//------------Main Function-------------------------
//--------------------------------------------------

int main() {

	long codesMax = 15;   //const for the amount of codes that there are to be
	int saleCodesNum = 0;	//the number of codes that produce a 10 cent donation
	int numCodes = 0;	//the amount of codes that there actually are
	
	set<string> codes1;		//the set for codes1
	set<string> saleCodes1;	//the set for the codes that produce a 10 cent donation
	set<string>::iterator it;	//iterator to go through the sets
	set<string>::iterator it2;	//iterator to go through the sets
	set<string>::iterator it3;	//iterator to go through the sets
	
	string codes;		//an array of the codes that can later be inserted into the set
	string saleCodes;		//an array of the codes that produce 10 cents to the foundation
	
	ifstream codesFile;		//the file that codes come from.
	ifstream salesFile;		//the file that sales codes come from.
	
	int breakFromSearch = 0;	
	
//------------------------------------
//--the sales of each type of yogurt--
//------------------------------------
	int vanillaSales = 0;
	int strawberrySales = 0;
	int blueberrySales = 0;
	int raspberrySales = 0;
	int keylimeSales = 0;
	int mixedberrySales = 0;
	int rainbowSales = 0;
	int bostoncreampieSales = 0;
	int redvelvetcakeSales = 0;
	int mangoSales = 0;
	
//--------------------------------------------------------------------
//------function calls and implimentation-----------------------------
//--------------------------------------------------------------------
	
	codesFile.open("codes.dat");
	salesFile.open("sales.dat");

	
//-------------------------------------------------------------------------
//-----------------------read from file------------------------------------	
//-------------------------------------------------------------------------	
	if(codesFile){
		while(codesFile >> codes){
			numCodes++;
			codes1.insert(codes);
		}
	}
	else 
	  cout << "The file did not open correctly." <<endl;
	
	if(salesFile){
		while(salesFile >> saleCodes){
			saleCodesNum++;
			saleCodes1.insert(saleCodes);
		}
		
	}else
		cout << "The file did not open correctly." << endl;
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
	
	codesFile.close();
	salesFile.close();
	
	cout << "codes inserted " << endl;
	  for (it=codes1.begin(); it!=codes1.end(); it++){
		  cout << " " << *it;
	  }
	  cout << endl;
  
	cout << "sale codes " << endl;
	  for (it=saleCodes1.begin(); it!=saleCodes1.end(); it++){
		cout << " " << *it;
	  }
	  cout << endl;
	  
	 for(it=codes1.begin(); it!=codes1.end(); it++){
		 for(it2=saleCodes1.begin(); it2!=saleCodes1.end(); it2++)
		 {
			 if(*it==*it2){
				 
				 cout << "it == it2...it worked!!" <<endl;
				 
			 }else if(breakFromSearch==saleCodesNum){
				 
				 cout << "printing *it: " << *it << endl;
				 cout << "printing set after removed.\n";
				 codes1.erase(it);
				  for (it3=codes1.begin(); it3!=codes1.end(); it3++)
					  cout << " " << *it3;
				  cout << endl;
				  
			 }else{
				 
				 breakFromSearch++;
				 
			 }
		 }
	 }
	 
/*	 for(it=codes1.begin; it!=codes1.end(); it++)
	 {
		 if(*it.substr(4,5)==0){
			 vanillaSales++;
		 }else if(*it[5]==1){
			 strawberrySales++;
		 }else if(*it[5]==2){
			 blueberrySales++;
		 }else if(*it[5]==3){
			 raspberrySales++;
		 }else if(*it[5]==4){
			 keylimeSales++;
			 cout << "Key Lime Sales: " << keylimeSales << endl;
		 }
	 }*/
	  	 
	return 0;
}


Topic archived. No new replies allowed.