String subscript out of range?????

i have this program for an school assignment. the goal here is to open an exicting data file, have it sorted, then add up the total gross for each individual and then get the total gross for the dept. The program works until it opens up and then i get this error message

Degug Assertion Failed

Expression:String Subscript out of range

i have gone over the program at least 100 times and i can not for the life of me find out why the program is doing this? all my arry elements are correct as far as i know, and the strings should work. Im hopeing that some one elses eyes will catch something that i may have missed. Here is the code:


#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void sort(int n);
void swap(string *p1, string *p2);
string lname[5],fname[5],dept[5],hours[5],rate[5];

int main()
{
int i;
double grosspay,totalgross = 0;
string lname,fname,dept,hours,rate;
ifstream fin;
fin.open("e:\data\dept.dat");

for(i=0;i<5;i++)
{
fin>>lname[i]>>fname[i]>>dept[i]>>hours[i]>>rate[i];
}
fin.close();

sort(5);
cout<<"nHere are all the departments sorted and totaled";

for(i=0;i<5;i++)
{
fin>>lname>>fname>>dept>>hours>>rate;
grosspay = atof(hours.c_str()) * atof(rate.c_str());
cout << lname[i]<<"t"<<fname[i]<<"t"<<dept[i]<<"t"<<hours[i]<<"t"<<rate[i]<<"t"<<grosspay<<endl;
}
for(i=0;i<5;i++)
{
grosspay = atof(hours.c_str()) * atof(rate.c_str());
totalgross = totalgross + grosspay;
}
cout<<"Toatl Gross Department Pay = $"<<grosspay;

cout <<"nn";
system("Pause");
return 0;
}

void sort(int n)
{
int i,j,low;
for(i=0;i<n-1;i++)
{
low=i;
for(j=i+1;j<n;j++)
if(dept[j]<dept[low]);
low=j;
if(i !=low);
swap(&lname[i],&lname[low]);
swap(&fname[i],&fname[low]);
swap(&dept[i], &dept[low]);
swap(&hours[i],&hours[low]);
swap(&rate[i], &rate[low]);
}
}
void swap(string *p1, string *p2)
{
string temp = *p1;
*p1 = *p2;
*p2 = temp;
}
You have this string lname[5],fname[5],dept[5],hours[5],rate[5]; global
and later this string lname,fname,dept,hours,rate; local

so what do you want?

This fin>>lname[i]>>fname[i]>>dept[i]>>hours[i]>>rate[i]; reads char's and will crash because the local strings have sizes of 0
Last edited on
dude you are a live SAVER!!!!!!!!!!!!!!!!!! that fixed it. dont know why i could not see it. Thankyou so much...
Topic archived. No new replies allowed.