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;
}

Recommended Answers

All 5 Replies

Try compiling like this

g++ filename.cpp -Wall -ansi -pedantic -o filename

It will point to some very suspicious areas in your code.

21: warning: null argument where non-null required (argument 2)
21: warning: null argument where non-null required (argument 2)
18: warning: ‘stri’ is used uninitialized in this function

The last one is probably the problem....Please use the code tags.

Actually, even though it really should be addressed, the warning itself isn't a serious problem. It certainly could be, that's why there's a warning generated, but the value is input a couple lines later. That variable is part of the problem though.

It is declared as a pointer to char, not an array of char. As a result, you can only legally hold one (1) char in the object it points to, not a C-style string. Additionally, because it can only hold one char, the use of strlen() will produce unpredictable results due to there being no NULL terminator in the array, because there is no array.

Actually, even though it really should be addressed, the warning itself isn't a serious problem. It certainly could be, that's why there's a warning generated, but the value is input a couple lines later. That variable is part of the problem though.

It is declared as a pointer to char, not an array of char. As a result, you can only legally hold one (1) char in the object it points to, not a C-style string. Additionally, because it can only hold one char, the use of strlen() will produce unpredictable results due to there being no NULL terminator in the array, because there is no array.

***********************************************************************************
But when i used to work on turbo c++, i used the same commands (char *strn)to declare array of characters.... n then used to take string from the console using cin command
there it didnt show any error.....
n even after taking the variable as array of char it is showing problem

>>But when i used to work on turbo c++, i used the same commands (char *strn)to declare array of characters.... n then used to take string from the console using cin command there it didnt show any error.....
Turbo C++ is a very old pre-standard compiler. That must have been a compiler extension. Those commands are trying to use a dynamic array. There really isn't any such thing in C++ unless you go to a vector, write your own container class, or use dynamic allocation manually.

>>n even after taking the variable as array of char it is showing problem
What was the problem? I suspect it was breaking up your input strings into multiple strings at the 'whitespace' characters.

Something like this should work though:

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

const int maxString = 81;

int main() {
  char strn[maxString] = {'\0'};

  cout << "Enter a string.";
  cin.getline(strn, maxString);

  cout << "You entered: " << strn << endl;

  cin.get();
  return 0;
}

Actually, even though it really should be addressed, the warning itself isn't a serious problem. It certainly could be, that's why there's a warning generated, but the value is input a couple lines later. That variable is part of the problem though.

It is declared as a pointer to char, not an array of char. As a result, you can only legally hold one (1) char in the object it points to, not a C-style string. Additionally, because it can only hold one char, the use of strlen() will produce unpredictable results due to there being no NULL terminator in the array, because there is no array.

I disagree with your first assessment.

char *stri;

never has any memory assigned or allocated that's why I homed in on it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.