Array counting loop not working
Oct 27, 2012 at 12:47am Oct 27, 2012 at 12:47am UTC
I'm having a problem with the sum counting the data being pulled in from my inputfile. I can display the input file correctly but the sum it not counting right. I have a input file with about 5 rows and 3 columns of data. I need sum to count the second column only. Can anyone help me?
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ifstream fin;
const int MAX_GRADES = 12;
int number[MAX_GRADES];
int maxpoints[MAX_GRADES];
int earned[MAX_GRADES];
int i = 0;
int j = 0;
int k = 0;
int l = 0;
int o = 0;
int sum = 0;
int sum1 = 0;
fin.open("quizinfo.txt" );
if (!fin)
{
cout<< "Input Failed To Open." ;
return 1;
}
for (i = 0; i < 12; i++)
{
sum += number[i];
fin>> number[i] >> maxpoints[i] >> earned[i];
}
for (j = 0; j < 6; j++)
{
cout<< number[k] << " " << maxpoints[l] << " " <<earned[o] << endl;
k++;
l++;
o++;
}
cout<< endl << sum;
return 0;
}
Last edited on Oct 27, 2012 at 3:39am Oct 27, 2012 at 3:39am UTC
Topic archived. No new replies allowed.