segmentation fault

i have my code all done but when i try to run it there is a segmentation fault right after i have someone input a year. i dont know why


#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int size = 1000;
void askName (string& name);
int search (string arr[], int size, string name);

int main()
{
string mnames[size], fnames[size];
string name;
int mloc, floc, year;
ifstream in_stream;
cout << "Enter the year you want to look at: " << endl;
cin >> year;
if (year == 2013){
in_stream.open("babynames2013.txt");
int i = 0;
while (in_stream >> mnames[i] >> fnames[i] && i++ < size);
for (int i = 0; i < size; i++)
cout << mnames[i] << "\t\t" << fnames[i] << endl;}

else if (year == 2014){
in_stream.open("babynames2014.txt");
int i = 0;
while (in_stream >> mnames[i] >> fnames[i] && i++ < size);
for (int i = 0; i < size; i++)
cout << mnames[i] << "\t\t" << fnames[i] << endl;}

else if (year == 2015){
in_stream.open("babynames2015.txt");
int i = 0;
while (in_stream >> mnames[i] >> fnames[i] && i++ < size);
for (int i = 0; i < size; i++)
cout << mnames[i] << "\t\t" << fnames[i] << endl;}
else
{cout << "Try Again: " << endl;
cin >> year;}

askName (name);
mloc= search (mnames, size, name);
floc= search (fnames, size, name);
mloc =+ 1;
floc =+ 1;
cout << name << "is the " << mloc << " most popular name for boys in " << year << endl;
cout << name << "is the " << floc << " most popular name for girls in " << year << endl;
}

void askName (string& name){
cout << "Enter a name" << endl;
cin >> name;
}

int search (string arr[], int size, string name){
int i;
i=0;
if (arr[i]!= name){
while (i < 1001)
i++;
}
return i;
}
The function search() causes segmentation fault.
I cannot reproduce the segmentation fault. Please provide more details like the content of the files in order to reproduce the problem.

The function search(...) doesn't make sense but it is not a problem (since the result is not used as an index).

Consider this:
1
2
mloc =+ 1; // 1 is assigned and hence the result from search(...) overwritten
floc =+ 1; // 1 is assigned and hence the result from search(...) overwritten 


1
2
3
while (in_stream >> mnames[i] >> fnames[i] && i++ < size); // At this point i contains the number of names
for (int i = 0; i < size; i++) // Now i is overwritten and the whole array is printed instead of the real number of names
cout << mnames[i] << "\t\t" << fnames[i] << endl;
while (in_stream >> mnames[i] >> fnames[i] && i++ < size)
This is going to stream data into the arrays BEFORE it does anything about checking their size. Afterwards, it will check i < size and then increment i as i++.

while (in_stream >> mnames[i] >> fnames[i] && ++i < size)
would work (prefix instead) but is horrible.

Try
while (i < size && in_stream >> mnames[i] >> fnames[i] ) i++;
It's safer.

Even then, as already pointed out, there are going to be problems down the line with search().

(Program checked by turning size down a lot and making up a file with the names of posters on this forum.)
Last edited on
Topic archived. No new replies allowed.