Search code examples
c++regexgccg++mingw32

c++ program crash when using regex


my code after compile crash when reaches to regex part:

I want check is any number exists in received string or no.

#include <iostream>
#include <regex>
using namespace std;

int main()
{
    int in, count, rate;
    char *w;
    cin >> count;

    for(in = 1; in < 5; in++) {
        rate = 0;
        cin >> w;
        cout << "Case #";
        cout << in;
        cout << ":";

        if (regex_match (std::string(w), regex("([0-9])")))
            ++rate;
        cout << rate;
        cout << endl;
    }
    return 0;
}

Solution

  • You are using a pointer with no allocated memory. This will crash your program. Just declare it as a string, and try to avoid naked pointers:

    std::string w;