C++ : Process died on signal 6










-4















i have a project of code in which i have compiled it and run it but it seems that it just return an error message like



This is my data txt file



Error Message



Can anyone help me?



#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>
#include <stdlib.h>
#include <sstream>
using namespace std;

struct course

string CourseID;
string CourseName;
int CourseSemester;
int CourseSKS;
c;
void printMenu_course();
void printReport_course(vector <course> x, int sem);
vector <course> readData_course(int sem);
void splitData(string temp1, string temp2[4]);
int ccounter;
int sem;
int main()

printMenu_course();
return 0;


void printMenu_course()

void splitData(string temp1, string temp2[4])

int x=0;
string mark = ",";
size_t pos = 0;
while ((pos = temp1.find(mark)) != string::npos )

temp2[x] = temp1.substr(0,pos);
x++;
temp1.erase(0, pos + mark.length());
temp2[x] = temp1;



vector <course> readData_course(int sem)

vector <course> x;
ccounter = 0;
string baris1, baris2[4];
ifstream courFile;
courFile.open("course.txt", ios::in);
while(!courFile.eof())

courFile >> baris1;
splitData(baris1,baris2);
x.push_back(course());
x[ccounter].CourseID = baris2[0];
x[ccounter].CourseName = baris2[1];
x[ccounter].CourseSemester = ::atof(baris2[2].c_str());
x[ccounter].CourseSKS = ::atof(baris2[3].c_str());
ccounter++;
courFile.close();
return x;


void printReport_course(vector<course> x, int sem)

int num = 0;
cout << " COURSE DATA n";
cout << "Semester " << sem << endl;
cout << setw(5) << "No.";
cout << setw(5) << "Course ID";
cout << setw(25) << "Course Name";
cout << setw(5) << "Course SKS n";
for(int i =0;i<ccounter;i++)

if(x[i].CourseSemester == sem)

cout << setw(5) << ++num;
cout << setw(5) << x[i].CourseID;
cout << setw(25) << x[i].CourseName;
cout << setw(5) << x[i].CourseSKS << "n";






when i change the vector to only temp2[3] and baris2[3] it can run without any error but if it is temp2[4] and baris2[4] it shows an error like that again.










share|improve this question






















  • stackoverflow.com/questions/5605125/…

    – Neil Butterworth
    Nov 13 '18 at 18:07











  • Attaching the input file as an image is a bad idea. There are many reasons why we might not be able to see the file and of those who can see it few are going to be interested enough to retype it. The harder you make things the less likely you will receive a favorable result. Plus it raises the question, "Why the heck would you embed a picture of text in a text-based medium?"

    – user4581301
    Nov 13 '18 at 18:15











  • There is a simpler way to write the parser. Take inspiration from option 2 of this answer.

    – user4581301
    Nov 13 '18 at 18:20











  • Having a global ccounter to track the vector length is dangerous and unnecessary. You can get the number of elements in a vector x with x.size() and the last element (the one just push_backed) with x.back()

    – user10605163
    Nov 13 '18 at 18:27











  • Signal 6 is SIGABRT. This may be relevant: stackoverflow.com/questions/3413166/…

    – Jesper Juhl
    Nov 13 '18 at 18:43















-4















i have a project of code in which i have compiled it and run it but it seems that it just return an error message like



This is my data txt file



Error Message



Can anyone help me?



#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>
#include <stdlib.h>
#include <sstream>
using namespace std;

struct course

string CourseID;
string CourseName;
int CourseSemester;
int CourseSKS;
c;
void printMenu_course();
void printReport_course(vector <course> x, int sem);
vector <course> readData_course(int sem);
void splitData(string temp1, string temp2[4]);
int ccounter;
int sem;
int main()

printMenu_course();
return 0;


void printMenu_course()

void splitData(string temp1, string temp2[4])

int x=0;
string mark = ",";
size_t pos = 0;
while ((pos = temp1.find(mark)) != string::npos )

temp2[x] = temp1.substr(0,pos);
x++;
temp1.erase(0, pos + mark.length());
temp2[x] = temp1;



vector <course> readData_course(int sem)

vector <course> x;
ccounter = 0;
string baris1, baris2[4];
ifstream courFile;
courFile.open("course.txt", ios::in);
while(!courFile.eof())

