What Am I Doing Wrong?
The following program compiles and runs, but the code that detects
and processes an STL map error doesn't work. That is, although it
detects the problem and attempts to change the map key, the 2nd "find"
(in the else clause) doesn't get out of the main "while loop" where is
would store the new object. Please advise. TIA
#pragma warning (disable:4786)
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <algorithm>
#include <string>
#include <map>
typedef char Str4[5];
using namespace std;
typedef struct TEAMMAPTYPE
{ // Team Ids, Names
Str4 teamCode; // Team Code
string teamName; // Team's Name
bool isAdded; // Added to working stack of teams
char teamTypeCode; // Team type Code
int teamMembers1; // Count of Team Members-1
int teamMembers2; // Count of Team Members-2
} teamData;
teamData teamWork; // storage for Team info
typedef map<char*, TEAMMAPTYPE> TEAMS;
TEAMS teamMap;
map<char*, TEAMMAPTYPE>::iterator teamIt;
int GTNumbs = 7000;
void addTeamInfo(Str4 tCode, string tName)
{
Str4 tWork;
strcpy(tWork, tCode);
teamIt = teamMap.find(tWork); // see if this Team exists
while(teamIt != teamMap.end()) // if key matches
{
teamWork = teamIt->second;
if(teamWork.teamName == tName) // same Code; same Name
{
return;
}
else // same Code; different Name
{
sprintf(tWork, "%4d", GTNumbs++); // change Code and
teamIt = teamMap.find(tWork); // see if this exists
}
} // while
teamWork.isAdded = true; // store new data object
teamWork.teamMembers1 = teamWork.teamMembers2 = 0;
strcpy(teamWork.teamCode, tWork);
teamWork.teamName = tName;
teamWork.teamTypeCode = '?';
teamMap.insert(TEAMS::value_type(tWork, teamWork));
return;
}
int main() // check if Team data exists; add it if not
{
addTeamInfo("1234", "My Team 1");
addTeamInfo("2345", "My Team 1");
addTeamInfo("1234", "My Team 2");
addTeamInfo("1234", "My Team 1");
size_t mSize = teamMap.size();
return EXIT_SUCCESS;
}