I am just beginner in c++ so I am weak in debugging c++ codes.
I am having problem with the code below.
It is showing segmentation error and i am not able to remove in from the code. I am using g++ editor in linux.
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define max 5
int main()
{ system("clear");
char *stri;
char delims[]="/";
char *result; // to check validity of date
char *res[max]; // to store token of date
int inc=0; // used as index in res array
cout<<"enter the date\n";
cin>>stri;
cout<<"the string is:";
cout<<stri;
if((strlen(stri)>10)||(strcmp(stri,NULL)==0))
{
cout <<"invalid date format";
exit(1);
}
result=strtok(stri,delims);
cout<<"first token\n";
if((result[2]!='/0') ||(result[0]<48)||(result[0]>57)||(result[1]<48)||(result[1]>57))
{
cout<<"invalid date";
exit(1);
}
res[inc]=result;
cout<<"dd: "<<res[inc]<<"\n";
while(result !=NULL)
{
cout<<result<<"\n";
result=strtok(NULL,delims);
inc++;
res[inc]=result;
cout<<inc<<"c:"<<res[inc]<<"\n";
}
// checking validity of month
switch(res[1][0])
{
case 0:
{
if((res[1][1]<49)||(res[1][1]>57))
{ cout<<"invalid month\n";
exit(1);
}
break;
}
case 1:
{
if((res[1][1]<48)||(res[1][1]>50))
{ cout<<"invalid month\n";
exit(0);
}
break;
}
default:
cout<<"invalid month\n";
}
// checking validity of year
if(res[2][0]<48 || res[2][0]>57 || res[2][1]<48 || res[2][0]>57 || res[2][2]<48 || res[2][0]>57 || res[2][2]<48 || res[2][0]>57 )
{ cout<<"invalid year\n";
return 1;
}
std::cin.get();
return 0;
}