Here is a program that reads the number of open and closed brackets. Everything works, before spaces appear in the string. How to solve?

#include <iostream> #include <stdio.h> using namespace std; int main(int argc, char** argv) { char str[80]; cout << "Enter string: "; cin >> str; int numberRound = 0; int numberSquare = 0; int i; for (i = 0; i < 80; i++) { if (str[i] == '(') { numberRound++; } if (str[i] == ')') { numberRound--; } if (str[i] == '[') { numberSquare++; } if (str[i] == ']') { numberSquare--; } } if (numberRound == 0) { cout << "Round skobok porovny\n"; } else cout << "Round Skobok ne porovny\n"; if (numberSquare == 0) { cout << "Square skobok porovny"; } else cout << "Square skobok ne porovny"; return 0; } 
  • What happens when spaces appear there? - arrowd Nov.
  • Give an example of the line on which problems arise, and which ones? I see no problems so far. - PinkTux
  • Does the program have to consider equal parentheses or are they balanced? - KoVadim
  • @arrowd then the number of brackets equals zero, that is, after it encounters a space, it ceases to count braces - Dasha Novikova
  • @PinkTux in any line, as soon as it encounters a space, stops counting the staples in the loop. - Dasha Novikova

3 answers 3

Your problem is that you read one word with >> .

Use the getline function.

And instead of the ladder if 's use switch .

And also - here

 for (i=0; i < 80; i++) { 

it is better to check the end of the line (you do not always enter something long!):

 for (i=0; str[i]; i++) { 

In general, here is the corrected code:

 int main(int argc, char** argv) { char str[80]; cout << "Enter string: "; cin.getline(str,80); int numberRound = 0; int numberSquare = 0; int i; for (i=0; str[i]; i++) { switch(str[i]) { case '(': numberRound++; break; case ')': numberRound--; break; case '[': numberSquare++; break; case ']': numberSquare--; break; } } if (numberRound == 0) { cout << "Round skobok porovny\n"; }else cout << "Round Skobok ne porovny\n"; if (numberSquare == 0) { cout << "Square skobok porovny"; } else cout << "Square skobok ne porovny"; return 0; } 
  • did not understand) why the characters are not brackets?)) - Dasha Novikova
  • Where? However, it does not matter. See how it is correct ... - Harry
  • Well, if you are satisfied with the answer - mark it as accepted :) - Harry

At a minimum, the condition for terminating a cycle is incorrect. If the length of the entered string is less than 80 characters, you begin to cheat garbage data. It would be better to:

 for( i = 0; str[i]; i++ ) { /* ... */ } 
  • Yes, but it does not solve the problem) - Dasha Novikova
  • @DashaNovikova, the second problem has already been described above. Before the calculation output the value of str and see everything. And even better - learn to use step-by-step debugging, such questions will go away. - PinkTux
 cin >> str; 
 gets(str); 
 scanf("%79[^\r\n]", str); 
 for (i=0; i < 80; i++) { 
 for (i=0; str[i]; i++) { 
  • Well what are you advising - gets ... :( - Harry
  • one
    @Harry, what is wrong with gets? - Grundy
  • @Grundy How to put it mildly ... He does not check the length of the input. To paraphrase a Coward, "Not strictly recommended for use even in preschool institutions" :) - Harry
  • @Harry, cin >> str doesn't check either. So what? - Qwertiy
  • @Qwertiy And you can't use it like that either, of course ... - Harry