-Need to write a program that asks the user to submit file. (already given in ma
ID: 3678277 • Letter: #
Question
-Need to write a program that asks the user to submit file. (already given in main) -
The program will then make sure all starting brackets (i.e {([])} ) have a matching end bracket in that given file.
-If brackets don't match the program will then let the user know what type of bracket it needs to be looked at and what row and column( 2 denominational array) the error is.
-It also SHOULDN'T account for brackets that are in comments or in quotations.
Was given this main as a starting point:
int main(){
string filename;
if (argc >= 2) { filename = argv[1]; // command line arguments are given. The first argument is the filename
}
else { cout << "Enter name of file to check: ";
cin >> filename;
}
cout << "File to check:" << filename << endl;
ifstream infile(filename);
if (!infile) { cout << "That file does not exist" << endl; return (-1); // end the program
}
string line;
while (getline(infile, line)) { cout << line << endl;
}
infile.close();
system("pause");
return(0); }
if need anymore info please let me know. also can you be specific on which part is .h and which is .cpp thank you.
Explanation / Answer
This is complete Running code.........
give some input file with paranthesis Expression in it and give it as command line argument
this is cpp file
****************************************************************************************
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <stack>
#include <string>
bool bracketsMatch(std::istream &in) {
std::stack<char> expectedDelimiters;
int lineNum = 0;
std::string line;
while (std::getline(in, line)) {
lineNum++;
size_t pos = 0;
while (std::string::npos != (pos = line.find_first_of("(){}[]", pos))) {
int colNum = pos + 1;
switch (line[pos]) {
case '(': expectedDelimiters.push(')'); break;
case '{': expectedDelimiters.push('}'); break;
case '[': expectedDelimiters.push(']'); break;
case ']':
case '}':
case ')':
if (expectedDelimiters.empty()) {
std::cout << "Mismatched " << line[pos]
<< " at line " << lineNum << ", col " << colNum
<< std::endl;
return false;
}
if (line[pos] != expectedDelimiters.top()) {
std::cout << "Expected " << expectedDelimiters.top()
<< ", found " << line[pos]
<< " at line " << lineNum << ", col " << colNum
<< std::endl;
return false;
}
expectedDelimiters.pop();
}
pos = colNum;
}
}
// Should check for a possible input error here, but I didn't bother.
if (!expectedDelimiters.empty()) {
std::cout << "Expected " << expectedDelimiters.top()
<< " at end of file" << std::endl;
return false;
}
return true;
}
int main(int argc, const char *argv[]) {
// The command-line parsing below is a bit sloppy (no validation,
// help message, or option handling, for example).
std::ifstream f;
std::istream &in = (argc > 1) ? (f.open(argv[1]), f) : std::cin;
if (!in) {
std::cerr << argv[0] << ": " << argv[1] << ": "
<< std::strerror(1) << std::endl;
return 2; // A rather arbitrary error code
}
return bracketsMatch(in) ? EXIT_SUCCESS : EXIT_FAILURE;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.