I have to write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. I have to use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. I have to return the line that has the error.
#include<iostream>
#include<string>
#include<stack>
#include<fstream>
usingnamespace std;
int checkBalance(char expression[]);
int main()
{
int checkResult;
char code[500];
string fileName = "input1.txt";
char x;
ifstream inFile;
inFile.open(fileName);
inFile >> x;
while (!inFile.eof())
{
for (int i = 0; i < 500; i++)
{
code[i] = x;
inFile >> x;
}
}
checkResult = checkBalance(code);
if (checkResult == 0)
cout << "There are no errors" << endl;
else
cout << "The error is on line: " << checkResult << endl;
return 0;
}
int checkBalance(char expression[])
{
stack<char> s;
char a, b, c;
int count = 1;
while (count > 0)
{
for (int i = 0; i < strlen(expression); i++)
{
if (expression[i] == '(' || expression[i] == '{' || expression[i] == '[')
{
s.push(expression[i]);
}
else
{
switch (expression[i])
{
case')':
a = s.top();
s.pop();
if (a == '{' || a == '[')
return count;
break;
case'}':
b = s.top();
s.pop();
if (b == '(' || b == '[')
return count;
break;
case']':
c = s.top();
s.pop();
if (c == '(' || c == '{')
return count;
break;
}
}
}
count++;
}
return 0;
}
It's compiling without any errors but when I try to run it, I get no output. I don't get any errors, but I don't get an output either. I don't understand what's wrong
inFile >> x;
while (!inFile.eof())
{
for (int i = 0; i < 500; i++)
{
code[i] = x;
inFile >> x;
}
}
if your text has more than 500 characters you'll overwrite the beginning of `code'
if it has less, you'll reach an invalid state but keep writing to the end.
regardless, you do not put the terminator character '\0' on the c-string, so you can't use it on `strlen()'
1 2 3 4 5 6
int size = 0;
while(input>>x and size<500-1){ //more characters will be discarded
code[size] = x;
++size;
}
code[size] = '\0';
you also said «I have to return the line that has the error» but I don't see where you separate between lines.
now, in `checkBalance()'
1 2 3 4 5
int count = 1;
while (count > 0){
//...
count++;
}
¿when will that end?
and another thing
1 2
//case ')', ']', '}'
a = s.top(); //¿what if `s' is empty?
I don't see a way to get the lines without a lot of trouble (and avoiding the stack). You can do it inefficiently by reading ahead, but that isn't very clean. If you don't allow the symbols to span lines, its simple, but assuming they can split lines, it a bit of a pain.
do you need to make a little struct with character type and line #s? Then whats left on the stack is mismatched at that line?
#include <iostream>
#include <sstream>
#include <string>
usingnamespace std;
//======================================================================
int checkBracket( istream &in )
{
const string bracket = "({[";
const string tekcarb = ")}]";
string state, line;
int numLines = 0;
while( getline( in, line ) )
{
numLines++;
for ( char c : line )
{
if ( bracket.find( c ) != string::npos ) state += c;
int pos = tekcarb.find( c );
if ( pos != string::npos )
{
if ( state == "" || state.back() != bracket[pos] ) return numLines;
state.pop_back();
}
}
}
return state == "" ? 0 : numLines;
}
//======================================================================
int main()
{
stringstream in( "(ab(cd))=\n""a{b[d]b)a\n""E[[[d ]]]\n" );
int L = checkBracket( in );
if ( L ) cout << "Error in line " << L << '\n';
else cout << "All's well with the world.\n";
}
oh. If you just want to know if they balance, you have an integer for each symbol pair. there are only like 5 total in most languages/math/etc type text files.
paren ()
bracket {}
square []
angle <>
so those integers, find one ++, find mate --, at the end check if zero to see if it balanced.
But to find the line number of the problem is more work. You can't do that with just an integer. You can across 1 line, but not across a file. At that point a stack or other tracking system is needed to actually find the match (rather than just total up ).
correct. They balance, but are incorrect in syntax. You can't check syntax with just an integer, for sure.
and when you get to a real language checker, you can't do it with just a stack.
you get junk like
string s = "blah blah \" blah" //no stack or anything else will tell you this one: you have to actually parse it and 'understand' the escaped quote vs normal quotes.