2011-12-08 27 views
1

當我打印我的數組時,它會打印,但最後還會有額外的數據。額外數據是應該在那裏的最後一行輸出之後的一行。它包含:」 -13096448" (注意,它開始用一個空格)。數組中額外不需要的數據

/***************************************************/ 
/* Author:  Sam LaManna       */ 
/* Course:           */ 
/* Assignment: Program 6 Elves      */ 
/* Due Date: 12/9/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, int &numoftoys); //Function declaration for getting data from file 
void headers();  //Prints headers 

int main() 
{ 

    string names [50];  //Array for storing names 
    int numoftoys [50];  //Array for storing the number of toys made 
    int i = 0; 
    int p = 0; 

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

    instruct();  //Function call to print instructions 

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

    for (int p = 0; p<i; p++) 
    { 
     cout << names[p] << " " << numoftoys[p] << "\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, int &numoftoys) 
{ 
    infile >> names; 
    infile >> numoftoys; 
    // infile.ignore ('\n'); 

    return; 
} 

數據文件:

Smiley 662 
Curley 88 
Clementine 335 
Jasper 105 
Lucinda 775 
Brunhilda 103 
Florence 441 
Oskar 820 
Snowflake 990 
Bernard 690 
Punch 298 
Chuckie 10 
Frosty 102 
Snowman 311 
April 830 
Merry 299 
Sunshine 331 
Buddy 1234 
Carol 271 
Misty 111 
Harold 52 
Henry 292 
Twinkle 308 
Starlight 703 
Burr 112 
Angelica 444 
Bluenose 689 
Harry 254 
Twinkle 259 
Stardust 121 
Greensleeves 453 
Noel 312 
Happy 209 
Yukon 534 
Snowcap 190 
Northpole 598 
+1

什麼類型的文件是什麼?做什麼的文件中查找的內容類似,如果在記事本中打開? –

+0

@Jim,與數據文件更新 –

+0

您應該打印我終值,以及將它與你期望讀取的行數進行比較,基本上對於一個空文件,你會通過你的閱讀循環一次,我會成爲一個,從而經過輸出循環一次(但沒有數據被讀取)。快速修復:第二個循環停止條件可以是p <(i-1)。 –

回答

1

沒有與輸入迴路多個問題。它可能應該讀一些類似如下:當輸入失敗,當出現在文件的最後一個空行(在這種情況下,最終.eof()測試會取得成功,並

while ((infile >> names[i]) && (infile >> numoftoys[i])) 
{ 
     ++i; 
} 

這避免了處理一個額外的行同時還增加iinput(...)會失敗。

+0

它的工作表示感謝! –

1

檢查,如果轉換成功。

​​

,所以你可以用它喜歡:

if (input(...)) { /* valid data */ } 
+0

爲什麼不簡單地'if((infile >> names)&&(infile >> numoftoys)) '? –

+0

stl文檔不會說錯誤返回null。 – perreal

相關問題