Segmentation fault (core dumped)

I am writing a program where the a txt file of names is passed into two arrays, then the user types a name and the program finds the name in the array and displays its location in the list. I am getting a segmentation fault (core dumped) message when I try to use this program. I've never encountered this before. Any help fixing this would be 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
  #include <iostream>
#include <fstream>
#include <string>
using namespace std;

int searchboy (string boys[], int n, string name);
int searchgirl (string girls[], int n, string name);


int main(){
   ifstream infile;
   infile.open ("project5data.txt");
   int n = 0;
   string boys[n];
   string girls[n];
   int arraysize = 1000;
   string name;
   int boyrank;
   int girlrank;

   while(n < arraysize && infile >> boys[n] >> girls[n]){
    n++;
   }

   infile.close();

   cout << "Enter the name you want to search: ";
   cin >> name;

   boyrank = searchboy(boys, n, name);

   girlrank = searchgirl(girls, n, name);

   cout << name << " is ranked " << boyrank << " in popularity among boys." << endl;
   cout << name << " is ranked " << girlrank << " in popularity among girls." << endl;


}

   int searchboy (string boys[], int n, string name)
   {
   int index = 0;
   bool found = false;
   while ((!found) && (index < n))
    if (name == boys[index])
      found = true;
    else
      index++;
    if (found)
      return index;
    else
      return -1;
   }

   int searchgirl (string girls[], int n, string name)
   {
   int index = 0;
   bool found = false;
   while ((!found) && (index < n))
    if (name == girls[index])
      found = true;
    else
      index++;
    if (found)
      return index;
    else
      return -1;
   }
Don't start a new topic when it is the same assignment you are working on.

http://www.cplusplus.com/forum/beginner/264705/
1
2
3
int n = 0;
string boys[n];
string girls[n];


Your arrays of string are of length 0. That is an issue you'll have to address.
Last edited on
1
2
3
   while(n < arraysize && infile >> boys[n] >> girls[n]){
    n++;
   }


Shouldn't this fix the array size issue?
If you were using a std::vector you can resize it at runtime.

http://www.cplusplus.com/reference/vector/vector/

A C-style array (what you are using) has it's size fixed at compile-time.

Unless you want to do a lot of manual memory management with new[]/delete[] and pointers.

Create your arrays with the arraysize variable, so you need to define that variable before you create your arrays.

Also, arraysize must be defined as const (or constexpr) if you are going to use it to create the size of your arrays:

const int arraysize = 1000;

If you are not receiving an error, or even a warning, using a non-const variable to create C-style arrays your compiler is seriously out-dated.
No. You have to dynamically allocate the string array every time you want to add values to it instead of doing n++. Arrays are fixed length, and you can't just simply add to the end of them without the new operator.

You probably meant to do something like this:
1
2
3
4
int arraysize = 1000; // assume this is the max number of element in the array
int n = 0;
string boys[arraysize];
string girls[arraysize];


and use n as the variable to keep track of how many strings are actually in the array.
I've fixed the array size, but the segmentation fault still needs to be fixed
Are you sure you fixed it? Mine worked just fine after applying the fix to your code.
This is what my code looks like now
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int searchboy (string boys[], int n, string name);
int searchgirl (string girls[], int n, string name);

int main(){
   ifstream infile;
   infile.open ("project5data.txt");
   int n = 0;
   const int arraysize = 1000;
   string boys[arraysize];
   string girls[arraysize];
   string name;
   int boyrank;
   int girlrank;

   while(n < arraysize && infile >> boys[arraysize] >> girls[arraysize]){
    n++;
   }

   infile.close();

   cout << "Enter the name you want to search: ";
   cin >> name;

   boyrank = searchboy(boys, n, name);

   girlrank = searchgirl(girls, n, name);

   cout << name << " is ranked " << boyrank << " in popularity among boys." << endl;
   cout << name << " is ranked " << girlrank << " in popularity among girls." << endl;


}

   int searchboy (string boys[], int n, string name)
   {
   int index = 0;
   bool found = false;
   while ((!found) && (index < n))
    if (name == boys[index])
      found = true;
    else
      index++;
    if (found)
      return index;
    else
      return -1;
   }

   int searchgirl (string girls[], int n, string name)
   {
   int index = 0;
   bool found = false;
   while ((!found) && (index < n))
    if (name == girls[index])
      found = true;
    else
      index++;
    if (found)
      return index;
    else
      return -1;
   }
I've fixed the array size

Then please post the updated code in a new reply here.

PLEASE do not edit your original post.
Assuming that your project5data.txt is formatted the way I think it is, then it should be working. If not, you'll need too modify the way you're reading things in from the file.

EDIT:
My clipboard did not seem to copy things over.

1
2
3
while(n < arraysize && infile >> boys[arraysize] >> girls[arraysize]){
    n++;
}

is still wrong. You should not be indexing using arraysize, but n instead.
Last edited on
This is how the txt file is formated
1
2
3
4
5
6
7
8
9
10
1 Jacob Sophia
2 Mason Emma
3 Ethan Isabella
4 Noah Olivia
5 William Ava
6 Liam Emily
7 Jayden Abigail
8 Michael Mia
9 Alexander Madison
10 Aiden Elizabeth
@fiji885, The OP's text file is here: http://www.d.umn.edu/~jallert/cs1/projects/babynames.txt

He split the same assignment into two topics making it confusing to follow, something I mentioned earlier.
@fiji885, THANK YOU
@Furry Guy, ah, I see. Thank you for this.

@petrjose Since the file is formatted slightly different from what I was using, you will now have to be reading in the rank of the names as well. This should not be too hard, and I am sure you can figure it out.
Something else occurred to me, after looking at the text file petrjose is using.

2 arrays containing std::strings, arrays of 1,000 elements each. On the stack.

That could be a reason for the seg fault. The stack is running out of space.

Not from the strings, though. IIRC the STL uses heap memory for strings.
Topic archived. No new replies allowed.