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:
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.