Reading data input Problem

Hello,

I'm trying to read data from an script file (for a game), and the data is not being read correctly. Here is my function:

void intializeRoute( string filename, Route& route )
{
      ifstream fin;
      fin.open (filename.data(), fstream::out);
        // I know that the file is open because the caller function checks

      string currentWord;

      while(fin >> currentWord)
      {
                // For comments in the file
            if(currentWord.compare("!=") ==  0)
                  if(currentWord.compare("=") == 0)
                        if(currentWord.compare("=!") == 0)
                        {
                              // (for testing) cout << "COMMENT" << endl;
                              string routeComment;
                              getline(fin, routeComment);
                        }
            else if(currentWord.compare("TradingPost") ==  0)
            {
                  string name;
                  getline(fin, name);
                  name.erase(0, 1);
                  route.addTradingPost(name);
                  cout << "Trading Post Added" << endl;
                        // This never gets printed although the keyword "TradingPost" is in the file
                  // getchar();
            }
            else if(currentWord.compare("Merchant") == 0) {
                  string merchName;
                  int currency;
                  fin >> merchName >> currency;
                  route.addMerchant(merchName, currency);
            }
            else if(currentWord.compare("Description") == 0) {
                  string desc;
                  getline( fin, desc );
                  desc.erase( 0, 1 );
                  route.addGoodDescription(desc);
            }
            else if(currentWord.compare("Good") == 0) {
                  string goodName;
                  int value;
                  fin >> value;
                  getline(fin, goodName);
                  goodName.erase(0, 1);
                  route.addGood(goodName, value);
            }
            else if(currentWord.compare("Arrangement") == 0) {
                  string tpDest, type;
                  int cost;
                  fin >> type >> cost;
                  getline(fin, tpDest);
                  tpDest.erase(0, 1);
                  route.addTravArrange(tpDest, type, cost);
            }
            else
                  cout << "Bad line in script" << endl;
      }
      fin.close();
}

Thanks,

Igor
igor92128Asked:
Who is Participating?
 
efnConnect With a Mentor Commented:
There is a problem here:

          if(currentWord.compare("!=") ==  0)
               if(currentWord.compare("=") == 0)
                    if(currentWord.compare("=!") == 0)
                    {
                         // (for testing) cout << "COMMENT" << endl;
                         string routeComment;
                         getline(fin, routeComment);
                    }
          else if(currentWord.compare("TradingPost") ==  0)

C++ doesn't care about indentation, so the else is associated with the last preceding if.  So to reflect the actual logical structure, it should look like this:

          if(currentWord.compare("!=") ==  0)
               if(currentWord.compare("=") == 0)
                    if(currentWord.compare("=!") == 0)
                    {
                         // (for testing) cout << "COMMENT" << endl;
                         string routeComment;
                         getline(fin, routeComment);
                    }
                    else if(currentWord.compare("TradingPost") ==  0)

So in all the cases where currentWord is not "!=", there is no else block and so it does nothing.

This logic is faulty anyway.  currentWord is not going to be equal to more than one string at a time, so if the first if condition is true, the others will always be false.

However, if you are correct that control is not getting into the while block, that must be due to some other problem.
0
 
igor92128Author Commented:
Also, this is the format of my initialization file:

!=      These are just comments
 =
 =
 =
 =
 =
 =
 =
 =      Hello Hello
 =
 =
 =
 =!

TradingPost Odessa
Merchant Vasili 5000
Description Warm Baked Bread
Good 13 bread
Description A Dozen Fresh Hen Eggs
Good 23 eggs
Description Brown Potatoes
Good 11 potato
Description Vitamin D Milk
Good 32 milk
Desciption Green Onion
Good 9 onion
Merchant Spopovitch 6000
Description Warm Baked Bread
Good 14 bread
Description A Dozen Fresh Hen Eggs
Good 21 eggs
Description Brown Potatoes
Good 12 potato
Description Vitamin D Milk
Good 37 milk
Desciption Green Onion
Good 6 onion
Desciption Distilled Vodka
Good 28 vodka
Merchant Ahmed 3000
Description A Dozen Fresh Hen Eggs
Good 20 eggs
Description Brown Potatoes
Good 17 potato
Description Vitamin D Milk
Good 29 milk
Desciption Green Onion
Good 11 onion
Arrangement caravan 60 Armensk
Arrangement caravan 150 Kharkiv

Igor
0
 
igor92128Author Commented:
Its not even entering the while loop...
0
 
igor92128Author Commented:
Maybe the compare() method is broken? Should I try another?
0
 
igor92128Author Commented:
Yeah I fixed the problem by simplifying the comments.
I was trying to read from a blank file before too so thats why I wasn't it wasn't entering the while loop.

Igor
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.