You are on page 1of 2

In C++, a pair of braces is used to group related code together as a block. The braces must be balanced, i.e.

, open brace must be followed by close brace otherwise the code does not compile. You are to write a C++ program to help check if the braces in a cpp file is balanced. The program reads from a file, re-position the brace to its own line, and number the braces according to physical level. The first opening brace is numbered as S1 (start 1); if followed by another opening brace then thats S2; if it follows with two close braces, then they should be numbered as E2 and E1, as shown in the example below: S1{ cout << within the first block; if(somecondtion) S2{ //some code E2} E1} The brace checked cpp file should go to an output file, and also be displayed to the screen. Screen output should display 20 lines first, and then pause for a key press to continue. When the entire file is checked, a summary of number of start / end braces and whether or not the braces are balanced should be displayed. Rules of brace checking: Only the braces that is used to group C++ code needs be re-positioned and numbered, the braces that are part of a string literal, comments, should not be considered. To be more specific, the braces in the following scenarios should be ignored and left as is: braces encountered within a comment either /* or // o //this is a brace { o /* brace within comments {}*/ braces that follow a backslash '\{' braces that follow quotes, either single or double, { or {

Note:

Error handling the program should check whether input and output files are opened successfully. If not, user is prompted whether to re-enter or not. The answer to re-enter or not must be y or n, otherwise it keeps on prompting. The program proceeds to check the file when it is opened, or terminates if user chooses not to re-enter.

Test your program by adding brace(s) in comments, both in // and /* */, and make sure it still works correctly. Use function(s) to organize your code. Remember the purpose of using function is to avoid redundant code and help code readability. Design your program so that functions do serve these purposes.

You might also like