courFile >> baris1;
splitData(baris1,baris2);
x.push_back(course());
x[ccounter].CourseID = baris2[0];
x[ccounter].CourseName = baris2[1];
x[ccounter].CourseSemester = ::atof(baris2[2].c_str());
x[ccounter].CourseSKS = ::atof(baris2[3].c_str());
ccounter++;
courFile.close();
return x;


void printReport_course(vector<course> x, int sem)

int num = 0;
cout << " COURSE DATA n";
cout << "Semester " << sem << endl;
cout << setw(5) << "No.";
cout << setw(5) << "Course ID";
cout << setw(25) << "Course Name";
cout << setw(5) << "Course SKS n";
for(int i =0;i<ccounter;i++)

if(x[i].CourseSemester == sem)

cout << setw(5) << ++num;
cout << setw(5) << x[i].CourseID;
cout << setw(25) << x[i].CourseName;
cout << setw(5) << x[i].CourseSKS << "n";






when i change the vector to only temp2[3] and baris2[3] it can run without any error but if it is temp2[4] and baris2[4] it shows an error like that again.










share|improve this question






















  • stackoverflow.com/questions/5605125/…

    – Neil Butterworth
    Nov 13 '18 at 18:07











  • Attaching the input file as an image is a bad idea. There are many reasons why we might not be able to see the file and of those who can see it few are going to be interested enough to retype it. The harder you make things the less likely you will receive a favorable result. Plus it raises the question, "Why the heck would you embed a picture of text in a text-based medium?"

    – user4581301
    Nov 13 '18 at 18:15











  • There is a simpler way to write the parser. Take inspiration from option 2 of this answer.

    – user4581301
    Nov 13 '18 at 18:20











  • Having a global ccounter to track the vector length is dangerous and unnecessary. You can get the number of elements in a vector x with x.size() and the last element (the one just push_backed) with x.back()

    – user10605163
    Nov 13 '18 at 18:27











  • Signal 6 is SIGABRT. This may be relevant: stackoverflow.com/questions/3413166/…

    – Jesper Juhl
    Nov 13 '18 at 18:43













-4












-4








-4








i have a project of code in which i have compiled it and run it but it seems that it just return an error message like



This is my data txt file



Error Message



Can anyone help me?



#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>
#include <stdlib.h>
#include <sstream>
using namespace std;

struct course

string CourseID;
string CourseName;
int CourseSemester;
int CourseSKS;
c;
void printMenu_course();
void printReport_course(vector <course> x, int sem);
vector <course> readData_course(int sem);
void splitData(string temp1, string temp2[4]);
int ccounter;
int sem;
int main()

printMenu_course();
return 0;


void printMenu_course()

void splitData(string temp1, string temp2[4])

int x=0;
string mark = ",";
size_t pos = 0;
while ((pos = temp1.find(mark)) != string::npos )

temp2[x] = temp1.substr(0,pos);
x++;
temp1.erase(0, pos + mark.length());
temp2[x] = temp1;



vector <course> readData_course(int sem)

vector <course> x;
ccounter = 0;
string baris1, baris2[4];
ifstream courFile;
courFile.open("course.txt", ios::in);
while(!courFile.eof())

courFile >> baris1;
splitData(baris1,baris2);
x.push_back(course());
x[ccounter].CourseID = baris2[0];
x[ccounter].CourseName = baris2[1];
x[ccounter].CourseSemester = ::atof(baris2[2].c_str());
x[ccounter].CourseSKS = ::atof(baris2[3].c_str());
ccounter++;
courFile.close();
return x;


void printReport_course(vector<course> x, int sem)

int num = 0;
cout << " COURSE DATA n";
cout << "Semester " << sem << endl;
cout << setw(5) << "No.";
cout << setw(5) << "Course ID";
cout << setw(25) << "Course Name";
cout << setw(5) << "Course SKS n";
for(int i =0;i<ccounter;i++)

if(x[i].CourseSemester == sem)

cout << setw(5) << ++num;
cout << setw(5) << x[i].CourseID;
cout << setw(25) << x[i].CourseName;
cout << setw(5) << x[i].CourseSKS << "n";






when i change the vector to only temp2[3] and baris2[3] it can run without any error but if it is temp2[4] and baris2[4] it shows an error like that again.










share|improve this question














i have a project of code in which i have compiled it and run it but it seems that it just return an error message like



This is my data txt file



Error Message



Can anyone help me?



