数组未正确分配

时间:2011-03-29 01:24:56

标签: c++ visual-studio-2008

我正在尝试编写一个接受文件名用户输入的程序。从那里它将文件中的数字存储到一个数组中,对它们进行排序,然后显示它们。但是,我得到的大数字类似于访问越界数组,但我可以从调试器告诉我不是。

  #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包含下一个值,那么为什么它不存储在数组中呢?谢谢你的帮助

2 个答案:

答案 0 :(得分: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的回答。

答案 1 :(得分:2)

您有一个用户定义的析构函数,但没有复制构造函数或赋值运算符。难怪赋值不能正常工作,你会泄漏一个缓冲区并且双重释放另一个缓冲区。

关注Rule of Three。或者更好的是,使用已经调试过的容器,例如std::vector