Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Algorithms and flowcharts (Yet another C++ assignment).
#15
Starting from the end: if you are passing something from the main program to the function, both of them need to know what it is. Therefore, it needs to be declared in both places. So add this to the main:
Code:
int linepos, wordpos;
  string word;

Next, the purpose of that bit of code. Yes, it's checking for something like +2+3. It does this by looking at the value of linepos/wordpos because when the function starts it sets them to -1. If they have a value other than that, it means they have already been assigned one, which means we've already seen that indicator. In the case of the +2+3 example, when we read the second '+', we check and see that linepos is already 2 (instead of -1, which indicates "not set yet"), and so we refuse to accept the 3.
A different way to do this would have been by using boolean variables, thus:
Code:
boolean have_linepos = false;
  boolean have_wordpos = false;
...
  if (ch == '+')
  {
    if (have_linepos) ExitWithError("Saw a '+' when already have linepos");
    else
    {
       have_linepos = true;
       linepos = num;
     }
  }
  else if (ch == '-')
  // same thing with wordpos

Remember that this is inside a loop, so it can happen more than once. It looks a little odd because it looks like you're asking about linepos' value before you had a chance to set it, but no, this is checking whether a previous iteration of the loop already set it.

If the program is submitted on UNIX you need to find a way to develop it on UNIX. Presumably "cin >> name" works there, and then you'd need c_str() for the "open" function since it wants a char * argument.
Code:
cin >> inFileName;
  inF.open(inFileName.c_str());
Note that c_str() works here because open doesn't try to change the string that is passed to it.


Finally, if it's due soon you need to find other helpers. It's 2:38 AM here and I've been asleep and will go back to sleep soon. Just got up to get a drink of water.
Reply


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

Forum Jump:


Users browsing this thread: 1 Guest(s)