#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>
#include <stdlib.h>
#include <sstream>
using namespace std;

struct course

string CourseID;
string CourseName;
int CourseSemester;
int CourseSKS;
c;
void printMenu_course();
void printReport_course(vector <course> x, int sem);
vector <course> readData_course(int sem);
void splitData(string temp1, string temp2[4]);
int ccounter;
int sem;
int main()

printMenu_course();
return 0;


void printMenu_course()

void splitData(string temp1, string temp2[4])

int x=0;
string mark = ",";
size_t pos = 0;
while ((pos = temp1.find(mark)) != string::npos )

temp2[x] = temp1.substr(0,pos);
x++;
temp1.erase(0, pos + mark.length());
temp2[x] = temp1;



vector <course> readData_course(int sem)

vector <course> x;
ccounter = 0;
string baris1, baris2[4];
ifstream courFile;
courFile.open("course.txt", ios::in);
while(!courFile.eof())

courFile >> baris1;
splitData(baris1,baris2);
x.push_back(course());
x[ccounter].CourseID = baris2[0];
x[ccounter].CourseName = baris2[1];
x[ccounter].CourseSemester = ::atof(baris2[2].c_str());
x[ccounter].CourseSKS = ::atof(baris2[3].c_str());
ccounter++;
courFile.close();
return x;


void printReport_course(vector<course> x, int sem)

int num = 0;
cout << " COURSE DATA n";
cout << "Semester " << sem << endl;
cout << setw(5) << "No.";
cout << setw(5) << "Course ID";
cout << setw(25) << "Course Name";
cout << setw(5) << "Course SKS n";
for(int i =0;i<ccounter;i++)

if(x[i].CourseSemester == sem)

cout << setw(5) << ++num;
cout << setw(5) << x[i].CourseID;
cout << setw(25) << x[i].CourseName;
cout << setw(5) << x[i].CourseSKS << "n";






when i change the vector to only temp2[3] and baris2[3] it can run without any error but if it is temp2[4] and baris2[4] it shows an error like that again.







c++






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 18:05









WeerBeezWeerBeez

1




1












  • stackoverflow.com/questions/5605125/…

    – Neil Butterworth
    Nov 13 '18 at 18:07











  • Attaching the input file as an image is a bad idea. There are many reasons why we might not be able to see the file and of those who can see it few are going to be interested enough to retype it. The harder you make things the less likely you will receive a favorable result. Plus it raises the question, "Why the heck would you embed a picture of text in a text-based medium?"

    – user4581301
    Nov 13 '18 at 18:15











  • There is a simpler way to write the parser. Take inspiration from option 2 of this answer.

    – user4581301
    Nov 13 '18 at 18:20











  • Having a global ccounter to track the vector length is dangerous and unnecessary. You can get the number of elements in a vector x with x.size() and the last element (the one just push_backed) with x.back()

    – user10605163
    Nov 13 '18 at 18:27











  • Signal 6 is SIGABRT. This may be relevant: stackoverflow.com/questions/3413166/…

    – Jesper Juhl
    Nov 13 '18 at 18:43

















  • stackoverflow.com/questions/5605125/…

    – Neil Butterworth
    Nov 13 '18 at 18:07











  • Attaching the input file as an image is a bad idea. There are many reasons why we might not be able to see the file and of those who can see it few are going to be interested enough to retype it. The harder you make things the less likely you will receive a favorable result. Plus it raises the question, "Why the heck would you embed a picture of text in a text-based medium?"

    – user4581301
    Nov 13 '18 at 18:15











  • There is a simpler way to write the parser. Take inspiration from option 2 of this answer.

    – user4581301
    Nov 13 '18 at 18:20











  • Having a global ccounter to track the vector length is dangerous and unnecessary. You can get the number of elements in a vector x with x.size() and the last element (the one just push_backed) with x.back()

    – user10605163
    Nov 13 '18 at 18:27











  • Signal 6 is SIGABRT. This may be relevant: stackoverflow.com/questions/3413166/…

    – Jesper Juhl
    Nov 13 '18 at 18:43
















stackoverflow.com/questions/5605125/…

– Neil Butterworth
Nov 13 '18 at 18:07





stackoverflow.com/questions/5605125/…

– Neil Butterworth
Nov 13 '18 at 18:07













