0

I wrote a simple REGEX to simply validate an IPv4 address. Though I am a C programmer, I have a small bit of experience with OOP and C++.

#include <iostream>
#include <regex>
#include <string>       
#include <string.h>
using namespace std;

#define REJECT  0;
#define ACCEPT  1;

bool validIP(string ipaddr){
    regex e("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
    return regex_match(ipaddr.begin(),ipaddr.end(),e);
}

int validate(string ipstring){//, char *filelist){
    if(!validIP(ipstring)){
        cerr << "Please Use a Valid IP Address" << endl;
        return REJECT;
    }else{
        cout << "Valid IP Address" << endl;
        return ACCEPT;
    }
    //will continue when correct
    char *ip=new char[ipstring.size()+1];
    ip[ipstring.size()]=0;
    memcpy(ip,ipstring.c_str(),ipstring.size());
}

int main(int argc, char **argv){    
    string test;
    cin >> test;
    validate(test);
    //cout << test;
}

Upon compiling with g++ file.cpp --std=c++11 and executing, I receive the following error message: enter image description here

The input I sent was "this is a test" and this error occurs regardless of whether or not the input string is a valid IP.

Columbo
  • 60,038
  • 8
  • 155
  • 203
Goodies
  • 4,439
  • 3
  • 31
  • 57
  • Works for me with CLion / clang on the mac. Which version of gcc are you using? – sfjac Nov 26 '14 at 17:48
  • `g++ -v` shows version 4.8.1. It's very possible that it is outdated. I actually program in C on Unix, not Windows, so I rarely touch the compilers I have. – Goodies Nov 26 '14 at 17:50
  • After checking mingw, it appears that I do, indeed, have the newest version. – Goodies Nov 26 '14 at 17:51
  • 2
    Note that your regex is not a complete validation, it will quite happily accept 333.333.333.333, etc. – sfjac Nov 26 '14 at 17:57
  • Right, that will certainly be corrected, but this was quickly written as a very basic check :) I'm more concerned with its functionality. – Goodies Nov 26 '14 at 18:11
  • 4
    Version 4.8 doesn't have full functionality of regex. From what I have seen even 4.9 is buggy. So better to use boost regex library – coder hacker Nov 26 '14 at 18:12

0 Answers0