[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Reading data input Problem

Posted on 2005-05-07
5
Medium Priority
?
283 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:igor92128
  • 4
5 Comments
 

Author Comment

by:igor92128
ID: 13952651
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
 

Author Comment

by:igor92128
ID: 13952754
Its not even entering the while loop...
0
 

Author Comment

by:igor92128
ID: 13952777
Maybe the compare() method is broken? Should I try another?
0
 
LVL 15

Accepted Solution

by:
efn earned 2000 total points
ID: 13952981
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
 

Author Comment

by:igor92128
ID: 13953091
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question