Question:
help finishing this c++ program?
?
2012-11-09 18:49:37 UTC
Basically, we are taking baby names from a file and opening the file up in our program. we are using arrays and taking it from a file named babynames2004.txt and putting that data into the array. the program should output the rank of the baby name. i can't figure out what's wrong with my program.

#include
#include
#include
#include
using namespace std;

int main( )
{
char male[1000], female[1000];
int count = 0, malerank = 0, femalerank = 0, num;
char targetname[16];
cout <<"Type name to search\n";
cin >> targetname;

ifstream infile;
infile.open ("babynames2004.txt");

if (infile.fail( ))
{
cout << "babynames2004.txtopening failed.\n";
exit(1);
}

while(! infile.eof( ))
{
infile >> num;
infile >> male[count];
infile >> female[count];
count++;
}
for (int i = 0; i < 1000; i++)
{
{
if(targetname == female[i])
malerank = i+1;
break;
}
{
if(targetname == female[i])
femalerank = i+1;
break;
}
}
if(malerank != 0 && femalerank !=0){
cout << targetname <<" is ranked "<< malerank <<"among boys.\n";
cout << targetname <<" is ranked "<< femalerank <<"among girls.\n";
}
else if(malerank != 0 && femalerank = 0){
cout << targetname <<" is ranked "<< malerank <<"among boys.\n";
cout << targetname <<" is not ranked among to 1000 girl names.\n";
}
else if(malerank = 0 && femalerank != 0){
cout << targetname <<" is ranked "<< femalerank <<"among girls.\n";
cout << targetname <<" is not ranked among to 1000 boy names.\n";
}
return 0;
}
Four answers:
?
2012-11-09 19:42:13 UTC
I'm not sure what exactly your problem is? maybe you could elaborate..but i can tell you on these two lines

else if(malerank != 0 && femalerank = 0)

else if(malerank = 0 && femalerank != 0



You should use == instead of = because when you put = it will actually assign the value 0 to your variable, while == just compares it which it what you want So change those two lines to this



else if(malerank != 0 && femalerank == 0)

else if(malerank == 0 && femalerank != 0)



Hope that helps!
2016-11-02 11:47:16 UTC
i have by technique of no capacity had a danger to debug your code; though once you've a operating linked list implementation, then all you should do is to take the gadgets so as from the first list and upload them to a sparkling empty 2d list interior the suited route. on condition that you'll have insertNodeFront and insertNodeEnd you've were given offered the needed positive residences; basically pick the guy who inserts interior the opposite order of getNext(). EDIT: it may well be such an excellent implementatation; there might want to be somewhat no LinkedList class. attempt a million. count the elements. 2. Make an array to carry a pointer for each ingredient. Use the remember or use an well-known array (I used 32 elements) see the detect[a million]. the different version (now no longer valid ISO C++) makes use of a variable sized array[2]. 3. walk the array and rewrite the "link" tips.
2012-11-09 19:51:06 UTC
include

#include

#include



using namespace std;



int main()

{

ifstream fin;

fin.open("babynames2004.txt");

if( fin.fail() )

exit(0);



string names[3000];

int rank[3000];

string name;

int r;

int i=0;



while( !fin.fail() )

{

fin>>name;

fin>>r;



names[i] = name;

rank[i] = r;



i++;

}





while( true )

{

cout << "Enter a name";

cin >> name;



for(int i=0; i<3000; i++)

{

if( names[i] == name)

cout << name << " is ranked " << rank[i];

}

}



system("pause");

return 0;

}
Brett
2012-11-09 18:52:22 UTC
If i were you i would go back through and check the spaces, happens when i do work computer programing at computer repair shop. I use this forum, lots of helpful answers, check it out heres a link!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...