I have a program that is supposed to match opening and closing braces. It's supposed to work like a stack. It works until the last line of the input file. The last line is matching, but because it thinks the array size is zero it drops out and states that it doesn't match. I could really use some help on this. I have been looking at this and just can't see it. Any help, tips, comments, or advice is greatly apppreciated!
class List
{
public:
//Typedef declarations
typedefchar ET;//Element Type
staticconst ET CAPACITY = 20; //Size of the array
List()
{
//Set to zero
Position = 0;
Used = 0;
//Zero out array
for(int i = 0; i < CAPACITY; i++)
{
MyAry[i] = 0;
}
}
void front()
{
//Sets position to the first location
Position = 0;
}
void end()
{
//Gets the end position
if(Used != 0)
{
Position = Used - 1;
}
}
void prev()
{
//Subtracts one from current position
if(Position - 1 >= 0)
{
Position--;
}
}
bool empty()
{
//Used = 0 is returns true
return Used;
}
void next()
{
//Adds one to current position
if(Position + 1 < Used)
{
Position++;
}
}
int getPos()
{
//Returns current Position
return Position + 1;
}
void setPos(int NewPos)
{
//Sets Position to New Position
if((NewPos >= 0) && (NewPos < Used))
{
Position = NewPos;
}
}
bool insertBefore(ET value)
{
//Checks for position out of bounds
if(Position + 1 > CAPACITY)
{
returnfalse;
}
else
{
//If used is zero then first element
if(Used == 0)
{
MyAry[Used] = value;
}
else
{
//Shuffles everything down to make room for new element
for(int i = Used; i > Position; i--)
{
MyAry[i] = MyAry[i - 1];
}
MyAry[Position] = value;
}
Used++;
returntrue;
}
}
ET getElement()
{
return MyAry[Position];
}
bool insertAfter(ET value)
{
//Checks for position out of bounds
if(Position + 1 > CAPACITY)
{
returnfalse;
}
else
{
//If used is zero then first element
if(Used == 0)
{
MyAry[Used] = value;
}
else
{
//Moves everything down to make room for new element
for(int i = Used; i > Position + 1; i--)
{
MyAry[i] = MyAry[i - 1];
}
MyAry[Position] = value;
}
//Increment
Position++;
Used++;
returntrue;
}
}
int size()
{
//Returns size of the array
return Used;
}
void replace(int RepVal)
{
//Replace current value at position with new value
MyAry[Position] = RepVal;
}
void clear()
{
//Return the List back to zero
for(int i = 0; i < CAPACITY; i++)
{
MyAry[i] = 0;
}
//Return values to zero
Position = 0;
Used = 0;
}
void erase()
{
//Move all remaining elements down
for(int i = Position; i < Used; i++)
{
MyAry[i] = MyAry[i + 1];
}
//Decrement Used
Used--;
}
friend ostream &operator<<(ostream &stream,List L);
private:
ET MyAry[CAPACITY];
int Used;
int Position;
};
ostream &operator<<(ostream &stream,List L)
{
for(int i=0;i<L.Used;i++)
{
stream<<L.MyAry[i]<<" ";
}
stream<<endl;
return stream;
}
//Declare file streams
ifstream inFile;
ofstream outFile;
int main()
{
List stack;
char inputArray[8];
char value;
int i=0;
bool flag=true;
//Open inFile
inFile.open("StackStr.txt");
while(!inFile)
{
cout << "Error opening the inFile." << endl;
return 1;
}
//Open outFile
outFile.open("StackResults.txt");
while(!outFile)
{
cout << "Error opening the outFile." << endl;
return 1;
}
while(inFile)
{
inFile.getline(inputArray, 8,'\n');
i = 0;
flag = true;
while(inputArray[i] != '\0')
{
value=inputArray[i];
cout << value << " ";
outFile << value << " ";
i++;
if(value=='(' || value=='[' || value=='{')
stack.insertBefore(value);
elseif(value==')' || value==']' || value=='}')
{
if(stack.size()==0)
{
flag=false;
break;
}
else
{
stack.front();
if((stack.getElement()=='(' && value==')'))
{
flag=true;
stack.erase();
}
elseif((stack.getElement()=='[' && value==']'))
{
flag=true;
stack.erase();
}
elseif((stack.getElement()=='{' && value=='}'))
{
flag=true;
stack.erase();
}
else
{
flag=false;
cout << value << " ";
outFile << value << " ";
break;
}
}
}
}
if(stack.size() == 0)
{
flag = true;
}
else
{
flag = false;
}
if(flag == true)
{
cout<<endl<<"Parenthesis matched" << endl;
outFile<<endl<<"Parenthesis matched" << endl;
}
else
{
cout<<endl<<"Parenthesis Not matched" << endl;
outFile<<endl<<"Parenthesis Not matched" << endl;
}
}
system("pause");
return 0;
}
means. I know this is a crazy assignment. Our instructor told us to use the List class we created in a previous project and we aren't allowed to used the builts that C++ has to offer. I do appreciate any help that can be provided.
I suggest you to simplify your analysis.
In your code `flag' is true and you are setting it to true again. It makes it hard to follow.
Like when you do
I have changed my code, hopefully for the better, but now it's saying that nothing matches. It's showing that nothing matches because the size of the array never appears as zero. I'm sure I'm overlooking something stupid. Any help is greatly appreciated!