Struct def "does not refer to a value"

Attempting to get through Stanfords Programming Abstractions and the solution provided for problem two assignment one will not compile due to this error...


statsT stats;
Error is
statsT does not refer to a value. on line 25

Here is the full code.

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
#include <iostream>
#include <string>
#include <string>
using namespace std;



int main(int argc, const char * argv[])
{

           
    struct statsT {
        int low;
        int high;
        double average;
    };
    /* CalculateStatistics()
     * Usage: stats = CalculateStatistics(filename)
     * --------------------------------------------
     * This function keeps track of the running low/high value
     * as it reads the file, as well as a total and a count to compute
     * the average when we're done
     */
    statsT CalculateStatistics(string filename) {
        statsT stats;
        // Since we know scores are between 0 and 100, we can set low and
        // high to beyond their range.  This way, the first update is
        // just like the rest.
        // Otherwise, we'd need a sentinel and a little more logic
        stats.low = 101;
        stats.high = -1;
        int total = 0;
        int count = 0;
        // Open a new filestream and make sure it worked
        ifstream in;
        in.open(filename.c_str());
        if (in.fail()) Error("Couldn't read '" + filename + "'");
        while(true) {
            int num;
            in >> num;
            // Check that we read successfully
            if (in.fail()) break;
            // Update or data if we need to
            if (num < stats.low) stats.low = num;
            if (num > stats.high) stats.high = num;
            total += num;
            count++;
        }
        // Don't forget to watch for integer division!
        stats.average = double(total)/count;
        // And make sure to close your files
        in.close();
        return stats;
    };
   



    return 0;
}




Here is a link to the assignments
http://see.stanford.edu/see/materials/icspacs106b/assignments.aspx
Any feedback would be helpful.
Last edited on
You cannot define a function inside of another function.
The problem comes from trying to define a function inside of another function, which is illegal.

You must define the function outside of main().
1
2
3
4
5
6
7
8
9
10
11
12
   //Function prototype. Let's everything below know that this function exists
statsT Calculate Statistics(string filename);

int main(){
//...
}

   //Function definition
statsT Calculate Statistics(string filename){
//...
}



Now considering your CalculateStatics() function has to be outside of main(), this means you're going to need to move your statsT struct outside as well.


Random note:
You don't need semi-colons after a function definition.
Welcome, Bdanielz!

I hope you find what you are looking for!
I am glad you are using code tags, because many new users don't. :)
Topic archived. No new replies allowed.