build error

this is my header file
#ifndef STATS_H // Prevent duplicate definition
#define STATS_H
#include <iostream>

namespace main_stats
{
class statistician
{
public:
// CONSTRUCTOR
statistician();

// MODIFICATION MEMBER FUNCTIONS
void next_number(double nextNum);
void reset();

// CONSTANT MEMBER FUNCTIONS
int length() const {return count; }
double sum() const { return total; }
double mean() const;
double minimum() const;
double maximum() const;

// FRIEND FUNCTIONS
friend statistician operator +(const statistician& s1, const statistician& s2);
friend statistician operator *(double scale, const statistician& s);

private:
int count; // How many numbers in the sequence
double total; // The sum of all the numbers in the sequence
double tinyest; // The smallest number in the sequence
double largest; // The largest number in the sequence
};

// NON-MEMBER functions for the statistician class
bool operator ==(const statistician& s1, const statistician& s2);
}

#endif


-----------------
this is my definition

#include "stats.h"
#include <cassert>

namespace main_stats
{
statistician::statistician()
{
reset();
}

void statistician::next_number(double nextNum)
{
if (count <= 0)
{
count = 1;
total = nextNum;
tinyest = nextNum;
largest = nextNum;
return;
}

else
{
count += 1;
total += nextNum;

if (nextNum < tinyest)
tinyest = nextNum;

if (nextNum > largest)
largest = nextNum;
}

mean();
}

void statistician::reset()
{
count = 0;
total = 0;
tinyest = 0;
largest = 0;

}

double statistician::mean() const
{
assert(count > 0);
return total / count;
}

double statistician::minimum() const
{
assert(count > 0);
return tinyest;
}

double statistician::maximum() const
{
assert(count > 0);
return largest;

}
statistician operator +(const statistician& s1, const statistician& s2)
{
statistician result;

if (s1.length() == 0)
return s2;
if (s2.length() == 0)
return s1;
result.count = s1.length() + s2.length();
result.total = s1.length() * s1.mean() + s2.length() * s2.mean();
if (s1.minimum() < s2.minimum())
result.tinyest = s1.minimum();
else result.tinyest = s2.minimum();
if (s1.maximum() > s2.maximum())
result.largest = s1.maximum();
else result.largest = s2.maximum();
return result;
}
bool operator ==(const statistician& s1, const statistician& s2)
{
if ((s1.length( ) == 0) || (s2.length( ) == 0))
return (s1.length( ) == 0) && (s2.length( ) == 0);
else return (s1.length( ) == s2.length( ))
&& (s1.mean( ) == s2.mean( ))
&& (s1.minimum( ) == s2.minimum( ))
&& (s1.maximum( ) == s2.maximum( ))
&& (s1.sum( ) == s2.sum( ));

}
statistician operator *(double scale, const statistician& s)
{
statistician answer;

answer.count = s.count;
answer.total = scale * s.total;
if (scale >= 0)
{
answer.tinyest = scale * s.tinyest;
answer.largest = scale * s.largest;
}
else
{
answer.tinyest = scale * s.largest;
answer.largest = scale * s.tinyest;
}
return answer;
}
}


but i am getting a weird error:
Description Resource Path Location Type
cannot open output file stats.exe: Permission denied stats C/C++ Problem

please help
Last edited on
this is my test file

//#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string.h> // Provides memcpy function
#include "stats.h"
using namespace main_stats;
using namespace std;

bool close(double a, double b)
{
const double EPSILON = 1e-5;
return (fabs(a - b) < EPSILON);
}

int test1()
{
// Test program for basic statistician functions.
// Returns 62 if everything goes okay; otherwise returns 0.

statistician s, t;
int i;
double r = 0;

if (s.length() || t.length()) return 0;
if (s.sum() || t.sum()) return 0;

for (i = 1; i <= 10000; i++)
{
s.next_number(i);
r += i;
};

if (t.length() || t.sum()) return 0;
if (s.length() != 10000) return 0;
if (!close(s.sum(), r)) return 0;
if (!close(s.mean(), r / 10000)) return 0;

// Reset and then retest everything
s.reset();
t.reset();
r = 0;

if (s.length() || t.length()) return 0;
if (s.sum() || t.sum()) return 0;

for (i = 1; i <= 10000; i++)
{
s.next_number(i);
r += i;
};

if (t.length() || t.sum()) return 0;
if (s.length() != 10000) return 0;
if (!close(s.sum(), r)) return 0;
if (!close(s.mean(), r / 10000)) return 0;

return 62;
}

int test2()
{
// Test program for minimum/maximum statistician functions.
// Returns 7 if everything goes okay; otherwise returns 0.

statistician s, t, u;
double r = 1000;
char n[15] = "10000000000000";

if (s.length() || t.length()) return 0;
if (s.sum() || t.sum()) return 0;

memcpy(&r, n, sizeof(double));
r = 1 / r;
s.next_number(r);
if ((s.minimum() != r) || (s.maximum() != r)) return 0;
r *= -1;
t.next_number(r);
if ((t.minimum() != r) || (t.maximum() != r)) return 0;

u.next_number(100); u.next_number(-1); u.next_number(101); u.next_number(3);
if ((u.minimum() != -1) || (u.maximum() != 101)) return 0;

return 7;
}

