Segmentation Fault with file I/O

closed account (92yp4iN6)
Hello, I am writing code for a project where the user chooses a year of popular baby names and a file is selected for the user. It is formatted like this...
1 Jacob Sophia
2 Mason Emma
etc...
I am getting a segmentation fault when i try to open the file because when I remove it I do not get the error. Any advice would be helpful, thank you!

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void readin (ifstream& names, string& mArray, string& fArray);
string searchArray (string mArray[], string fArray[], string target);

int main () {
  ifstream names;
  int year;
  cout<<"Enter 2012 2013 2014 or 2015: ";
  cin>>year;

  if (year == 2012)
    names.open("babynames2012.txt");

  else if (year == 2013)
    names.open("babynames2013.txt");

  else if (year == 2014)
    names.open("babynames2014.txt");

  else if (year == 2015)
    names.open("babynames2015.txt");

  else terminate();

  string target;
  string mArray[1000]; //array for male name
  string fArray[1000]; //array for female name

  readin (names, mArray[1000], fArray[1000]);

  cout<<"Enter a name (capitalize the first letter): ";
  cin>>target;
  
  target = searchArray (mArray[1000], fArray[1000], target);

}

void readin (ifstream& names, string& mArray, string& fArray) {
  int i=0, order;
  while (names >> order){
    names >> mArray[i] >> fArray[i];
    i++;
  }
}

string searchArray (string mArray[], string fArray[], string target) {
  int i;
  bool mname, fname;

  for (i=0; i<1000; i++) {
    if (target == mArray[i])
      cout<<target<<" is ranked"<<i<<" in popularity among boy names.";
      mname = true;
    }
    if (target == fArray[i]) {
      cout<<target<<" is ranked"<<i<<" in popularity among girl names.";
      fname = true;
    }
    if (mname != true)
      cout<<target<<" is not ranked among the top 1000 boy names.";

    if (fname != true)
      cout<<target<<" is not ranked among the top 1000 girl names.";

    return target;
}
Last edited on
Remove the occurrences of [1000] in lines 33 and 38 - you only need to use the name of the array (which will pass the requisite pointer). At present you are passing a single element.

In the function definition line 42 remove the & after both occurrences of string and either replace each with * or put [] after mArray and fArray; either technique will indicate a pointer to an array.

(Not in a position to try this out at present.)
The code doesn't compile.

The arguments passed to function searchArray don't match the parameter list.

The header for function readin is incorrect.

The function calls are incorrect.

The function header should be:
 
void readin (ifstream& names, string mArray[], string fArray[])


and the function calls should be:
 
    readin (names, mArray, fArray);


 
    target = searchArray (mArray, fArray, target);


closed account (92yp4iN6)
Hey I got it to work. Thank you guys so much for the help! Apparently I need to work on my function signature lines and the calling of them.
Topic archived. No new replies allowed.