Array Question

Oct 31, 2014 at 5:24pm
I am new to programming and I can't figure out why this is only giving me the first number in the file and not filling the array. The file is just 12 numbers and its only printing out the first one. This is just going to be a function in the program but I can't get it to work. Any help is appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
int main()
{
    int i,quizArray[12];
   
    int numOfQuizzes=0;
    
    
    
    ifstream inFile;
    
    inFile.open( "/Users/mikerizza/Desktop/quizScores.txt" );
    
    if ( inFile.fail() )
    {
        cout << "The quizscores.txt input file did not open";
        exit(-1);
    }
    
    
    
    while (inFile.good())
    {
        
        numOfQuizzes++;
        
        for (int i = 0; i < 13; i++)
        
            inFile>> quizArray[i];
    }
    
    inFile.close();
    
    cout<<quizArray[i]<<endl<<numOfQuizzes;


}
Oct 31, 2014 at 5:35pm
1. Since you start from zero index, comparison in you for loop has to be changed from i < 13 to i < 12

2. You are printing only one number (and with bad index). Use for loop to iterate through that array.
Last edited on Oct 31, 2014 at 5:37pm
Oct 31, 2014 at 5:58pm
Thank you I forgot to use the for loop when printing it out. I am only printing it out to make sure its right so I wasn't too worried about the index. The other problem I have is that the numOfQuizzes keeps coming out as 0?
Topic archived. No new replies allowed.