【发布时间】:2026-01-28 08:30:01
【问题描述】:
我被分配了一个硬件任务,所以我不是要求一整套代码,但也许是一些关于我应该做什么的提示。任务如下: 包含文本文件 babynames2004.txt。包含美国最受欢迎的 1000 个名字的列表。 这是一个以空格分隔的文件,包含 1000 个条目,其中排名在前,然后是相应的男孩名和女孩名。最受欢迎的名字排在最前面,最不受欢迎的名字排在最后。编写一个允许用户输入名称的程序。然后程序应该从文件中读取并在女孩和男孩中搜索匹配的名字,然后将这些信息存储在一个数组中。如果找到匹配项,它应该输出名称的排名。该程序还应指出是否不匹配。
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
//function prototypes
void input(int search(const int name[][Max_Girl_Name], int, int, int );
void display(
int main()
{
const int Max_Girl_Name = 1000, Max_Boy_Name = 1000; //array sizes
int name[][Max_Girl_Name], count(0);
ifstream inputfile; //open stored file
inputFile.open("c:/temp/babynames2004.txt")
if (inputFile.fail())
{
cout<<"Error opening File\n";
}
else
{
while(!inputFile.eof())
inputFile>>name[count];
count++
}
input(
searchname = (
display(
inputFile.close();
cout << count " is ranked " << Rank <<
}
system("PAUSE");
return 0;
}
int search(const int name[][Max_Girl_Name], int name_input, int targetname, int size_Dimension_1)
{
for(int i = 0;i<size_Dimension_1;i++)
{
int index = 0;
bool found = false;
while ((!found) && (index < number_used))
if (target == name[index])
found = true
else
index++;
if(found)
return index;
else -1;
}
}
我也有几个想法:
- 是否可以使用多维数组来解决这个问题,例如
names[boy_name][girl_name]? - 这个程序的函数是
search(value),input(void),output(void)吗? - 您将如何遍历数组的两个索引?
我希望我足够清楚。
【问题讨论】:
-
@irrelephant 不。甚至没有关闭。
-
不,这只是我已经走了多远。我无法确定该程序将使用哪些类型的函数。
-
语法错误很多。如果您还没有这样做,我建议您使用像 Visual Studio Express 这样的免费 IDE。
-
你熟悉STL吗?关联容器映射对您有意义吗?文件 i/o 可以通过多种方式完成,因为 C++ 是面向对象的语言,您是否考虑过对象 i/o?(例如,使用 iostream_iterator)。知道您需要通过作业确认哪些知识也很有趣。
-
如果你看这些家庭作业的问题足够长,你就会记住它们......如果我是一名老师,我至少会在每个学期把练习混在一起。 “1000 个婴儿名字 C++”会在 SO 搜索中找到。 C++ match string in file and get line number
标签: c++ arrays multidimensional-array