1

Possible Duplicate:
Partially Fill an array from a file

i have no idea what this means. All of my variables line up. I cant seem to find any errors.

I also get the error error:no match for 'operator<<' in 'infile << names' if you repleace names with numoftoys its another error its giving me.

/***************************************************/
/* Author:     Sam LaManna                         */
/* Course:                                         */
/* 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], int &i);    //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
  int i = 0;

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

  instruct();     //Function call to print instructions

  while (!infile.good())
    {
      input (infile, names[i] , numoftoys[i]);
      ++i;
    }

  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], int &i)
{
  infile << names;
  infile << numoftoys;
  infile.ignore ('\n');

  return;
}
Community
  • 1
  • 1
Sam LaManna
  • 425
  • 2
  • 7
  • 15

2 Answers2

3

Because you declared you function to take an array this degenerates into a pointer to the first element.

void input (ifstream &infile, string names [50], int numoftoys[50], int &i);  

Here you are passing a single string and a single int to input() - because names[i] -is a single string in your array.

input (infile, names[i] , numoftoys[i]);

I suggest to change your function like this

void input(ifstream &infile, string &name, int &numoftoy)
{
  infile >> name;
  infile >> numoftoy;
  infile.ignore ('\n');

// return; no need return 
}
Adrian Cornish
  • 23,227
  • 13
  • 61
  • 77
0

You should iterate over the array of string. The reason you get error, because only operator<<(ostream& os, const string& str) is defined, not operator<<(ostream& os, const string* str).

A possible souliton would be iterating over the array with a for statement. Another (little bit more elegant) solution would be using std::for_each.

WebMonster
  • 2,981
  • 2
  • 22
  • 29