Im trying to create a c program that checks to see if a give

Im trying to create a c++ program that checks to see if a given file is balanced but the code constantly says it is balanced when it is not, code:

#include <iostream>
#include <fstream>
#include <stack>
#include <string>

using namespace std;

int main()
{
ifstream infile;
char f[100];

cout << \"Enter the name of the file you wish to open.\" << endl;
cin >> f;
infile.open(f);

//cout << f << endl;

if (infile.fail())
{
  cout << \"Cannot find file.\" << endl;
}
else
{
  char c;
  stack <char> stack;
  int x = 0;
  while (infile >> c)
   cout << c << endl; // Delete this
  {
   if (c == \'(\' || c == \'{\' || c == \'[\')
   {
    stack.push(c);
   }
   if (!stack.empty() && (c == \')\' || c == \'}\' || c == \']\'))
   {
   if( (stack.top() == \'(\' && c == \')\' ) || (stack.top() == \'{\' && c == \'}\' ) || (stack.top() == \'[\' && c ==\']\'))
    {
     stack.pop();
    }
    else
    {
     x == 1;
    }
   }
   
  }
  if (stack.empty() && x == 0)
  {
   cout << \"File is balanced.\" << endl;
  }
  else
  {
   cout << \"File is not balanced.\" << endl;
  }

  {
   int z = 0;
   cin >> z;
  }
}

}

Solution

Try taking an array for char c

ie char c[i] and try if it works.

Here is an alternate code for your reference :

#include<stdlib.h>

#include<stack> //Use Standard template library to create Stack data structure

using namespace std;

/* Driver functions */

bool CheckForBalancedParenthesis(char s[]);

bool Match(char char_1, char char_2);

/* Main Method */

int main()

{

if(CheckForBalancedParenthesis(\"www.(firmcodes).com\"))

printf(\"Parenthesis are balanced \ \");

else

printf(\"Parenthesis are NOT balanced \ \");

return 0;

}

/* Return 1 Parenthesis has balanced  */

bool CheckForBalancedParenthesis(char s[])

{

/* Declare an character Stack using STL */

stack<char> Stack;

int i=0;

/* Traverse the given string or expresstion to check matching parenthesis */

while(s[i])

{

/*If the exp[i] is a starting parenthesis then push it to Stack*/

if( s[i]==\'(\' || s[i]==\'{\' || s[i]==\'[\' )

{

Stack.push(s[i]);

}

/* If exp[i] is a ending parenthesis then check for empty stack or

paranthesis matching then pop it from Stack*/

if( s[i]==\')\' || s[i]==\'}\' || s[i]==\']\' )

{

if( Stack.empty() || !Match(Stack.top(),s[i]) )

{

return false;

}

else

{

Stack.pop();

}

}

i++;

}

/*If Stack is empty then paranthesis are balanced otherwise NOT */

return Stack.empty();

}

/* Match for relevent paranthesis */

bool Match(char char_1, char char_2)

{

if( char_1==\'(\' && char_2==\')\' )

return true;

else if(char_1==\'{\' && char_2==\'}\')

return true;

else if(char_1==\'[\' && char_2==\']\')

return true;

else

return false;

}

Im trying to create a c++ program that checks to see if a given file is balanced but the code constantly says it is balanced when it is not, code: #include <
Im trying to create a c++ program that checks to see if a given file is balanced but the code constantly says it is balanced when it is not, code: #include <
Im trying to create a c++ program that checks to see if a given file is balanced but the code constantly says it is balanced when it is not, code: #include <

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site