Need help on this code

I am having a terrible time in a C++ class right now, and I can't seem to get the right help. Can anyone debug this by chance? 6 errors.




#include <iostream>
#include <iomanip>


using namespace 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;
}
This will now compile without errors.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <iomanip>
#include <fstream> // *** added - required for std::ifstream

using namespace std;

void readData(ifstream& inputFile, int list[], int size);
void holdscrn( ); // void function to hold screen open before exit
void print(int list[], int size) ; // *** added - declare prior to use

int main()
{
    int scores[8] = {0};

    // ifstream infile; // *** modified
    // infile.open("TestScoresData.txt"); // *** modified
    ifstream infile( "TestScoresData.txt" ) ; // *** this is simpler

    // if (infile) // ***
    if( !infile ) // *** corrected - if infile is not in a good state
    {
        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(); // *** this is not really required
    // the file will be automagically closed at the end

    holdscrn( ); // Hold screen before exit

    // return 0; // *** this too is notr essential
    // if there is no return, return 0 is implied
}

// *** the logic in this function is completely wrong
// *** you need to read the scores in the file into the array list
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; // *** removed
}

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;
}


However, the program logic in readData() (and maybe in print() too) is all wrong. Examine what the code is doing (perhaps print out intermediate values - for instance, what does the array contain after readData() returns?) and then make modifications so that it will do what it is supposed to do.

Thanks! That is pretty much where I ended up and is the part I can't figure out. Hopefully someone else can find it.
> Hopefully someone else can find it.

Making a genuine effort to tackle the problem is the simplest way to induce someone else to help you find it.
Topic archived. No new replies allowed.