Attaching the input file as an image is a bad idea. There are many reasons why we might not be able to see the file and of those who can see it few are going to be interested enough to retype it. The harder you make things the less likely you will receive a favorable result. Plus it raises the question, "Why the heck would you embed a picture of text in a text-based medium?"

– user4581301
Nov 13 '18 at 18:15





Attaching the input file as an image is a bad idea. There are many reasons why we might not be able to see the file and of those who can see it few are going to be interested enough to retype it. The harder you make things the less likely you will receive a favorable result. Plus it raises the question, "Why the heck would you embed a picture of text in a text-based medium?"

– user4581301
Nov 13 '18 at 18:15













There is a simpler way to write the parser. Take inspiration from option 2 of this answer.

– user4581301
Nov 13 '18 at 18:20





There is a simpler way to write the parser. Take inspiration from option 2 of this answer.

– user4581301
Nov 13 '18 at 18:20













Having a global ccounter to track the vector length is dangerous and unnecessary. You can get the number of elements in a vector x with x.size() and the last element (the one just push_backed) with x.back()

– user10605163
Nov 13 '18 at 18:27





Having a global ccounter to track the vector length is dangerous and unnecessary. You can get the number of elements in a vector x with x.size() and the last element (the one just push_backed) with x.back()

– user10605163
Nov 13 '18 at 18:27













Signal 6 is SIGABRT. This may be relevant: stackoverflow.com/questions/3413166/…

– Jesper Juhl
Nov 13 '18 at 18:43





Signal 6 is SIGABRT. This may be relevant: stackoverflow.com/questions/3413166/…

– Jesper Juhl
Nov 13 '18 at 18:43












1 Answer
1






active

oldest

votes


















0














Reducing your program to the minimum needed to demonstrate the error and adding a couple debug statements



#include <iostream>
#include <string>
using namespace std;

void splitData(string temp1, string temp2[4])

int x = 0;
string mark = ",";
size_t pos = 0;
while ((pos = temp1.find(mark)) != string::npos)

temp2[x] = temp1.substr(0, pos);
cout << temp2[x] << ": ";
x++;
temp1.erase(0, pos + mark.length());
cout << x << ": " << temp1 <<"n";

if (x >= 4)

cout << "Accessing temp2[x] with x = " << x << " when temp2 contains 4 elementsn";

temp2[x] = temp1;


int main()

string temp2[4];
splitData("A,B,C,D,", temp2);



we get the following output:



A: 1: B,C,D,
B: 2: C,D,
C: 3: D,
D: 4:
Accessing temp2[x] with x = 4 when temp2 contains 4 elements


Which clearly shows x is allowed to get out of range. Why? Count the number of items in



"A,B,C,D,"
1 2 3 4 5


There is an empty fifth element. Luc Besson would not approve.



Solution



Remove the trailing comma in the input file.



Better Solution



Use a vector so that the number of elements doesn't matter.



#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector<string> splitData(string temp1)

vector<string> tokens;
string mark = ",";
size_t pos = 0;
while ((pos = temp1.find(mark)) != string::npos)

tokens.push_back(temp1.substr(0, pos));
temp1.erase(0, pos + mark.length());

tokens.push_back(temp1);
return tokens;


int main()

vector<string> tokens = splitData("A,B,C,D,");
cout << "Number of tokens: " << tokens.size() << 'n';

for (const string & token: tokens)

cout << token << 'n';

cout << "Done.n";



Output:



Number of tokens: 5
A
B
C
D

Done.


Unrelated comments



