C++匹配文件中的字符串并获取行号
我有一个包含前 1000 个婴儿名字的文件。我想询问用户一个名字...搜索文件...并告诉用户该名字对于男孩名字的排名以及女孩名字的排名。如果它不是男孩名字或女孩名字,它会告诉用户它不属于该性别的流行名字。
该文件的布局如下:
Rank Boy-Names Girl-Names
1 Jacob Emily
2 Michael Emma
.
.
.
输入 Michael
所需的输出将是:
迈克尔是男孩名字中第二受欢迎的名字。
如果迈克尔不是女孩的名字,则应该说:
迈克尔并不是最受欢迎的女孩名字之一
但如果是的话,它会说:
迈克尔是(排名)女孩名字中的
到目前为止我的代码如下..我似乎无法弄清楚。感谢您的任何帮助。
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;
void find_name(string name);
int main(int argc, char **argv)
{
string name;
cout << "Please enter a baby name to search for:\n";
cin >> name;
/*while(!(cin>>name))
{
cout << "Please enter a baby name to search for:\n";
cin >> name;
}*/
find_name(name);
cin.get();
cin.get();
return 0;
}
void find_name(string name)
{
ifstream input;
int line = 0;
string line1 = " ";
int rank;
string boy_name = "";
string girl_name = "";
input.open("/<path>/babynames2004.rtf");
if (!input)
{
cout << "Unable to open file\n";
exit(1);
}
while(input.good())
{
while(getline(input,line1))
{
input >> rank >> boy_name >> girl_name;
if (boy_name == name)
{
cout << name << " is ranked " << rank << " among boy names\n";
}
else
{
cout << name << " is not among the popular boy names\n";
}
if (girl_name == name)
{
cout << name << " is ranked " << rank << " among girl names\n";
}
else
{
cout << name << " is not among the popular girl names\n";
}
}
}
input.close();
}
I have a file with the top 1000 baby names. I want to ask the user for a name...search the file...and tell the user what rank that name is for boy names and what rank for girl names. If it isn't in boy names or girl names, it tells the user it's not among the popular names for that gender.
The file is laid out like this:
Rank Boy-Names Girl-Names
1 Jacob Emily
2 Michael Emma
.
.
.
Desired output for input Michael
would be:
Michael is 2nd most popular among boy names.
If Michael is not in girl names it should say:
Michael is not among the most popular girl names
Though if it was, it would say:
Micheal is (rank) among girl names
The code I have so far is below.. I can't seem to figure it out. Thanks for any help.
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;
void find_name(string name);
int main(int argc, char **argv)
{
string name;
cout << "Please enter a baby name to search for:\n";
cin >> name;
/*while(!(cin>>name))
{
cout << "Please enter a baby name to search for:\n";
cin >> name;
}*/
find_name(name);
cin.get();
cin.get();
return 0;
}
void find_name(string name)
{
ifstream input;
int line = 0;
string line1 = " ";
int rank;
string boy_name = "";
string girl_name = "";
input.open("/<path>/babynames2004.rtf");
if (!input)
{
cout << "Unable to open file\n";
exit(1);
}
while(input.good())
{
while(getline(input,line1))
{
input >> rank >> boy_name >> girl_name;
if (boy_name == name)
{
cout << name << " is ranked " << rank << " among boy names\n";
}
else
{
cout << name << " is not among the popular boy names\n";
}
if (girl_name == name)
{
cout << name << " is ranked " << rank << " among girl names\n";
}
else
{
cout << name << " is not among the popular girl names\n";
}
}
}
input.close();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在你扫描完列表之前,你就已经承认失败了(“X 不在流行的 Y 名字之列”)。一个很好的简单方法(如果不是最有效的话)是在宣布没有匹配之前记住排名直到列表末尾。像这样的事情:
You are conceding defeat ("X is not among the popular Y names") before you have finished scanning the list. A good simple way (if not the most efficient) is to remember the ranks until the end of the list, before announcing that there was no match. Something like this:
在发布问题之前,您绝对可以对问题进行更多分析和隔离。
例如,您处于调用
getline
的循环中,该循环将从您的input
中读取一行文本到line1
中。但随后您对line1
不执行任何操作。您可以使用 iostream 运算符读取其后一行的数据字段。所以这将有效地跳过所有其他行。很容易看出,如果您只是在循环中进行调试健全性检查,例如:那么您可以将您的问题表述为“为什么我只得到每隔一行”。人们更容易回答这个问题,但您也可能有机会自己回答。
另一个潜在的问题是,如果您有 .RTF“富文本”文件而不是纯文本文件。它可能包含额外的垃圾,使您的代码感到困惑。家庭作业通常不会给你提供古怪的格式来处理,因为那是一种全新的蠕虫。
You could definitely do some more analysis and isolation of your problem before posting a question.
For instance, you are in a loop calling
getline
, which reads a line of text intoline1
out of yourinput
. But then you do nothing withline1
. You use iostream operators to read in the fields of data on the line after it. So this is going to effectively skip every other line. It would be easy to tell that if you just put in a debugging sanity check in your loop like:Then you could formulate your question as "why am I only getting every other line". Which would be easier for people to answer, but you might also have a chance at answering it yourself.
Another potential problem is if you have a .RTF "Rich Text" file instead of a plain-ol text file. It may have extra junk in it that's confusing your code. Homework assignments would not typically give you wacky formats to deal with, because that's a whole new can of worms.
首先,您似乎正在尝试打开富文本格式文件 (.rtf)。这是行不通的,因为该文件不仅包含文本,还包含其他数据(http://en .wikipedia.org/wiki/Rich_Text_Format)。
然后在您的代码中:
while(getline(input,line1))
每次迭代都会读取一行。没问题,但是在循环内你可以输入input>>> 。排名>>男孩名字>> Girl_name;
在下一行中继续读取您想要使用
line1
。您可以从 line1 构造一个字符串流,然后从中读取名称:That 以及 Beta 在他的答案中写的内容;您在名称不匹配的每一行中“放弃”。
First off, looks like you're trying to open a rich text format file (.rtf). This won't work, as the file doesn't contains only text but also other data (http://en.wikipedia.org/wiki/Rich_Text_Format).
Then in your code:
while(getline(input,line1))
reads a line every iteration. That's fine, but inside the loop yo doinput >> rank >> boy_name >> girl_name;
which continues to read in next lineYou want to work with
line1
. You can construct a stringstream from line1, then read the names from it:That and what Beta wrote in his answer; you "give up" in each line where names don't match.