I’m trying to write a program that will validate a user input consisting of brackets for proper nesting using a stack. I am trying to do this without the use of STL containers or recursion. I have somewhat hit a road block and I’m looking for a little nudge in the right direction. I think I am kind of close, but I feel like I may be oversimplifying it (I’m in the process of learning through self teaching)
Here is what I have so far:
#include <iostream>
#include <string>
#include "ArrayStack.h"
using namespace std;
bool test(char *argg);
int main()
{
string input;
int size = 50;
cout << "enter here: ";
getline(cin, input);
for (int i = 0; i < size; i++)
test(input[i]);
}
bool test(char *argg)
{
ArrayStack S;
char D;
while ( *argg ) {
switch( *argg ) {
case '[': case '{': case '(':
S.push( *argg );
break;
case ']':
if( S.isEmpty() )
return false;
D = S.pop();
if( D!='[' )
return false;
break;
case '}':
if( S.isEmpty() )
return false;
D = S.pop();
if( D!='{' )
return false;
break;
case ')':
if( S.isEmpty() )
return false;
D = S.pop();
if( D!='(' )
return false;
break;
default:
return false;
}// end switch
argg++;
}// end while
return S.isEmpty(); // return true if reach here with empty stack
}
Thanks for any assistance in advance
If anything, you’re overcomplicating it
Should be all you need.