2011-03-29 144 views
0

我正在嘗試編寫接受用戶輸入的文件名稱的程序。從那裏它將文件中的數字存儲到數組中,對它們進行排序,然後顯示它們。但是,我得到的數字與訪問超出界限的數組類似,但我可以從調試器中知道我不是。未正確分配數組

#include <iostream> 
using namespace std; 

class TestScores 
{ 
public: 
    TestScores(); 

    TestScores(int scores); 

    ~TestScores(); 

    void AddScore(int newScore); 

    void DisplayArray(); 

    void SortScores(); 

    bool ArraySorted(); 

    int AvgScore(); 

private: 
    int *scoresArray; //Dynamically allocated array 
    int numScores; //number of scores input by user 
    int scoreCounter; 
    const static int default_NumArrays=10; //Default number of arrays 
}; 

#include <iostream> 
#include "TestScores.h" 

TestScores::TestScores() 
{ 
    scoresArray=new int[default_NumArrays]; 
    scoreCounter=0; 
    numScores=default_NumArrays; 
} 

TestScores::TestScores(int scores) 
{ 
    scoresArray=new int[scores]; 
    numScores=scores; 
    scoreCounter=0; 
    for(int i=0; i<scores;i++) 
     scoresArray[i]=0; 
} 

TestScores::~TestScores() 
{ 
    delete[] scoresArray; 
} 

void TestScores::AddScore(int newScore) 
{ 
    if(scoreCounter<numScores){ 
     scoresArray[scoreCounter]=newScore; 
     scoreCounter++; 
    } 
    else 
     cout<<"More scores input than number of scores designated"<<endl; 
} 

void TestScores::DisplayArray() 
{ 
    for(int i=0; i<numScores; i++) 
     cout<<scoresArray[i]<<endl; 
    cout<<endl<<"This is scoresArray"<<endl; 
} 

bool TestScores::ArraySorted() 
{ 
    for(int i=0; i<(scoreCounter-1);i++){ 
     if(scoresArray[i]<=scoresArray[i+1]) 
      continue; 
     else 
      return false; 
    } 
    return true; 
} 

void TestScores::SortScores() 
{ 
    int tempValue; 

    while(ArraySorted()!=true){ 
     for(int i=0; i<(scoreCounter-1); i++){ 
      if(scoresArray[i]<=scoresArray[i+1]) 
       continue; 
      else{ 
       tempValue=scoresArray[i+1]; 
       scoresArray[i+1]=scoresArray[i]; 
       scoresArray[i]=tempValue; 
      } 
     } 
    } 
} 


int TestScores::AvgScore() 
{ 
    int sumScores=0; 

    if(scoreCounter>0){ 
     for(int i=0; i<scoreCounter; i++) 
      sumScores+=scoresArray[i]; 
     return (sumScores/scoreCounter); 
    } 
    else{ 
     cout<<"There are no scores stored."<<endl; 
     return 0; 
    } 
} 

#include "TestScores.h" 
#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 
//Function prototypes 
bool FileTest(ifstream& inData); 
void StoreScores(ifstream& inData, int& newNumScores, TestScores satScores); 

int main() 
{ 
    int newNumScores=0; 
    string inputFile; //Contains name of the user file being used 

    //Opening file stream 
    ifstream inData; 

    //User prompt for input file 
    cout<<"Please enter the file name containing the student scores you wish to " 
     <<"have stored, sorted, and displayed."<<endl; 

    cin>>inputFile; 

    //Opening file streams 
    inData.open(inputFile.c_str()); 

    while(FileTest(inData)==false){ 
     cout<<"I'm sorry, the file you entered was not a valid file. " 
      <<"Please enter another file name, or enter q to exit"<<endl; 
     cin>>inputFile; 
     if(inputFile=="q") 
      return 0; 

     //Opening file streams 
     inData.open(inputFile.c_str()); 
    } 

    inData>>newNumScores; 
    TestScores satScores(newNumScores); //Instantiating TestScores variable 
    StoreScores(inData, newNumScores, satScores); //Storing scores into array 

    satScores.DisplayArray(); 
    satScores.SortScores(); 
    satScores.DisplayArray(); 
    cout<<endl<<"This is the array after sorting"<<endl<<endl; 

    cout<<"This is the average score "<<satScores.AvgScore()<<endl; 

    //Program pauses for user input to continue 
    char exit_char; 
    cout<<"\nPress any key and <enter> to exit\n"; 
    cin>>exit_char; 

    inData.close(); 

    return 0; 
} 


bool FileTest(ifstream& inData) 
{ 
    if(!inData) 
    { 
     cout<<"Your file did not open.\n"; 
     return false; 
    } 
    return true; 
} 


void StoreScores(ifstream& inData, int& newNumScores, TestScores satScores) 
{ 
    int userScore; 
    while(inData>>userScore){ 
     satScores.AddScore(userScore); 
    } 
} 

我的測試文件是random.dat幷包含以下內容:

15 
67 
76 
78 
56 
45 
234 

基礎上,通過調試器看,我可以告訴大家,scoreCounter正確遞增和newScore包含的下一個值,所以爲什麼它不被存儲在數組中?感謝您的幫助

+0

這些大數字到底是什麼?這可能是與[這個問題和我的答案]的問題相關的東西(http://stackoverflow.com/questions/5447002/how-to-read-a-birthday-with-symbols-and-from-a-txt -file/5447085#5447085)以Unicode編碼的文本文件,C++本身無法識別。 – Xeo 2011-03-29 01:26:51

+0

它看起來像這樣15 10位數字# neg。 8位數字# neg 10位數字#和一對更負的10位數字 – ChadM 2011-03-29 01:30:37

+0

@Chad:好的,我用你的代碼做了一個快速測試,得到相同的數字,並在退出應用程序後斷言失敗。在我研究錯誤時請保持穩定。 :) – Xeo 2011-03-29 01:37:44

回答

2

好的,問題很簡單:您將satScores的值傳遞給StoreScores。這隻會填補本地副本,改變StoreScores的簽名如下修正:

void StoreScores(ifstream& inData, int& newNumScores, TestScores& satScores) 

(順便說一句,你不實際使用的newNumScores變量)。 然後作爲預期的輸出:

15 
67 
76 
78 
56 
45 
234 
0 
0 
0 
0 
0 
0 
0 
0 
0 
0 

爲了進一步改進您的代碼,請參閱GMan的評論和Ben的回答。

+0

是啊那工程... btw,我怎麼不使用newNumScores?我用它來調用參數化的構造函數,我認爲用newNumScores定義了數組的數量,這是由15個數組確定的。我可能仍然是錯的 – ChadM 2011-03-29 01:51:06

+0

@Chad:我的意思是在'StoreScores'函數中。 :) – Xeo 2011-03-29 01:51:32

2

您有用戶定義的析構函數,但沒有複製構造函數或賦值運算符。難怪分配不能正常工作,你會得到一個緩衝區的泄漏,而另一個緩衝區是雙重的。按照Rule of Three。或者更好的是,使用已經被調試的容器,如std::vector