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

Dani AI

Generated

A few practical notes that complement the replies by and and help diagnose why a Visual Studio build for this kind of function might fail even when other projects compile.

Common compile-time causes

  • Missing standard includes (e.g. <fstream>, <string>, <limits>, <stdexcept>, <iostream>) or missing std:: qualifiers.
  • An undefined helper like Error(...) — that is not part of the standard library; replace it with throw std::runtime_error(...) or a simple std::cerr + return.
  • Project settings: the file must have a .cpp extension and the project must compile as C++. Visual Studio projects often enable precompiled headers by default (e.g. stdafx.h or pch.h); failing to include the precompiled header will produce errors.
  • Runtime problems not caught at compile time: empty input files (division by zero when computing average) or non-numeric lines in the file.

A safer pattern (compact example)

#include <fstream>
#include <iostream>
#include <limits>
#include <stdexcept>
#include <string>

struct Stats { int low; int high; double average; };

Stats CalculateStatistics(const std::string& filename) {
    std::ifstream in(filename);
    if (!in) throw std::runtime_error("cannot open " + filename);
    int low = std::numeric_limits<int>::max();
    int high = std::numeric_limits<int>::min();
    long long total = 0;
    int count = 0, v;
    while (in >> v) {
        if (v < 0 || v > 100) continue; // or handle as error
        if (v < low) low = v;
        if (v > high) high = v;
        total += v; ++count;
    }
    if (count == 0) throw std::runtime_error("no valid scores");
    return {low, high, static_cast<double>(total) / count};
}

Practical tips

  • Use while (in >> num) rather than manual fail checks.
  • Prefer std::numeric_limits instead of magic sentinels like 101/−1.
  • Print or copy compiler error messages when asking for help — the exact text is the fastest route to diagnosis (as suggested).
  • If the original assignment used an Error(...) helper, check for a course support file or replace it with standard error handling.

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.