c++ - 从文件中部分填充数组

标签 c++

我需要用文件中的数据部分填充 2 个数组并使它们保持并行。但是我当前的代码给我的错误看起来像胡言乱语。如果有人甚至可以帮助我解码错误,我将不胜感激。

代码:

/***************************************************/
/* Author:     Sam LaManna                         */
/* Course:     CSC 135 Lisa Frye                   */
/* Assignment: Program 6 Elves                     */
/* Due Date:   11/22/11                            */
/* Filename:   program6.cpp                        */
/* Purpose:    Write a program that will process   */
/*             the work done by santas elfs        */
/***************************************************/

#include <iostream>     //Basic input/output
#include <iomanip>      //Manipulators
#include <string>       //String stuff 
#include <fstream>      //File input/output

using namespace std;

void instruct ();     //Function Declaration for printing instructions 
void input (ifstream &infile, string names [50], int numoftoys[50]);    //Function declaration for getting data from file

int main()
{

  string names [50] = { };       //Array for storing names
  int numoftoys [50] = { };      //Array for storing the number of toys made

  ifstream infile("eleves.dat"); //Opens input file "elves.dat"

  instruct();     //Function call to print instructions

  while (!infile.eof())
    {
      input (names [50], numoftoys [50]);
    }

  cout << names << "\n" << "\n";

  cout << numoftoys << "\n" << "\n";




  return 0;
}




/***************************************************/
/* Name: instruct                                  */
/* Description: Prints instructions to user        */
/* Parameters: N/A                                 */
/* Return Value: N/A                               */
/***************************************************/

void instruct ()                                   
{
  cout << "\n" << "This program will calculate the toys made by santas elfs and assign" << "\n";
  cout << "a rating to each elf. It will also sort them and print average, min and max." << "\n";
  cout << "\n" << "Make sure you have a file named elves.dat in the same directory as";
  cout << "this porgram or you will recieve errors.";
  cout << "\n" << "\n";

  return;
}


/***************************************************/
/* Name: input                                     */
/* Description: Reads from file                    */
/* Parameters: N/A                                 */
/* Return Value: N/A                               */
/***************************************************/

void input (ifstream &infile, string names [50], int numoftoys[50])
{
  infile >> names >> numoftoy;
  infile.ignore ("\n");

  return;
}

错误:

直接链接:http://i.imgur.com/q7I4g.png

最佳答案

istream::operator>> has overloads for neither arrays of strings nor arrays of integers.

您必须一次读取每个字符串和每个整数。最初由 @Seth Carnegie 发布

input (infile, names, numoftoys); //your call was completely wrong

infile.ignore ('\n'); //notice the char instead of string

infile >> names >> numoftoys; //this won't work like this but at least we fixed the declaration error

如果您被允许使用 std::vector 等,我们可以提供更多 C++ 答案。另外请避免使用 - using namespace std;

关于c++ - 从文件中部分填充数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8422105/

相关文章:

c++ - 指针如何引用不同的类型?

c++ - 遍历一个数组

C++ 使用 std::get_time 解析 YYMMDD ISO 8601 日期字符串给出意外结果?

c++ - 为什么我们将对象的引用作为参数传递给重载输出运算符

c++ - 如何确定 Windows 用户是否可以休眠(使用 C++)

c++ - Singleton - 为什么使用类?

c++ - 在 win7 上检测操作系统文本大小

c++ - 绘制 GL_TEXTURE_RECTANGLE 与 GL_TEXTURE_2D

c++ - 在同一指令中引用和取消引用

c++ - CMake 生成的 Xcode 项目无法编译