to modify my code to make it check if my input .cpp source files are balanced in single and double quotes

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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);
}

Expert Solution
steps

Step by step

Solved in 2 steps with 3 images

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY