Hello,
First of all, this place is great. With one exception, I have always been able to find information leading me to my answer by browsing through the forums and tutorials. The one exception I will detail below.
I will be honest, this is a homework question, but I am NOT looking for the answer. I have already written the code, I am an astute student, I refuse to share my homework with other students etc, but I am having problems with this one instance. I would ask my professor, but she is impossible to understand, and I've noticed her make a few errors during class, so I'd rather speak to the experts here hehe.
Basically, I need to input baby names from a file into an array (I did three arrays; one each for the rank, boys names, and girls names), then allow the user to search for any name. The result returned to the user is the ranking of their query among Boys names and among Girls names. If the name is not found, I also display that to the console for the user. I am using XCode 3.2.6.
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
|
#include <iostream>
#include <fstream>
using namespace std;
void getInput();
void getBoysNameRank();
void getGirlsNameRank();
string userInput;
string boys[1000];
string girls[1000];
int rank[1000];
bool boysFound = false;
bool girlsFound = false;
ifstream input;
int main() {
getInput();
getBoysNameRank();
getGirlsNameRank();
return 0;
}
void getInput() {
input.open("babynames.txt");
for(int i=0; i<1001; i++) {
input >> rank[i];
input >> boys[i];
input >> girls[i];
}
cout << "Please input your query: ";
cin >> userInput;
input.close();
}
void getBoysNameRank() {
int position;
for(int index=0; index<1001; index++) {
if (userInput == boys[index]) {
position = index;
boysFound = true;
}
}
if (boysFound)
cout << userInput << " is ranked " << position << " in popularity among boys names." << endl;
if (!boysFound)
cout << userInput << " is not among the top 1000 boys names." << endl;
}
void getGirlsNameRank() {
int position;
for(int index=0; index<1001; index++) {
if (userInput == girls[index]) {
position = index;
girlsFound = true;
}
}
if (girlsFound)
cout << userInput << " is ranked " << position << " in popularity among girls names." << endl;
if (!girlsFound)
cout << userInput << " is not among the top 1000 girls names." << endl;
}
|
I've spent a lot of time fiddling and experimenting, but whatever I do, I cannot get the girls result to display with the boys result, and I get what seems like an infinite loop...
Example:
"Please input your query: " Walter
Output:
"Walter is ranked 365 among boys names."
Program received signal: EXC_BAD_ACCESS
(gdb)
I then have to Terminate the program in order to stop it, whereas it should terminate right after printing the results.
I tried removing all the code pertaining to the "getGirlsNameRank()" function, and the program has zero errors, no EXC_BAD_ACCESS or anything.
So I researched this EXC_BAD_ACCESS, which let me know that it is either an object not initialized, an object already released, or "something else that is not very likely to happen" (
http://www.touch-code-magazine.com/how-to-debug-exc_bad_access/). I am brand new to C++, just started learning at the beginning of May, and I am just not sure what I am doing wrong. I read that I can use Zombies to fix the releasing problem, but I am unclear how to apply that to my situation, and as we have not learned anything about that yet, I assume that it is not the fix to my problem.
Is it saying that I cannot reuse the "userInput" variable for my getGirlsNameRank() function? I flipped the order of the getGirlsNameRank and the getBoysNameRank in the int main() function call, but then the program would not output anything.
Or is it something to do with passing values between functions? I tried using references to pass values (in the prototype, the call, and the definition) for the getGirlsNameRank function, then for the whole program, which did not fix it. I also tried simplifying the getBoysNameRank and getGirlsNameRank functions by removing any cout statements and the boolean tests, but that still didn't fix my problem.
Any help is appreciated. If this is deemed too close to helping me with my homework, I understand. Thank you for your time anyway.