2011-12-07 148 views
1

我需要用文件中的數據部分填充2個數組並保持它們平行。但是我現在的代碼給了我看起來像giberish的錯誤。如果有人甚至可以幫我解碼錯誤,我會非常感激。部分從文件中填充數組

代碼:

/***************************************************/ 
/* 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

+0

讓我們有錯誤的文本請。而且,實際上,這種「技術支持」問題在這裏是無關緊要的,但是因爲您標記爲「家庭作業」,所以我會削減一些鬆懈。 –

回答

2
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 ::向量等,我們可以提供一種更加C++的答案。也請避免使用 - using namespace std;

+0

使用命名空間標準差什麼是錯的; ?這就是我被教導使用 –

+0

@SamLaManna那麼你應該給我老師的號碼,所以我可以給他打電話。檢查這個答案:http://cboard.cprogramming.com/cplusplus-programming/117354-why-std-not-using-namespace-std.html – FailedDev

+1

@SamLaManna:再一次突出C++在教學質量普遍較差世界。 [這是爲什麼](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-a-bad-practice-in-c) –