I'm supposed to be debugging this source file so that it works properly. There are a total of 6 errors in the file and I've found 4 so far. I can't figure out the last two errors. I have an idea of where the next one is but i can't figure it out. I'll include both source files. The original and the one i've debugged so far. Any help would be appreciated in helping me understand this.
// Debug Program -- there are 6 errors in this program
// Correct this program
#include <iostream>
#include <iomanip>
usingnamespace std;
void readData(ifstream& inputFile, int list[], int size);
void holdscrn( ); // void function to hold screen open before exit
int main()
{
int scores[8] = {0};
ifstream infile;
infile.open("TestScoresData.txt");
if (infile)
{
cout << "Cannot open the input file. Program terminates!"
<< endl;
holdscrn( ); // Hold screen before exit
return 1;
}
readData(infile, scores, 8);
print(scores, 8);
cout << endl;
infile.close();
holdscrn( ); // Hold screen before exit
return 0;
}
void readData(ifstream& inputFile, int list[], int size)
{
int score;
int index;
cin >> score;
while (inputFile)
{
index = score / 25;
if (index == size)
index--;
if (index < size)
list[index]++;
inputFile >> score;
}
return 0;
}
void print(int list[], int size)
{
int range;
int lowRange = 0;
int upperRange = 24;
cout << " Range # of Students" << endl;
for (range = 0; range < size; range++)
{
cout << setw(3) << lowRange << " - "
<< upperRange << setw(15)
<< list[range] << endl;
lowRange = upperRange + 1;
upperRange = upperRange + 25;
if (range == size - 2)
upperRange++;
}
cout << endl;
return;
}
void holdscrn( ) // void function to hold screen open before exit
{
char holdscreen;
cout << "\n\n\tEnter one character and press return to exit program: ";
cin >> holdscreen;
return;
}
// Debug Program -- there are 6 errors in this program
// Correct this program
#include <iostream>
#include <iomanip>
#include <fstream> // error 1 no file stream header was included
usingnamespace std;
void readData(ifstream& inputFile, int list[], int size);
void holdscrn( );
void print(int list[], int size); // error 2 void function wasn't delcared
int main()
{
int scores[8] = {0};
ifstream infile;
infile.open("TestScoresData.txt");
if (!infile) // error 3 made it equal to not infile
{
cout << "Cannot open the input file. Program terminates!"
<< endl;
holdscrn( );
return 1;
}
readData(infile, scores, 8);
print(scores, 8);
cout << endl;
infile.close();
holdscrn( );
return 0;
}
void readData(ifstream& inputFile, int list[], int size)
{
int score;
int index;
cin >> score; //I think the next error has something to do with this
//It's not reading the inputFile but waiting for a input
//response from the user which throws off the end total
while (inputFile)
{
index = score / 25;
if (index == size)
index--;
if (index < size)
list[index]++;
inputFile >> score;
}
return; // error 4 was returning a value
}
void print(int list[], int size)
{
int range;
int lowRange = 0;
int upperRange = 24;
cout << " Range # of Students" << endl;
for (range = 0; range < size; range++)
{
cout << setw(3) << lowRange << " - "
<< upperRange << setw(15)
<< list[range] << endl;
lowRange = upperRange + 1;
upperRange = upperRange + 25;
if (range == size - 2)
upperRange++;
}
cout << endl;
return;
}
void holdscrn( )
{
char holdscreen;
cout << "\n\n\tEnter one character and press return to exit program: ";
cin >> holdscreen;
return;
}
Edit: Last minute edit that I thought might be needed maybe? This is the information that is in the resource file that infile is pulling from.
I wasn't given an explanation for what the program is supposed to do. I was just supposed to debug it. From what I have gathered of the program though it is supposed to take the resource file with those numbers at the bottom of my first post and then spit out a table with a varying ranges and the "# of students" who fall within those ranges according to the numbers read in from the resource file.
Edit: So range 1-24 would be 1 student, 25-50 would be 1 student, 51-75 would be 0 students and so on.
I found another error but can't seem to find the 6th one. The program works fine with 5 errors fixed so I'm assuming there is a logic error somewhere but I can't figure it out because from what I can tell the program is running as intended.
// Debug Program -- there are 6 errors in this program
// Correct this program
#include <iostream>
#include <iomanip>
#include <fstream> // error 1 no file stream header was included
usingnamespace std;
void readData(ifstream& inputFile, int list[], int size);
void holdscrn( );
void print(int list[], int size); // error 2 void function wasn't delcared
int main()
{
int scores[8] = {0};
ifstream infile;
infile.open("TestScoresData.txt");
if (!infile) // error 3 made it equal to not infile
{
cout << "Cannot open the input file. Program terminates!"
<< endl;
holdscrn( );
return 1;
}
readData(infile, scores, 8);
print(scores, 8);
cout << endl;
infile.close();
holdscrn( );
return 0;
}
void readData(ifstream& inputFile, int list[], int size)
{
int score;
int index;
inputFile>> score; // error 5 changed cin to inputFile
while (inputFile)
{
index = score / 25;
if (index == size)
index--;
if (index < size)
list[index]++;
inputFile >> score;
}
return; // error 4 was returning a value
}
void print(int list[], int size)
{
int range;
int lowRange = 0;
int upperRange = 24;
cout << " Range # of Students" << endl;
for (range = 0; range < size; range++)
{
cout << setw(3) << lowRange << " - "
<< upperRange << setw(15)
<< list[range] << endl;
lowRange = upperRange + 1;
upperRange = upperRange + 25;
if (range == size - 2)
upperRange++;
}
cout << endl;
return;
}
void holdscrn( )
{
char holdscreen;
cout << "\n\n\tEnter one character and press return to exit program: ";
cin >> holdscreen;
return;
}
Edit: Just wanted to update this just in case anyone was trying to help me so I could let them know I figured it out aside from the allusive 6th error.