将.text文件读入数组

时间:2012-11-08 09:09:30

标签: c++ arrays multidimensional-array

  

可能重复:
  C++ match string in file and get line number

我已经获得了一个硬件分配,所以我不是要求一整套代码,但可能会提示我想要做什么。作业如下: 包含文本文件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;
    }
}

我也有几点想法:

  1. 这可以使用多维数组来解决,例如names[boy_name][girl_name]吗?
  2. 此计划的功能是search(value)input(void)output(void)吗?
  3. 你将如何重复遍历数组的两个索引?
  4. 我希望我足够清楚。

3 个答案:

答案 0 :(得分:1)

根据您的知识水平,您会遇到阵列。你应该有3个ondimensional数组,就像你已经定义的那样。 (在实际程序中,您将定义一个包含三个项目boysname,girlsname和rank)的类

1.应该是这样的:

int maxLines=1000;
int maNameLength=100; // This would go away later coz you would use std::string when you learnt them
char boysName[maxLines][maxNameLength];
char girlsName[maxLines][maxNameLength];
int rank[maxLines];

(所以这里是你的&#34;多维数组&#34;,这就是女孩和男孩的名字)

2.你是对的,有3种功能可以。输入功能应该是例如逐行读取文件,将其拆分为三个字段并将它们存储在这些数组中。这可以通过各种方式完成,使用std :: library中的高级构造,或者作为初学者,您可能希望手动执行此操作。 (注意文件的行数多于数组大小,或者名称长于maxNameLength)。搜索函数应该通过你的数组并检查名称相等性,如果找到则返回数组索引,否则例如-1

输出函数然后获取此数组索引,首先检查其有效(&gt; = 0和lt maxLines),然后输出等级或未找到等级。)

不要感到沮丧,作为初学者,你有很多工作要做,让这个程序运行起来......

答案 1 :(得分:1)

在提示之前,只提出如何处理事情的建议。直到编码成为你的第二天性,你应该花时间用pseduocode写出你想要的最终代码。然后将行更改为注释,并开始充实您需要满足要求的变量和循环等。通过这样做(a)你变得更有条理,(b)它将允许你记录你的方法。

//The text file babynames2004.txt is included. 

//Contains a list of the 1000 most popular names in the US. It is a space-delimited file of //1000 entries in which the rank is listed first, followed by the corresponding boy name //and girl name. 
const int Max_Name = 1000

//The most popular names are listed first and the least popular names are listed last. 
int rankArray[Max_Name];//a given index in all three corespondss to the row in the file
char boyArray[Max_Name][SomeConstantMaxLengthValue];
char girlArray[Max_Name][SomeConstantMaxLengthValue];

//write a program that allows the user to input a name. 
int main(unsigned int argc, const char** argv )
{
    std::cin >> searchName;

    //The program should then read from the file and search for a matching name among the   
    //girls and boys then store this information in an array. If a match is found, it 
    //should output the rank of the name. The program should also indicate if there is no 
    //match.
    if ( readNameFromFile( searchName , rank, girlName, boyName ) )
    {
        Store( rank,girlName,boyName );
        std::cout << .... //details retireved...
    }
    else
    {
         //Not found - do something helpful
    }
}

readFromFileStore是完成工作的位,考虑通过引用传递参数(google / c ++教科书),以便您可以操作它们在你的功能中。 请参阅Ians提示阅读文件。

您的问题似乎只是希望您存储用户选择的问题,否则您希望搜索文件并选择正确的行。确实,读取所有内容然后存储更有效,但这是您的决定。如果您确实阅读了所有内容,那么您只需要遍历2个名称数组,直到名称匹配为止。然后从排名,女孩和男孩数组中提取详细信息...... 提示 strncmp(...)或者如果你可以使用std::string来表示姓名等等。

std::string boyNames[Max_Name];
std::string girlNames[Max_Name];

它可能更容易,但我猜你需要使用char

需要谨慎选择实际数组,因为名称是任意长度,因此您可能需要考虑如何记录详细信息 - 如果它将是char数组,那么您需要确保存储时的长度等....如果有任何问题,请了解如何继续并回来。

答案 2 :(得分:0)

<强>提示

ifstream有一个名为getline的方法,可让您指定所需的分隔字符。

由于男孩名字和女孩名字彼此之间没有任何特定的关系,你可以将它们存储在完全独立的列表中,但是如果两个列表中都出现名称会怎样?

每个名称都有一个排名,因此需要存储信息,使得两个数据可以相互关联。