array error
Dec 2, 2011 at 4:22am UTC
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;
}
Dec 2, 2011 at 4:31am UTC
grade is an int but you try to use it as an array
Dec 2, 2011 at 4:33am UTC
what do i use?
Dec 2, 2011 at 4:34am UTC
i can't figure out how to get the sum and then how to display it. so i guess theres my problem
Dec 2, 2011 at 5:17am UTC
you can get one value from ifstream by one step.
so after
you get only one name from the file, not all names of one column.
Dec 2, 2011 at 5:18am UTC
You have to declare grade as an array of int's. When you do
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.