segmentation problem

I am having problem with the code below. I am usin g++ in linux to compile it. It is showing segmentation error for the input in the form 12/10/1199
I am not able to understand how to remove it.
Please If someone can help with it.


#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;
}
You need to make some general fixes to the code.
You need to get rid of the macro and you should replace the char arrays/strcmp/etc. with std::string. Instead of using magic numbers, you should use the proper char constants. '/0' does not exist, you probably meant '\0'.
Your problems will most likely have taken care of themselves after applying these fixes.

Also, when posting code you should always use code tags and when you have a segmentation fault, you always should specify the line that caused it.
Topic archived. No new replies allowed.