Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Algorithms and flowcharts (Yet another C++ assignment).
#13
Sorry for taking so long to get to this.

As for the c_str() thing - you can't use it there because it is "const char *", meaning it's read-only. You can't use it if you're trying to update the value of the string.

I just rechecked the reference and http://www.cplusplus.com/reference/strin...tor%3E%3E/ says "cin >> inFileName" should work. I don't know why your compiler is giving you a hard time with it. Personally I'd just use the same workaround as before, changing the variables to char[256], because that really isn't the important part of this assignment and we shouldn't waste time on it.

GetWord() is the important bit. What it needs to do is get the next word, including its position, from the input file provided.
We discussed the overall algorithm before (read two numbers, decide which is lineNum and which is wordNum, and then read the word).
Now we must finish the little details: the terminator (:0:0) and dealing with whitespace.

Some more code (I don't have a compiler on this comp so there may be errors)

Code:
char NonWhiteChar(istream &inF)     // function to find the next meaningful input character
{
    char ch;
    do
    {
       inF >> ch;
       if inF.eof() return '\0';
    } while (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
    return ch;
}

void GetWord(istream & inF, int & linepos, int & wordpos, string & word)
{
    char ch;  
    int num;
    linepos = wordpos = -1;     // indicates they don't have values yet

// Read two numbers, in unknown order and with possible whitespace between them
    do
    {
        ch = NonWhiteChar(inF);
        if (ch == '+' || ch == '-')
        {
             inF >> num;
             if (ch == '+')
             {
               if (linepos > 0) ExitWithError("two line positions provided for one word");
               else linepos = num;
             }
             else
             {
               if (wordpos > 0) ExitWithError("two word positions provided for one word");
               else wordpos = num;
             }
         }
         else if (ch == ':')
         {
          // read the next 3 characters and if they are 0:0 then exit, otherwise exit with format error message - not coded yet
         }
         else ExitWithError("Format error: expecting +, -, or :");
     } while (linepos < 0 or wordpos < 0);

     inF >> word;      // this will likely give you the same trouble as cin >> inFileName... if someone doesn't post a fix for that soon you'll need to workaround with a char[] again :-(

     return;
}

The way you use this function is in your main loop. You call GetWord, and then do the switch for putting "word" in line#linepos of the struct. Do that repeatedly until input is exhausted.
Reply


Messages In This Thread
Algorithms and flowcharts (Yet another C++ assignment). - by SaptaZapta - 2013-09-25, 02:54 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)