I've got this code but I can't for the life of me figure out why I have an unhandled exception:
Code:
#include <iostream>
#include <fstream>
using namespace std;
#include <string>
#include <stack>
class Indent
{
public:
void processLine(string &str, int lineNum);
Indent()
{
// Handy to push an initial entry on the stack
// starting at location 0
m_startingColStack.push(0);
}
private:
stack<int> m_startingColStack;
};
void Indent::processLine(string &str, int lineNum)
{
int len = str.size();
int startingCol = 0;
// Find the location of the first non-blank
// Assuming no TABs, .... TABs would take a little more work here.
while (str[startingCol] == ' ' && startingCol < len)
startingCol++;
// Skip completely blank lines
if (startingCol == len || len == 0)
return;
// If the new starting line is > top of stack, then push it on stack
if (startingCol > m_startingColStack.top())
m_startingColStack.push(startingCol);
// Pop off the stack all of the starting locations > than
// the current starting position
if (m_startingColStack.top() == startingCol && m_startingColStack.size() > 1)
m_startingColStack.pop();
// If the top of the remaining stack == the current starting position,
// we are in good shape, otherwise we have an error to report
if (m_startingColStack.top() != startingCol)
throw "Line is not properly indented!\n";
}
int main()
{
Indent indent;
ifstream infile("D://test.txt");
string str;
if (!infile)
{
cout << "Unable to open input file" << endl;
cin.get();
return EXIT_FAILURE;
}
try
{
string str;
int lineNum=1;
while (infile)
{
getline(infile, str);
if (infile)
{
indent.processLine(str, lineNum);
}
lineNum += 1;
}
cout << "Everything is properly indented " << endl;
}
catch (string e)
{
cout << e << endl;
}
cout << "\n\nPress enter to quit...\n\n";
cin.get();
return 0;
}
any ideas would be greatly appreciated.