// // Matching parentheses: tell if parens don't match, and give position in // file if the problem is an extra right paren. "count" keeps track of how // deeply nested we currently are. // #include const char LEFT = '('; const char RIGHT = ')'; int main(void) { int count = 0, position=0; char letter; cin.unsetf(ios::skipws); // let's not skip whitespace while (cin >> letter) { // expression evaluates to 0 on EOF position = position + 1; if (letter == LEFT) ++count; if (letter == RIGHT) --count; if (count < 0) { cout << "extra right parenthesis at position " << position << endl; return 0; } } if (count > 0) cout << "too many left parentheses" << endl; else cout << "OK." << endl; return 0; }