I l@ve RuBoard |
![]() ![]() |
7.7 DebuggingFirst you inspect the program to see if you can detect the error. In such a small program it is not difficult to spot the mistake. However, let's assume that instead of a 21-line program, you have a much larger one containing 5,000,000 lines. Such a program would make inspection more difficult, so you need to proceed to the next step. Most systems have C++ debugging programs, but each debugger is different. Some systems have no debugger. In that case you must resort to a diagnostic print statement. (More advanced debugging techniques are discussed in Chapter 17.) The technique is simple: put a std::cout where you're sure the data is good (just to make sure it really is good). Then put a std::cout where the data is bad. Run the program and keep putting in std::cout statements until you isolate the area in the program that contains the mistake. The program, with diagnostic std::cout lines added, looks like: std::cout << "Enter oper_char and number: "; std::cin >> value; std::cin >> oper_char; std::cout << "## after cin " << oper_char << '\n'; if (oper_char = '+') { std::cout << "## after if " << oper_char << '\n'; result += value;
Running the program again results in: Result: 0 Enter operator and number: + 123 ## after cin + ## after if + Result: 123 Enter operator and number: + 52 ## after cin + ## after if + Result: 175 Enter operator and number: x 37 ## after cin x ## after if + Result: 212 From this you see that something is going wrong with the if statement. Somehow the variable operator is an x going in and a + coming out. Closer inspection reveals that you have the old mistake of using = instead of ==. After you fix this bug, the program runs correctly. Building on this working foundation, you add in the code for the other operators, -, *, and /, to create Example 7-6. Example 7-6. calc3/calc3.cpp#include <iostream> int result; // the result of the calculations char oper_char; // operator the user specified int value; // value specified after the operator int main( ) { result = 0; // initialize the result // loop forever (or until break reached) while (true) { std::cout << "Result: " << result << '\n'; std::cout << "Enter operator and number: "; std::cin >> oper_char >> value; if ((oper_char == 'q') || (oper_char == 'Q')) break; if (oper_char == '+') { result += value; } else if (oper_char == '-') { result -= value; } else if (oper_char == '*') { result *= value; } else if (oper_char == '/') { if (value == 0) { std::cout << "Error:Divide by zero\n"; std::cout << " operation ignored\n"; } else result /= value; } else { std::cout << "Unknown operator " << oper_char << '\n'; } } return (0); } You expand the test plan to include the new operators and try it again: + 123 Result should be 123 + 52 Result should be 175 x 37 Error message should be output - 175 Result should be zero + 10 Result should be 10 / 5 Result should be 2 / 0 Divide by zero error * 8 Result should be 16 q Program should exit Testing the program, you find much to your surprise that it works. The word "Preliminary" is removed from the specification and the program, test plan, and specification are released. |
I l@ve RuBoard |
![]() ![]() |