Thanks for your help . I am trying to use the Map to store the data i want. However, i have to convert the data i read from the stream to int. I have quoted in my code what i am trying to do. I tried using stringstream to convert the output from in.get(ch) to an int but i couldn't do it. I also tried to use expressions of the form in >> base where base is int, but that also didn't work. How can i make this work?
Thank you
Code:
#include <iostream>
#include <fstream>
#include <map>
using namespace std;
int main() {
ifstream in;
string fileName, words, flag, flagbase;
char ch;
int countL, countM, countML, i, base;
map<int,int> baseAd;
cout << "Enter the name of the file: ";
cin >> fileName;
in.open(fileName.c_str());
i = 1;
base = 0;
countL = 1; //line counter
countML = 1; //line counter inside module
countM = 1; //module counter
if (in.is_open()) {
while (in >> words) {
cout << words << endl;
in.get(ch);
if (flag == "progtext" && flagbase != "done") {
// i am trying to find a way to convert the result from in.get(ch) to int and store it to base
baseAd[countM] = base;
cout << "test " << base << endl;
flagbase = "done"; //the computing of the base address of the current module is done
}
if (ch == '\n') {
countL ++;
countML ++;
cout << "new line " << countM << "." << countML << endl;
switch (countML) { //keep track of the current module list
case 1:
flag = "deflist";
break;
case 2:
flag = "uselist";
break;
case 3:
flag = "progtext";
break;
}
if (countL % 3 == 0) { //each module has 3 lines
countM++;
flagbase = " "; //i reset the flag
}
if (countML == 3) //each module has 3 lines, so i initialize the module line counter every 3 lines
countML = 0;
}
}
in.close();
for (i; i<=baseAd.size(); i++) { //just print the contents
cout << "size " << baseAd.size() << endl;
cout << "base " << baseAd[i] << endl;
}
}
else
cout << "Unable to open file" << endl;
return 0;
}