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

Recommended Answers

All 3 Replies

Where is the main function ! you can't run C++ code without the main function

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.