0

I am trying use the content of a file as input and separate operands, parenthesis and operators. Since the input file contains 2 lines of input, I thought I should read the whole line instead of a value at a time because I do not want to mix up the values from the two lines. What I was thinking of doing is

  1. Using getline and store one line at a time into a string variable called input
  2. Break down the string (using white space as delimiter) into segments and push them into a stack called tempContainer.
  3. Store tempContainer.top() into a temp variable and call tempContainer.pop()
  4. Process temp to separate parenthesis from operand and store them into two different variables.

Everything went well till I try pushing the last data into the stack. I checked the values before calling tempContainer.push(temp); and everything checks out fine, so I don't get why I am getting a segmentation fault. The error occur during runtime, not during compilation.

Output produced:

A + B * (C - D * E) / F    <-----The original line to break down. Line 1
A
+
B
*
(C
-
D
*
E)
/
F
AB * CDE + (RST - UV / XX) * 3 - X5  <-----Line 2
AB
*
CDE
+
(RST
-
UV
/
XX)
*
3
-
//Segmentation fault here

Here's the code (the line with the error is near the bottom)

int main(int argc, char* argv[])
{
   string input, temp;
   fstream fin;
   stack<string>aStack;
   vector<string>sOutput;
   stack<string>tempContainer;
   int substr1, substr2;

   fin.open(argv[1], ios::in);
   if(!fin.good())
   {
      //...
   }
   else
   {
      while(!fin.eof())
      {
         getline(fin, input);
         cout << input << endl; //For verifying the content of input. Delete later
         if(input[0] == '\0')  //To prevent reading the last data in a file twice
         {
            break;
         } 
         else
         {
            //+++++++++++++++++++Breaking down string into sections++++++++++++++++++++
            //Storing the unprocessed segments of the original string into a stack
            //segments will be popped out later to be processed to separate parenthesis
            substr1 = 0;
            substr2 = 0;

            for(int i = 0; i < input.length(); )
            {
                while(input[i] != ' ')
                {
                   substr2++;
                   i++;
                }
                temp = input.substr(substr1, substr2 - substr1);
                substr2++;
                substr1 = substr2;
                i++;

                tempContainer.push(temp);  //ERROR here
                cout << tempContainer.top() << endl; //For testing purpose, delete later.
            }
            //+++++++++++++++++++++Finish breaking down strings++++++++++++++++++++++
         }
      }
   }
}

Could you help me track down the error(s)? Thank you for your time!

Vince
  • 121
  • 1
  • 9
  • It's hard to help you debug this with so much missing code. For instance, the variable declarations would be useful. – GWW May 31 '13 at 02:58
  • @GWW I was afraid to post too many things as I have been advised to get straight to the point previously. I'll fix this right away – Vince May 31 '13 at 02:59
  • 1
    Have you tried stepping through your code with a debugger and checking the value of `i` compared to the length of the string? You iterate `i` a bunch of times outside of the `for` loop without performing any bounds checks. – GWW May 31 '13 at 03:02
  • @GWW I don't think so. I assumed that it would be fine if I can make sure that the string was separated properly by using cout statements. I removed the cout << temp << endl; statement to get rid of unnecessary lines. – Vince May 31 '13 at 03:08
  • @GWW now that you mention it, I do recall seeing the cout statement printing out an extra line of nothing after printing out X5, which is the last value in line 2. I'll go back and have another look. – Vince May 31 '13 at 03:11
  • 1
    [`while (!eof())` is wrong.](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – chris May 31 '13 at 03:12
  • @chris derp. eof gave me a hard time once before, and somehow i am silly enough to use it again for this practice. Thanks for the reminder! – Vince May 31 '13 at 03:17
  • how big does the string grows? –  May 31 '13 at 03:36

1 Answers1

3

You need some sort of boundary checking like this:

while(i < input.length() && input[i] != ' ')
{
   substr2++;
   i++;
}
perreal
  • 85,397
  • 16
  • 134
  • 168