There are two problems with this program. Please help me fix them. 1. Should not
ID: 3536875 • Letter: T
Question
There are two problems with this program. Please help me fix them.
1. Should not use full path to open the input file. the full path only exists on your computer.
2. After entering a name, the program stops respond, no output.
//
// main.cpp
// ch. 6 p. 17
//
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
void find_name(string);
int main ()
{
string name;
cout << "Enter a baby name to see how popular it is: ";
cin >> name;
find_name (name);
return 0;
}
void find_name (string name)
{
int rank, brank,grank;
string male, female;
ifstream fin;
bool male_rank = false, female_rank = false;
fin.open ("/Users/carolynmeyers/Desktop/C++/ch. 6 project 17/ch. 6 project 17/babynames2004.txt");
if (fin.fail() && male_rank == false && female_rank == false)
{
cout << "The file can not be opened. ";
exit(1);
}
while (!fin.eof() && male_rank == false || female_rank == false)
{
fin>>rank>>male>>female;
if (name == male)
{male_rank=true;
brank = rank;
}
if (name == female)
{female_rank=true;
grank=rank;
}
}
if(male_rank == true && female_rank != true)
{
cout << " "<<name << " is ranked " << brank << " in popularity among boys. ";
cout << " "<<name << " is not ranked among the top 1000 girl names. ";
}
else if (!male_rank== true && female_rank == true)
{
cout << " "<<name << " is not ranked among the top 1000 boys names. ";
cout <<" "<< name << " is ranked " << grank << " in popularity among girls. ";
}
else if (male_rank == true && female_rank == true)
{
cout <<" "<< name << " is ranked " << brank << " in popularity among boys. ";
cout <<" "<< name << " is ranked " << grank << " in popularity among girls. ";
}
else if (male_rank != true && female_rank != true)
{
cout << " "<<name << " is not ranked among the top 1000 boys names. ";
cout <<" "<< name << " is not ranked among the top 1000 girl names. ";
}
fin.close( );
}
Explanation / Answer
1) for the first problem u shoulf save ur program in the same directry in which the given file is saved then u only have to write the filename into the command
like if u save ur file into the file ch. 6 project 17. then ur command will be
fin.open ("babynames2004.txt");
2) may be it's because In the command
fin.open ("/Users/carolynmeyers/Desktop/C++/ch. 6 project 17/ch. 6 project 17/babynames2004.txt");
u have writen /ch. 6 project 17/ch. 6 project 17/ two times.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.