C++, I get an infinite loop when I execute this code -
i have been trying find out problem can not pinpoint it. appreciated:
#include<iostream> #include<fstream> #define infile "input.txt" using namespace std; void readfromfile(int numofpeopleandsalary[6][2], int departmentid[50][6], int &counter, int &totalsalary) { int id, depnum, salary, peoplenum; counter = 0; ifstream myfile; myfile.open(infile); if (myfile.fail()) { cout << "did not read file, there problem" << endl; system("pause"); } else { while (!myfile.eof()) { myfile >> id >> depnum >> salary; numofpeopleandsalary[depnum][0] = numofpeopleandsalary[depnum][0] + 1; numofpeopleandsalary[depnum][1] = numofpeopleandsalary[depnum][1] + salary; peoplenum = numofpeopleandsalary[depnum][0]; departmentid[peoplenum][depnum] = id; totalsalary = totalsalary + salary; counter++; } } } int averagefunction(int counter, int totalsalary, int numofpeopleandsalary[6][2], int departmentid[50][6], int secondaverage[6]) { int average; if (counter == 0) { average = 0; } else { average = totalsalary / counter; } (int = 1; < 6; i++) { if (numofpeopleandsalary[i][0] == 0) { secondaverage[i] = 0; } else { secondaverage[i] = numofpeopleandsalary[i][1] / numofpeopleandsalary[i][0]; } } return average; } int main() { int counter, average, numberofpeeps; int numofpeopleandsalary[6][2] = {0,0,0,0,0,0,0,0,0,0,0,0}; int departmentid[50][6]; int secondaverage[6]; int totalsalary = 0; readfromfile(numofpeopleandsalary, departmentid, counter, totalsalary); average = averagefunction(counter, totalsalary, numofpeopleandsalary, departmentid, secondaverage); (int = 1; < 6; i++) { cout << "department #: " << << " number of people: " << numofpeopleandsalary[i][0] << " total salary: " << numofpeopleandsalary[i][1] << endl; cout << secondaverage[i] << endl; numberofpeeps = numofpeopleandsalary[i][0] + 1; cout << "----------employee id's---------" << endl; (int j = 1; j < numberofpeeps; i++) { cout << departmentid[j][i] << endl; } } system("pause"); return 0; }
when run should read information external file (input.txt) , make calculations. inside external file have these test cases:
23 1 66 6 3 54 213 5 54 434 4 24 324 4 32 789 2 32 4 2 24 345 3 32 568 5 12 24 1 67 789 3 54 24 5 56 56 2 43 9 3 45 32 4 53 67 4 75
you can copy , past if like.
replace i++
j++
in for (int j = 1; j < numberofpeeps; i++)
Comments
Post a Comment