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
#include <iostream>
#include <string>
#include <string>
usingnamespace std;
int main(int argc, constchar * 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;
}
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.