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.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace 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;
elsereturn -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;
elsereturn -1;
}
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.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace 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;
constint 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;
elsereturn -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;
elsereturn -1;
}
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 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
@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.