Seg fault I can't seem to fix

I believe my headers and 2 of my .cpp files are correct. however i get a segfault that seems to be because of lines 20 and 32 of the second code block. I can provide more code if needed. I can't fix the segfault. any help would be greatly appreciated

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <istream>
#include "dates.h"
#include "addresses.h"
#include "student.h"

using namespace std;

int main(){
// creeate a new instance of the array student[50] on the heap
student *studentgroup = new student[49];

//use fstream and the method read student
//to assign values for all peramiters in the
//aray studentgroup[50]
  string line;
  int i = 1;
  ifstream myfile ("uneditedinput.txt");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line, '\n');
      studentgroup[i].readStudent(line);
      i++;
    }
    myfile.close();
  }

  else cout << "Unable to open file";
//use bubble sort to sort the array by name
//utilizing a temporary student class to hold values
    student tempstudent;
    int j, k; //variables used for for loops.
  for (j = 0; j < 49; j++) {
    for (k = 0; k < 48; k++) {
      if (studentgroup[k].firstName > studentgroup[k + 1].firstName) {
        tempstudent = studentgroup[k];
        studentgroup[k] = studentgroup[k + 1];
        studentgroup[k + 1] = tempstudent;
      }
    }
  }
//open menu
//use command prompt to ask for value
//1. print student data in a report format
//2. print list of students first and last name
//3. exit
bool keepgoing = true;
int menu;
int l;
    while(keepgoing){
        cout << "Menu" << endl << "1. Print Student Data report" << endl;
        cout << "2.Print list of students by first and last name" <<endl;
        cout << "3. exit" << endl << "Enter a value from 1 to 3: ";
        cin >> menu;
            if (menu == 1){
                    for (l = 0; l < 49; l++ ){
                        cout << studentgroup[l].firstName << " " << studentgroup[l].lastName;
                        cout << endl << "adress: " << studentgroup[l].adress.adressLine1 << endl << studentgroup[l].adress.city << ',' << studentgroup[l].adress.state << ',' << studentgroup[l].adress.zipcode << endl;
                        cout << "DOB: " << studentgroup[l].dob.month << "/" << studentgroup[l].dob.day << "/" << studentgroup[l].dob.year;
                        cout << " Completion Date: " << studentgroup[l].completionDate.month << "/" << studentgroup[l].completionDate.day << "/" << studentgroup[l].completionDate.year;
                        cout << endl << "GPA: "<< studentgroup[l].gpa << " Credit Hours Completed: ";
                        cout << studentgroup[l].creditHours << endl;
                        }
            } else if (menu == 2){
                    for (l = 0; l < 49; l++ ){
                        cout << studentgroup[l].firstName<< " " << studentgroup[l].lastName <<endl;
                        }
            } else if (menu == 3){
                    for (l = 0; l < 49; l++ ){
                        studentgroup[l].~student();
                        }
                        return 0;
            }else {//incorrect input
                    cout << "Please follow input instructions." << endl;
                } // end if
      } // end while
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "student.h"
#include "dates.h"
#include "addresses.h"
#include <iostream>
#include <istream>
#include <fstream>
#include <string>

using namespace std;

student::student(){

}

student::~student(){

}


string* student::tokenize( const string str, string delim, int arrSize ){
  istringstream iss(str);
  string *words = new string[arrSize];
  string temp;
  int i = 0;
  while( getline( iss, temp, ',') ){
    words[i] = temp;
    i++;
  }
  return words;
}
void student::readStudent(string stng){
  string * s= student::tokenize(stng, ",", 13);
  firstName = s[0];
  lastName = s[1];
  adress.adressLine1 = s[2];
  adress.city = s[3];
  adress.state = s[4];
  adress.zipcode = s[5];
  dob.month = s[6];
  dob.day = s[7];
  dob.year = s[8];
  completionDate.month = s[9];
  completionDate.day = s[10];
  completionDate.year = s[11];
  gpa = s[12];
  creditHours = s[13];
}
Are you sure that the string that you pass to tokenize don't contain more than 13 comma separated words?
positive, here is an example from the input file.

Fry,Lock,123 Hillside Drive,North Brunswick,NJ,57237,08,22,1970,05,15,2012,4.00,90

its 14 total so 13 should be right counting 0.
no, that is not right. When you create the array with size 13, it can store only 13 elements. Last element will be s[12].
okay fixed that line... still won't run tho
Seg fault has almost always to do with boundary trespasing. Some of the arrays you use have the same problem as your s[].

Hope that limits a bit your debug options.
If you build with debug symbols and then run it under a debugger, it will tell you the exact line of code causing the segFault and allow you to check the values of variables at that point to find the culprit. Hours of staring at the code becomes thirty seconds of debugging if you use a debugger.
Topic archived. No new replies allowed.