C++正在将行搜索到txt文件中
C++ Searching line into txt file
txt文件中有1000个名称。
1 Jacob Sophia
2 Mason Emma
3 Ethan Isabella
4 Noah Olivia
5 William Ava
.....
节目总是说这个名字不在流行的男孩/女孩名字中。
调试后,我意识到"排名"从未增加。总是1,我以为程序只是在检查第一行。我再次运行程序并键入Jacob。结果是一样的。我看不出我的错误在哪里。
int main()
{
bool boyName = false;
bool girlName = false;
ifstream ifs;
int rank;
string boy_name, girl_name, name;
ifs.open("babynames2012.txt");
if (ifs.fail())
cout << "fail" << endl;
else
cout << "success" << endl;
cout << "Enter a name" << endl;
cin >> name;
while (ifs >> rank)
{
ifs >> rank >> boy_name >> girl_name;
if (name == boy_name)
{
cout << name << " is ranked " << rank << " among boy names" << endl;
boyName = true;
}
if (name == girl_name)
{
cout << name << " is ranked " << rank << " among girl names" << endl;
girlName = true;
}
if (boyName == false)
{
cout << name << " is not among the popular boy names" << endl;
}
if (girlName == false)
{
cout << name << " is not among the popular girl names" << endl;
}
}
ifs.close();
system("pause");
return 0;
}
结果总是相同的
<name> is not among the popular boy names.
<name> is not among the popular girl names.
在循环中间关闭ifs
不是个好主意。。。
相关文章:
- 如何将 txt 文件中的行分隔为组件C++
- 无法打开.txt文件
- 如何使用CLion在Mac上创建一个新的.txt文件
- 使用fstream对txt文件中的数字进行循环
- Visual Studio在尝试读取resource.txt文件时崩溃
- 如何从txt文件中读取多个不同长度的数组?
- 在一次迭代中从 txt 文件中读取多行
- 通过比较C++中的行在 txt 文件中搜索的最简单方法是什么?
- 写入超过 1GB 的.txt文件时出现奇怪的行为
- 视觉 如何循环访问C++中遵循类似格式的多个.txt文件?
- txt 文件中浮点数的最大和最小值
- 从.txt文件中读取浮点型数字并在公式中使用它们
- C++将文本从.txt文件解析为变量
- 在树结构.txt文件中搜索节点
- 为什么我无法打开/读取从 Python 调用的 C 扩展名中的 txt 文件?
- 在 txt 文件中显示前两个数字的程序
- 如何从具有两列的.txt文件创建并行数组?
- 如何使用c ++读取带有非英文字母的*.txt文件?(国际化)
- 如何像在 C++ 中处理数组一样对 .txt 文件中的字符进行寻址?
- 如何读取 TXT 文件并仅将 int 存储在 C 中