Array not being assigned correctly <p>I'm trying to write a program that accepts user input for a file name. From there it stores the numbers in the file into an array, sorts them, then displays them. However, i'm getting large numbers similar to accessing an out-of-bounds array, but I can tell from the debugger i'm not. </p> <pre><code> #include &lt;iostream&gt; 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 &lt;iostream&gt; #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&lt;scores;i++) scoresArray[i]=0; } TestScores::~TestScores() { delete[] scoresArray; } void TestScores::AddScore(int newScore) { if(scoreCounter&lt;numScores){ scoresArray[scoreCounter]=newScore; scoreCounter++; } else cout&lt;&lt;"More scores input than number of scores designated"&lt;&lt;endl; } void TestScores::DisplayArray() { for(int i=0; i&lt;numScores; i++) cout&lt;&lt;scoresArray[i]&lt;&lt;endl; cout&lt;&lt;endl&lt;&lt;"This is scoresArray"&lt;&lt;endl; } bool TestScores::ArraySorted() { for(int i=0; i&lt;(scoreCounter-1);i++){ if(scoresArray[i]&lt;=scoresArray[i+1]) continue; else return false; } return true; } void TestScores::SortScores() { int tempValue; while(ArraySorted()!=true){ for(int i=0; i&lt;(scoreCounter-1); i++){ if(scoresArray[i]&lt;=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&gt;0){ for(int i=0; i&lt;scoreCounter; i++) sumScores+=scoresArray[i]; return (sumScores/scoreCounter); } else{ cout&lt;&lt;"There are no scores stored."&lt;&lt;endl; return 0; } } #include "TestScores.h" #include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;string&gt; using namespace std; //Function prototypes bool FileTest(ifstream&amp; inData); void StoreScores(ifstream&amp; inData, int&amp; 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&lt;&lt;"Please enter the file name containing the student scores you wish to " &lt;&lt;"have stored, sorted, and displayed."&lt;&lt;endl; cin&gt;&gt;inputFile; //Opening file streams inData.open(inputFile.c_str()); while(FileTest(inData)==false){ cout&lt;&lt;"I'm sorry, the file you entered was not a valid file. " &lt;&lt;"Please enter another file name, or enter q to exit"&lt;&lt;endl; cin&gt;&gt;inputFile; if(inputFile=="q") return 0; //Opening file streams inData.open(inputFile.c_str()); } inData&gt;&gt;newNumScores; TestScores satScores(newNumScores); //Instantiating TestScores variable StoreScores(inData, newNumScores, satScores); //Storing scores into array satScores.DisplayArray(); satScores.SortScores(); satScores.DisplayArray(); cout&lt;&lt;endl&lt;&lt;"This is the array after sorting"&lt;&lt;endl&lt;&lt;endl; cout&lt;&lt;"This is the average score "&lt;&lt;satScores.AvgScore()&lt;&lt;endl; //Program pauses for user input to continue char exit_char; cout&lt;&lt;"\nPress any key and &lt;enter&gt; to exit\n"; cin&gt;&gt;exit_char; inData.close(); return 0; } bool FileTest(ifstream&amp; inData) { if(!inData) { cout&lt;&lt;"Your file did not open.\n"; return false; } return true; } void StoreScores(ifstream&amp; inData, int&amp; newNumScores, TestScores satScores) { int userScore; while(inData&gt;&gt;userScore){ satScores.AddScore(userScore); } } </code></pre> <p>My test file is random.dat and contains the following:</p> <pre><code>15 67 76 78 56 45 234 </code></pre> <p>Based on looking through the debugger, I can tell that scoreCounter is incrementing correctly and that newScore contains the next value, so why isn't it being stored in the array? Thanks for the help</p>