i have written my code and feel exhausted. when my code finds in error on a given input text file, it does a great job in finding the mismatched symbols. what can i add to show there is no error when no error is found. i attempted adding another else statement but that did not work.
any help would be appreciated. Thanks
here is my code:
#include <fstream>
#include <string>
#include <iostream>
#include <stack>
using namespace std;
int main()
{
int count=0; // counter initialized to zero
string line; //string to keep count of total lines
stack<char> sym; //symbol declarator
ifstream Data; //reads the documents and the files
// opens document to be read
Data.open("input 1.txt");
if(!Data)
{
cout<<"There must be a mistake! File does not exist!"<<endl;
return 1;
}
getline(Data, line);
while (Data)
{
count++;
for(int x=0; x<static_cast<int>(line.length()); x++)
{
if(line[x]=='('||line[x]=='{'||line[x]=='[')
sym.push(line[x]);
else if(line[x]==')')
{
if(sym.top()=='(')
sym.pop();
else
{
cout<<"There is a mistake! Symbols DO NOT match!!"
<<sym.top()<<" ) in line " <<count<<endl;
return 0;
}
}
else if(line[x]=='}')
{
if(sym.top()=='{')
sym.pop();
else
{
cout<<"There is a mistake! Symbols DO NOT match!!"
<<sym.top()<<" } in line " <<count<<endl;
return 0;
}
}
else if(line[x]==']')
{
if(sym.top()==']')
sym.pop();
else
{
cout<<"There is a mistake! Symbols DO NOT match!!"
<<sym.top()<<" ] in line " <<count<<endl;
return 0;
}
}
}
getline(Data, line);
}
return 0;
}
when the user inputs the following for example:
int func2(int m, int n) {
if (n == 0)
return 0;
else
return m + func2(m, n-1};
}
the program outputs: There is a mistake! Symbols DO NOT match!! in line 5.
this is fine but when there is no mistake i would like for it to display there is no mistake. how could i implement that?