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
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <set>
usingnamespace 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;
}
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:
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.)