array error

i keep getting the error "invalid types for array subscript" in my program.i bolded the line with the error message. sorry its not indented like it should. i took a picture of my program i just don't know how to attach it here.

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{
ifstream fp;
int grade;
string name;

cout<<left<<"Name"<<right<<"Total"<<endl;
cout<<"----------------------------"<<endl;

fp.open("grades.dat");
if(!fp)
{
cout<<"Error opening file\n";
return 0;
}

fp>>name;
for (int i=1; i<=10; i++)
{
cout<<left<<name[i];
}
fp>>grade;
int sum=0;
for(int i=1; i<=10; i++)
{
sum+=grade[i];
}
cout<<right<<sum<<endl;
}
grade is an int but you try to use it as an array
what do i use?
i can't figure out how to get the sum and then how to display it. so i guess theres my problem
you can get one value from ifstream by one step.
so after
 
fp >> name

you get only one name from the file, not all names of one column.
You have to declare grade as an array of int's. When you do

 
fp >> grade;

you are reading a single value. You have to use a loop, reading the values to different positions in your array.
Topic archived. No new replies allowed.