W
W
William032018-12-03 17:33:55
C++ / C#
William03, 2018-12-03 17:33:55

What is the error, Visual Studio 2017?

I just installed Microsoft Visual Studio 2017. At first there was a problem - I couldn't find the project.exe file. It turned out that the problem is with the SDK. Installed a new one, everything seems to be fine. Program:

#include <iostream>
using namespace std;

int main() {
        cout << "Why don't you work?!!";
        return 0;

It works and compiles. But after I copy the code from another program:
/* Program 03.12.2018 */

#include <iostream>
#include <string>
#include <stack>
using namespace std;

bool IsCorrectSequence (const string& sq) {
  stack <char> seq;
  char v;
  for (const char& c: sq) {
    v = seq.top();
    if ((c == ')' && v == '(') ||
      (c == ']' && v == '[') ||
      (c == '}' && v == '{')) {seq.pop();}
    else if (c == ')' || c == ']' || c == '}') {return false;}
    else {seq.push(c);}
  }
  return true;
}
  


int main() {
  string s;
  cin >> s;
  cout << "S is " << (IsCorrectSequence(s) ? "correct sequence" : "not correct sequence") << endl;
  return 0;
}

Everything collapses:
5c053ddd4bea9919884829.png
PS The program solves the "Correct Bracket Sequence" problem:
A correct bracket sequence is a sequence consisting of characters -
"brackets", in which each opening bracket corresponds to a closing bracket of the same
type as the opening bracket. For example, the following sequences are correct:
[([])(((])))]{()}, ()((())).
Bracket sequences [[)) (the type of closing brackets does not match the type of opening brackets), }{ (the closing bracket comes
before the opening bracket), [[{{}}] (not every opening bracket has a closing bracket) will not be correct .
Is this bracket sequence correct?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
G
GavriKos, 2018-12-03
@William03

Well, you have logical errors. seq is empty and you are making it top - trying to take the top element. According to the documentation for the function, this leads to undefined behavior.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question