Debug question for class

I have this debug assignment which tells me to locate six errors within the code for the program. My instructor doesn't tell me what the code does once running but I found and fixed all the syntax errors. The only thing that's hanging me up is the three LOGICAL errors. Thanks for the help.

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
  // Test Scores Debug 
// Debug Program -- there are 6 errors in this program
// Correct this program

#include <iostream>
#include <iomanip>
#include<fstream>

using namespace std;

const int ARRAY_SIZE = 8; 
void readData(ifstream& inputFile, int list[], int size);
void holdscrn( );    // void function to hold screen open before exit
void print (int list[], int size);

int main()
{
    int scores[ARRAY_SIZE] = {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 index = 0;
    int score;
    
	while (inputFile)
	{
		index = (score / 25);

		if (index < size)
			cout << index--;
			cout << endl;
		if (index > size)
			cout << list[index]++;
            cout << endl;
		inputFile >> score;
	}
    return;
}

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

I can't do your homework for you. The errors will become obvious if you run the program. Try running the program with, and without an input file.
Last edited on
omg... I'm going to love CS one if this is what it's going to be like, rofl...
first programming class. Didn't ask you to do the homework for me, despite the fact I have been staring at it for two days so its not like I just asked someone to do it for me. thanks anyway.
You first need to establish what is wrong with the program - what bad output is there, or does it seg fault? Try & work out which part of the input might be causing it, if possible.

Then, the best thing to do is learn how to use the debugger on your system. This is a different thing to the compiler & linker. It should allow you to have a watch list of variables, set break points, step through the code 1 line at a time. You can see how the values change & deduce where & why things go wrong.

The other way which is much harder & longer & worse, is to put cout statements everywhere, to see where the values go wrong. Much easier to use a proper debugger.

BTW - staring at code for 2 days is no way to debug, because there is nothing wrong with the code compilation wise - the most low tech way is to track the value of the variables with pencil & paper.

I wouldn't bother with return statements at the end of void functions, but they are handy if you want to exit the function early. I know this code was given to you all, and my advice won't make any difference in this case, - maybe it is something you can ask your teacher about?

Hope all goes well 8+)
I find that having it display it's status helps a lot. So, run it a few times, and find where you are being troubled. If you can't find it, you'll have to start from the top. If you find a specific area, it makes things easier. Just have it display the variables (or all of them, whichever) that are supposed to be 'wrong' as it executes (and pause after each display if there's no runtime error). I find that this works best for me, especially since my programs can easily become thousands of lines long, and manually tracking them is tedious and tirsome.
Last edited on
Topic archived. No new replies allowed.