int test3()
{
// Test program for + operator of the statistician
// Returns 7 if everything goes okay; otherwise returns 0.

statistician s, t, u, v;

if (s.length() || t.length()) return 0;
if (s.sum() || t.sum()) return 0;

t.next_number(5);
u.next_number(0); u.next_number(10); u.next_number(10); u.next_number(20);

v = s + s;
if (v.length() || v.sum()) return 0;
v = s + u;
if (!(u == v)) return 0;
v = t + s;
if (!(t == v)) return 0;
v = t + u;
if (v.length() != 5) return 0;
if (!close(v.sum(), 45)) return 0;
if (v.minimum() != 0) return 0;
if (v.maximum() != 20) return 0;
if (!close(v.mean(), 45.0 / 5)) return 0;
v = v + t;
if (v.length() != 6) return 0;
if (!close(v.sum(), 50)) return 0;
if (v.minimum() != 0) return 0;
if (v.maximum() != 20) return 0;
if (!close(v.mean(), 50.0 / 6)) return 0;
return 7;
}

int test4()
{
// Test program for * operator of the statistician
// Returns 7 if everything goes okay; otherwise returns 0.

statistician s, t, u;

if (s.length() || t.length()) return 0;
if (s.sum() || t.sum()) return 0;

u.next_number(0); u.next_number(10); u.next_number(10); u.next_number(20);

s = 2 * u;
if (s.length() != 4) return 0;
if (!close(s.sum(), 80)) return 0;
if (s.minimum() != 0) return 0;
if (s.maximum() != 40) return 0;
if (!close(s.mean(), 80.0 / 4)) return 0;

s = -2 * u;
if (s.length() != 4) return 0;
if (!close(s.sum(), -80)) return 0;
if (s.minimum() != -40) return 0;
if (s.maximum() != 0) return 0;
if (!close(s.mean(), -80.0 / 4)) return 0;

s = 0 * u;
if (s.length() != 4) return 0;
if (!close(s.sum(), 0)) return 0;
if (s.minimum() != 0) return 0;
if (s.maximum() != 0) return 0;
if (!close(s.mean(), 0)) return 0;

s = 10 * t;
if (s.length() != 0) return 0;
if (s.sum() != 0) return 0;

return 7;
}

int test5()
{
// Test program for == operator of the statistician.
// Returns 7 if everything goes okay; otherwise returns 0.

statistician s, t, u, v, w, x;

if (s.length() || t.length()) return 0;
if (s.sum() || t.sum()) return 0;

t.next_number(10);
u.next_number(0); u.next_number(10); u.next_number(10); u.next_number(20);
v.next_number(5); v.next_number(0); v.next_number(20); v.next_number(15);
w.next_number(0);
x.next_number(0); x.next_number(0);

if (!(s == s)) return 0;
if (s == t) return 0;
if (t == s) return 0;
if (u == t) return 0;
if (!(u == v)) return 0;
if (w == x) return 0;

return 7;
}

int main()
{
int value = 0;
int result;

cerr << "Running statistician tests:" << endl;

cerr << "TEST 1:" << endl;
cerr << "Testing next, reset, length, sum, and mean (62 points).\n";
result = test1();
value += result;
if (result > 0) cerr << "Test 1 passed." << endl << endl;
else cerr << "Test 1 failed." << endl << endl;

cerr << "\nTEST 2:" << endl;
cerr << "Testing minimum and maximum member functions (7 points).\n";
result = test2();
value += result;
if (result > 0) cerr << "Test 2 passed." << endl << endl;
else cerr << "Test 2 failed." << endl << endl;

cerr << "\nTEST 3:" << endl;
cerr << "Testing the + operator (7 points).\n";
result = test3();
value += result;
if (result > 0) cerr << "Test 3 passed." << endl << endl;
else cerr << "Test 3 failed." << endl << endl;

cerr << "\nTEST 4:" << endl;
cerr << "Testing the * operator (7 points).\n";
result = test4();
value += result;
if (result > 0) cerr << "Test 4 passed." << endl << endl;
else cerr << "Test 4 failed." << endl << endl;

cerr << "\nTEST 5:" << endl;
cerr << "Testing the == operator (7 points).\n";
result = test5();
value += result;
if (result > 0) cerr << "Test 5 passed." << endl << endl;
else cerr << "Test 5 failed." << endl << endl;

cerr << "If you submit the statistician to Dora now, this part of the\n";
cerr << "grade will be " << value << " points out of 90.\n";

system("pause");
return EXIT_SUCCESS;

}
.
Last edited on
how?
Click on my profile page, and click 'Send PM'.
Topic archived. No new replies allowed.