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?

How about a boolean variable initialized to false? When a mistake occurs, set it to true and display the error message. If the boolean variable is still false at the end after everything is read in, then that means there were no errors. If that's the case, display a message saying that there were no errors. Don't position the test of the boolean variable as an else statement inside the while loop. Instead, put an if statement testing that boolean variable right before return 0 .

Use a simple and clear loop

while (getline(Data,line)) {
   ...
}

instead of clumsy and redundant

getline(Data,line);
while (Data) {
   ...
   getline(Data,line);
}
This article has been dead for over six months. Start a new discussion instead.