Segmentation Fault

Pages: 12
You know what? It's not worth fighting over. I'll just laugh when you come back with another SEGFAULT.
I'm not trying to fight with you Pi. Grey's a douche and has me agitated. You were trying to be constructive and I appreciate that.
closed account (z05DSL3A)
I didn't actually bother counting the number of Albums in the data file. So you are reading five lots of data but you are still going out of bounds on the beatles array as it is defined for 4 albums. You are just lucky that you have not overwritten any other data.

I'm only trying to help you here.
Last edited on
Well, an array of ar[4] has a count of 5 correct? I was certain that it was for all arrays. You're trying to say that beatles[4] has a count of just 4?
closed account (z05DSL3A)
int ar[4]; has four ints, ar[0] to ar[3].
Last edited on
what about arse of 4? where did that one go?
closed account (z05DSL3A)
See: http://www.cplusplus.com/doc/tutorial/arrays/
when I change my code from <= 4 to x <= 5 then the segfault returns.. that's what I'm saying
I also made the struct album beatles[4]; to struct album beatles[5];
closed account (z05DSL3A)
your code should be allong the lines of
1
2
3
4
5
6
7
8
const int size= 5;

struct album beatles[size];
//...
for (int i = 0; i < size; ++i)  // note not <=
{
    //...
}
ok then why can it not be a number declared inside the array? and that's the same as i <= 4 correct?
ok here is the tweaked code with the sort album names function incorporated and the incorporation of your last post:

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

struct album
{
  string albumN,year,songL[30],song[30];
  int c;
};

void sSort(string ar[], string arL[], int elems)
  {
    int swaps = 1,j;
    while(swaps)
      {
        swaps = 0;

        for(j = 0; j < elems-1; j++)
          {
            if(arL[j].compare(arL[j+1]) == 1)
              {
                swap(arL[j],arL[j+1]);
                swap(ar[j],ar[j+1]);
                swaps = 1;
              }
          }
      }
  }

void sort_albums( struct album beatles[], int c)
  {
    int swaps = 1,j;
    while(swaps)
      {
        swaps = 0;
        for(j = 0; j < c-1; j++)
          {
            if(beatles[j].albumN.compare(beatles[j+1].albumN) == 1)
              {
                swap(beatles[j],beatles[j+1]);
                swaps = 1;
              }
          }
      }
  }


int main ()
  {
    const int size = 5;
    string line,ar[200];
    struct album beatles[size];
    int len,i = 0,x = 0;

    ifstream beatlesData ("beatles.c"); //opening the file.

    if (beatlesData.is_open()) // if the file is open
    {
       while (!beatlesData.eof()) // while the end of file is NOT reached
          {
             for(x = 0; x < size; x++)
             {

               // get album name
               getline(beatlesData,line);
               beatles[x].albumN = line;

               // get album year
               getline(beatlesData,line);
               beatles[x].year = line;
               beatles[x].c = 0;

               // get the first song
               getline(beatlesData,line);

               // get the rest of the songs
               // get album songs
               do
                 {
                   len = line.length();
                   beatles[x].song[i] = line;
                   beatles[x].songL[i] = beatles[x].song[i].substr(4,len);
                   beatles[x].c++;
                   getline(beatlesData,line);
                   i++;
                 } while(line[0] != '=' && !beatlesData.eof());

               //sort songs

               sSort(beatles[x].song, beatles[x].songL, i);
               // reset the number of songs to 0
               i = 0;
             }
          }

        sort_albums(beatles, 5);

        for(int y = 0; y < size; y++)
          {
            cout << beatles[y].albumN << endl;
            cout << beatles[y].year << endl;
            for(int x = 0; x < beatles[y].c; x++)
              {
                cout << beatles[y].song[x] << endl;
              }
            cout << "==============================" << endl;
          }
        beatlesData.close(); //closing the file
      }

    else cout << "Unable to open file"; //if the file is not open output
    return 0;
  }


I maybe got a little butt hurt about how harshly you criticized it at first.. No hard feelings?
closed account (z05DSL3A)
You can do
1
2
3
4
5
6
struct album beatles[5];
//...
for (int i = 0; i < 5; ++i)  // note not <=
{
    //...
}

or even
1
2
3
4
5
6
struct album beatles[5];
//...
for (int i = 0; i <=4 ; ++i) 
{
    //...
}

but ultimately these two forms are more prone to errors if you change the code.

in your original code, you should change struct album beatles[4]; to struct album beatles[5]; then you have the correct array size for the five albums to be read into and your indexing of the array will not go out of bounds.
so one would always want a const int inside of an array declaration?
closed account (z05DSL3A)
No hard feelings?

I've got a thick skin, if I had hard feeling I would have just left the thread alone.

Have fun.

Edit:
Array declarations have to be constant, doing it with a const int in the way shown means that you only have to change the size in one place if needed and you will not break your indexing.

anyway, its very late here and I have to get up for work in six hours.
Last edited on
Alrighty thanks man I appreciate it.
Topic archived. No new replies allowed.
Pages: 12