Hi I have been racking my head over trying to make my iterator pointer find the specified key for the map stl container. The following project is for my parser excercise where we are given a file and the point is to parse through it and read each line by line. The file is supposed to be in the project folder to be read. The file has the following text
[settings]
;this is the settings section
fullscreen = true
gamepad = false
windowWidth = 800
windowHeight = 600
[levels]
numLevels = 3
Level1 = file1.lvl
Level2= file2.lvl
Level3=file3.lvl
[player]
name = Student
speed = 5.0
[enemy]
name ="Zombie"
speed = 1.0
and we are supposed to make a Mapkey that's a combination of section (which is the text in brackets) and "|" and key (that is the text before the =), the value is the text after the equals sign.
I have code that works up until I try to use the find function to find the Mapkey specified. Can someone help??
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
std::map<std::string, std::string> m_mapPairs;
int main()
{
string strLine;
fstream myFile;
myFile.open("New Text Document.txt"); //name of the file to be read
if (myFile.is_open())
{
static string section = " ";
string key = " ";
string value= " ";
while (!myFile.eof())
{
getline(myFile, strLine);
if (strLine.find("[") !=-1)
{
int index1 = strLine.find_first_of("["); //finds it in a line, if it does it puts it in index 1 if it doesnt it doesnt bother
int index2 = strLine.find_first_of("]");//finds it in a line, if it does it puts it in index 2 if it doesnt it doesnt bother
section = strLine.substr(index1 + 1, index2 - 1);
section = section + "|";
}
if (strLine.find("=") != -1)
{
int index1 = strLine.find("");
int index2 = strLine.find("=");
key = strLine.substr(index1, index2 );
}
if (strLine.find("=")!=-1 )
{
int index1 = strLine.find("="); //finds it in a line, if it does it puts it in index 1 if it doesnt it doesnt bother
value = strLine.substr(index1+1 , 11);
}
if (strLine.find(";") > -1)
{
int index1 = strLine.find(";"); //finds it in a line, if it does it puts it in index 1 if it doesnt it doesnt bother
string ivalue = strLine.substr(index1 + 1, 11);
}
if (key != "") {
std::string Thekey = section + key;
m_mapPairs.insert(std::pair<std::string, std::string>(Thekey, value));
}
}
std::map<std::string, std::string>::iterator iter; //iterating through all the keys
iter = m_mapPairs.begin();
while (iter != m_mapPairs.end())
{
std::cout << iter->first << "\'s value is ";
std::cout << iter->second << std::endl;
iter++;
}
iter = m_mapPairs.find("settings|windowWidth"); //trying to find this key in the map but cant find it
if (iter != m_mapPairs.end())
{
std::cout << "found: " << std::endl;
return true;
}
else
{
return false;
}
myFile.close();
}
int a;
std::cin >> a;
return (0);
}
I will be grateful for any help. Thanks