to modify my code to make it check if my input .cpp source files are balanced in single and double quotes
I need help to modify my code to make it check if my input .cpp source files are balanced in single and double quotes. My code below can determine whether parenthesis and curly brackets are balanced in a .cpp source file..
Here is my code:
// A small demonstration program for a stack.
#include // Provides cin, cout
#include // Provides stack
#include // Provides string
#include //Creates and/or read files
using namespace std;
// PROTOTYPE for a function used by this demonstration program
bool balancedSymbols(const string& sym);
// Postcondition: A true return value indicates that the parentheses in the
// given expression are balanced. Otherwise, the return value is false.
int main()
{
string userInput;
cout << "Enter a .cpp source file to check if symbols are balanced\n";
getline(cin, userInput);
if (balancedSymbols(userInput))
cout << "The parentheses and curly brackets in the source code are balanced.\n";
else
cout << "The parentheses and curly brackets in the source code are not balanced.\n";
return 0;
}
bool balancedSymbols(const string& fileName)
// Library facilities used: stack, string
{
string symbols; //to store current line
ifstream inFile(fileName);
stack ch; // Stack to store the left parentheses as they occur
bool flag = false; // Becomes true if a needed parenthesis is not found
while (getline(inFile,symbols)) {
for (int i = 0; !flag && (i < symbols.length()); ++i)
{
if (symbols[i] == '(')
ch.push(symbols[i]);
else if (symbols[i] == '{')
ch.push(symbols[i]);
else if ((symbols[i] == ')')) {
// if right paranthesis encountered, match with left paranthesis at top
if (ch.empty())
flag = true;
else if (ch.top() == '(')
ch.pop(); // Pops the corresponding left parenthesis.
else {
flag = true; // if the stack top has other type of paranthesis
}
}
else if ((symbols[i] == '}')) {
// if right paranthesis encountered, match with left paranthesis at top
if (ch.empty())
flag = true;
else if (ch.top() == '{')
ch.pop(); // Pops the corresponding left parenthesis.
else {
flag = true; // if the stack top has other type of paranthesis
}
}
}
}
inFile.close();
return (ch.empty() && !flag);
}
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 2 steps with 3 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)