The naming of most of the variables is bad. string temp1 contains zero information. This makes debugging harder as you have to keep looking what the anonymous variables mean and what they are supposed to contain. Don't do this to yourself. Use descriptive names. As an added bonus the code will comment itself if the names are good enough.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287045%2fc-process-died-on-signal-6%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Reducing your program to the minimum needed to demonstrate the error and adding a couple debug statements



    #include <iostream>
    #include <string>
    using namespace std;

    void splitData(string temp1, string temp2[4])

    int x = 0;
    string mark = ",";
    size_t pos = 0;
    while ((pos = temp1.find(mark)) != string::npos)

    temp2[x] = temp1.substr(0, pos);
    cout << temp2[x] << ": ";
    x++;
    temp1.erase(0, pos + mark.length());
    cout << x << ": " << temp1 <<"n";

    if (x >= 4)

    cout << "Accessing temp2[x] with x = " << x << " when temp2 contains 4 elementsn";

    temp2[x] = temp1;


    int main()

    string temp2[4];
    splitData("A,B,C,D,", temp2);



    we get the following output:



    A: 1: B,C,D,
    B: 2: C,D,
    C: 3: D,
    D: 4:
    Accessing temp2[x] with x = 4 when temp2 contains 4 elements


    Which clearly shows x is allowed to get out of range. Why? Count the number of items in



    "A,B,C,D,"
    1 2 3 4 5


    There is an empty fifth element. Luc Besson would not approve.



    Solution



    Remove the trailing comma in the input file.



    Better Solution



    Use a vector so that the number of elements doesn't matter.



    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;

    vector<string> splitData(string temp1)

    vector<string> tokens;
    string mark = ",";
    size_t pos = 0;
    while ((pos = temp1.find(mark)) != string::npos)

    tokens.push_back(temp1.substr(0, pos));
    temp1.erase(0, pos + mark.length());

    tokens.push_back(temp1);
    return tokens;


    int main()

    vector<string> tokens = splitData("A,B,C,D,");
    cout << "Number of tokens: " << tokens.size() << 'n';

    for (const string & token: tokens)

    cout << token << 'n';

    cout << "Done.n";



    Output:



    Number of tokens: 5
    A
    B
    C
    D

    Done.


    Unrelated comments



    The naming of most of the variables is bad. string temp1 contains zero information. This makes debugging harder as you have to keep looking what the anonymous variables mean and what they are supposed to contain. Don't do this to yourself. Use descriptive names. As an added bonus the code will comment itself if the names are good enough.






    share|improve this answer





























      0














      Reducing your program to the minimum needed to demonstrate the error and adding a couple debug statements



      #include <iostream>
      #include <string>
      using namespace std;

      void splitData(string temp1, string temp2[4])

      int x = 0;
      string mark = ",";
      size_t pos = 0;
      while ((pos = temp1.find(mark)) != string::npos)

      temp2[x] = temp1.substr(0, pos);
      cout << temp2[x] << ": ";
      x++;
      temp1.erase(0, pos + mark.length());
      cout << x << ": " << temp1 <<"n";

      if (x >= 4)

      cout << "Accessing temp2[x] with x = " << x << " when temp2 contains 4 elementsn";

      temp2[x] = temp1;


      int main()

      string temp2[4];
      splitData("A,B,C,D,", temp2);



      we get the following output:



      A: 1: B,C,D,
      B: 2: C,D,
      C: 3: D,
      D: 4:
      Accessing temp2[x] with x = 4 when temp2 contains 4 elements


      Which clearly shows x is allowed to get out of range. Why? Count the number of items in



      "A,B,C,D,"
      1 2 3 4 5


      There is an empty fifth element. Luc Besson would not approve.



      Solution



      Remove the trailing comma in the input file.



      Better Solution



      Use a vector so that the number of elements doesn't matter.



      #include <iostream>
      #include <string>
      #include <vector>
      using namespace std;

      vector<string> splitData(string temp1)

      vector<string> tokens;
      string mark = ",";
      size_t pos = 0;
      while ((pos = temp1.find(mark)) != string::npos)

      tokens.push_back(temp1.substr(0, pos));
      temp1.erase(0, pos + mark.length());

      tokens.push_back(temp1);
      return tokens;


      int main()

      vector<string> tokens = splitData("A,B,C,D,");
      cout << "Number of tokens: " << tokens.size() << 'n';

      for (const string & token: tokens)

      cout << token << 'n';

      cout << "Done.n";



      Output:



      Number of tokens: 5
      A
      B
      C
      D

      Done.


      Unrelated comments



      The naming of most of the variables is bad. string temp1 contains zero information. This makes debugging harder as you have to keep looking what the anonymous variables mean and what they are supposed to contain. Don't do this to yourself. Use descriptive names. As an added bonus the code will comment itself if the names are good enough.






      share|improve this answer



























        0












        0








        0







        Reducing your program to the minimum needed to demonstrate the error and adding a couple debug statements



        #include <iostream>
        #include <string>
        using namespace std;

        void splitData(string temp1, string temp2[4])

        int x = 0;
        string mark = ",";
        size_t pos = 0;
        while ((pos = temp1.find(mark)) != string::npos)

        temp2[x] = temp1.substr(0, pos);
        cout << temp2[x] << ": ";
        x++;
        temp1.erase(0, pos + mark.length());
        cout << x << ": " << temp1 <<"n";

        if (x >= 4)

        cout << "Accessing temp2[x] with x = " << x << " when temp2 contains 4 elementsn";

        temp2[x] = temp1;


        int main()

        string temp2[4];
        splitData("A,B,C,D,", temp2);



        we get the following output:



        A: 1: B,C,D,
        B: 2: C,D,
        C: 3: D,
        D: 4:
        Accessing temp2[x] with x = 4 when temp2 contains 4 elements


        Which clearly shows x is allowed to get out of range. Why? Count the number of items in



        "A,B,C,D,"
        1 2 3 4 5


        There is an empty fifth element. Luc Besson would not approve.



        Solution



        Remove the trailing comma in the input file.



        Better Solution



        Use a vector so that the number of elements doesn't matter.



        #include <iostream>
        #include <string>
        #include <vector>
        using namespace std;

        vector<string> splitData(string temp1)

        vector<string> tokens;
        string mark = ",";
        size_t pos = 0;
        while ((pos = temp1.find(mark)) != string::npos)

        tokens.push_back(temp1.substr(0, pos));
        temp1.erase(0, pos + mark.length());

        tokens.push_back(temp1);
        return tokens;


        int main()

        vector<string> tokens = splitData("A,B,C,D,");
        cout << "Number of tokens: " << tokens.size() << 'n';

        for (const string & token: tokens)

        cout << token << 'n';

        cout << "Done.n";



        Output:



        Number of tokens: 5
        A
        B
        C
        D

        Done.


        Unrelated comments



        The naming of most of the variables is bad. string temp1 contains zero information. This makes debugging harder as you have to keep looking what the anonymous variables mean and what they are supposed to contain. Don't do this to yourself. Use descriptive names. As an added bonus the code will comment itself if the names are good enough.






        share|improve this answer















        Reducing your program to the minimum needed to demonstrate the error and adding a couple debug statements



        #include <iostream>
        #include <string>
        using namespace std;

        void splitData(string temp1, string temp2[4])

        int x = 0;
        string mark = ",";
        size_t pos = 0;
        while ((pos = temp1.find(mark)) != string::npos)

        temp2[x] = temp1.substr(0, pos);
        cout << temp2[x] << ": ";
        x++;
        temp1.erase(0, pos + mark.length());
        cout << x << ": " << temp1 <<"n";

        if (x >= 4)

        cout << "Accessing temp2[x] with x = " << x << " when temp2 contains 4 elementsn";

        temp2[x] = temp1;


        int main()

        string temp2[4];
        splitData("A,B,C,D,", temp2);



        we get the following output:



        A: 1: B,C,D,
        B: 2: C,D,
        C: 3: D,
        D: 4:
        Accessing temp2[x] with x = 4 when temp2 contains 4 elements


        Which clearly shows x is allowed to get out of range. Why? Count the number of items in



        "A,B,C,D,"
        1 2 3 4 5


        There is an empty fifth element. Luc Besson would not approve.



        Solution



        Remove the trailing comma in the input file.



        Better Solution



        Use a vector so that the number of elements doesn't matter.



        #include <iostream>
        #include <string>
        #include <vector>
        using namespace std;

        vector<string> splitData(string temp1)

        vector<string> tokens;
        string mark = ",";
        size_t pos = 0;
        while ((pos = temp1.find(mark)) != string::npos)

        tokens.push_back(temp1.substr(0, pos));
        temp1.erase(0, pos + mark.length());

        tokens.push_back(temp1);
        return tokens;


        int main()

        vector<string> tokens = splitData("A,B,C,D,");
        cout << "Number of tokens: " << tokens.size() << 'n';

        for (const string & token: tokens)

        cout << token << 'n';

        cout << "Done.n";



        Output:



        Number of tokens: 5
        A
        B
        C
        D

        Done.


        Unrelated comments



        The naming of most of the variables is bad. string temp1 contains zero information. This makes debugging harder as you have to keep looking what the anonymous variables mean and what they are supposed to contain. Don't do this to yourself. Use descriptive names. As an added bonus the code will comment itself if the names are good enough.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 18:57

























        answered Nov 13 '18 at 18:50









        user4581301user4581301

        19.8k51831




        19.8k51831



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287045%2fc-process-died-on-signal-6%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto