Debugging

closed account (L6Rz8vqX)
Hi. I am having difficulty with seeing or interpreting the errors in this program. Any help with showing how/what needs to be fixed would be appreciated!

Thank you!

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
#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;
}
Try compiling it. Your compiler should give you error messages to help you debug.

But here are a few major issues:
1. You need to declare functions and variables before you can use them.
2. ifstream needs the header #include <fstream>
3. Your function readData is a void function yet it returns a value.
Last edited on
1. #include <fstream>
2. Remove line 54. Can't return a value in a function declared to return void.
3. Forward declare void print(int list[], int size) like you did with your other functions.
closed account (L6Rz8vqX)
Thank you for the help!

Booradley, could you please explain what you mean by "forward declare"? Sorry to be so obtuse about it!
He meant function prototype.

http://opencbp.sourceforge.net/en_US.ISO8859-1/books/opencbook/func.prototypes.html
or
http://en.wikipedia.org/wiki/Function_prototype

You can have function declaration, or prototype and declaration. Prototype is a declaration of function before implementing it(writing function code).
It looks like this:
1
2
3
4
5
6
7
void holdscrn();//Now we know that function holdscrn() exists and what it looks like.
//some code...

void holdscrn()
{
 //code for function
}


Function prototypes are broadly used because it's just easier to have code in one place(usually at the end of file or in other file), and declarations at the beginning. It kind of looks like table of contents in book ;)

Cheers!
Topic archived. No new replies allowed.