string sets and accessing single character in it

I need to access the last character of the string sets to determine if the integer is 0-9 and then doing something after I determine what the integer is.The commented out section of the code is where I want to determine what the last integer is.

This is how the codes.dat and sales.dat are set up

1
2
3
4
5
NWL269
BBM904
BHC869
ARZ410
WKK497




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

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <set>

using namespace std;

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

int main() {

	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
	
	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;
	int counter = 0;
	int counter2 = 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();


//-------------------------------------------------------------------------
//--- Searching the codes to see if there are sale codes to match ---------
//-------------------------------------------------------------------------

	  cout << "SalecodesNum:  " << saleCodesNum << endl;
	 for(it=codes1.begin(); it!=codes1.end(); it++){
	   for(it2=saleCodes1.begin(); it2!=saleCodes1.end(); it2++)
		 {
		     if(*it==*it2){
		    	 
			   //cout << "hey it worked bitch\n";
			   //cout << "it: " << *it << " it2: " << *it2 << endl;

			 }else{

		         breakFromSearch++;

				 if(breakFromSearch==saleCodesNum){
					 //cout << "removing *it: " << *it << endl;
					 codes1.erase(*it);
				 }
		      }

		 }
	   breakFromSearch = 0;
	 }
	   cout << "printing codes1 after everything is removed: " << endl;
	   for(it=codes1.begin(); it!=codes1.end(); it++){
	     cout << " " << *it;
	   }
	   cout << endl;



//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------


/*	 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;
}

Last edited on
br0k3nfr4m3s wrote:
I need to access the last character of the string sets to determine if the integer is 0-9 and then doing something after I determine what the integer is.


First things first: classes and functions -- use them liberally. A single 100+ line main() function is verging on unmaintainable.

Secondly, this looks like a homework assignment and probably belongs on the "Beginners" forum rather than "General C++ Programming".

One way to handle this is to use a case statement:

121
122
123
124
125
126
127
128
129
130
131
    for(it=codes1.begin; it!=codes1.end(); it++)
    {
        const char c = (*it)[5];
        switch (c)
        {
        case '0':
            vanillaSales++;
            break;
        case '1':
             strawberrySales++;
             ...


Another way to handle this involves a map<std::string, Function> where "4" maps to a function/functor that effectively does "raspberrySales++". (That's the "General C++ Programming" answer.)

Hope this helps.

Alright thanks man and my main is long but everything is going into functions (teachers instruction) and sorry for the wrong area.
Topic archived. No new replies allowed.