Hi frens,
I couldn't compile this solution code given by my university on visual studio.Visual studio works fine with other C++ files.

Here are the question and solution code pasted below.I wonder if anybody could try to compile the solution code on thier machine and see if they can run or not please.I would be very grateful.Thank you in advance:

Problem 2: Files and Structs

When we grade your exams, we’re going to keep track of some statistics like the min,

max and average scores. Define a struct containing these statistics. Then, write a

function that takes a filename, reads the scores from it (one per line where 0 <= score <=

100), and returns the struct you defined. For efficiency’s sake, your function should
make only a single pass over the file


And solution is

Problem 2: Files and Structs

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

Edited 6 Years Ago by WaltP: Added CODE tags -- with all the help about them, how could you miss using them????

Hi frens,
I couldn't compile this solution code given by my university on visual studio.Visual studio works fine with other C++ files.

Why not? Does your machine crash?

When asking for help always give enough information so someone that is not in your class or can't see your screen can understand the problem. Full disclosure -- what happened, what went wrong, what was supposed to happen? At least.

Yes I missed the main function.Hopefully that problem is resolved.cheers.

This question has already been answered. Start a new discussion instead.