Hi to all C++ programmers, I am at collage and studding computer science. I have problem to solve but my code have a error. Basically I have to read a file with 1000 boys and girls names being ranked , load them in to arrays and then after user type a name the program have to search those arrays and find the name and display it along with its ranking. File:
1 Jacob Emily
2 Michael Emma
3 Joshua Madison
4 Matthew Olivia
5 Ethan Hannah
6 Andrew Abigail
7 Daniel Isabella
8 William Ashley
9 Joseph Samantha
10 Christopher Elizabeth
When I compile my code assigning tagetName as char it compiles but no output, when I assign it as string it gives error that it can transfer int to char in that line ; if(targetName == boys[i])
I am caind of stcuk at this point, I will be thankfull if someone can give me some tip or if I need to change the way I have done the code.
Thanks
code:
Code:#include <iostream> #include <fstream> #include <string> #include <cstdlib> using namespace std; int main( ) { char boys[1000], girls[1000]; int count = 0, boyRank = 0, girlsRank = 0, num; char targetName[16]; cout <<"Type name to search\n"; cin >> targetName; ifstream inFile; inFile.open ("babynames2004.txt"); if (inFile.fail( )) { cout << "Input file opening failed.\n"; exit(1); } while(! inFile.eof( )) { inFile >> num; inFile >> boys[count]; inFile >> girls[count]; count++; } for (int i = 0; i < 1000; i++) { { if(targetName == boys[i]) boyRank = i+1; break; } { if(targetName == girls[i]) girlsRank = i+1; break; } } if(boyRank != 0 && girlsRank !=0) cout << targetName <<" is ranked "<< boyRank <<"among boys.\n"; cout << targetName <<" is ranked "<< girlsRank <<"among girls.\n"; else if(boyRank != 0 && girlsRank = 0) cout << targetName <<" is ranked "<< boyRank <<"among boys.\n"; cout << targetName <<" is not ranked among to 1000 girl names.\n"; else if(boyRank = 0 && girlsRank != 0) cout << targetName <<" is ranked "<< girlsRank <<"among girls.\n"; cout << targetName <<" is not ranked among to 1000 boy names.\n"; return 0; }