Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include<string.h>
#include <iostream>
using namespace std;
class Average{

private:
	void sumFn(int);
	int userValue, sum;
	float avg;

public:
	Average();
	~Average();
	float printAvg();
	void inputVal(int);
};

Average::Average()
{
	/* 
	int uVal[4];
	for (int i = 0; i < 5; i++)
	userValues[i] =  uVal[i] = 00; */
	
	sum = 0;
	userValue = 0;
	avg = 0.0;
}

void Average:: sumFn(int ss){
	sum += ss;
}

Average::~Average()
{
	cout << "***  \nThank you for using Average Software!  ***";
	cout << "\n*** Have a Nice Day  ***!";
}

void Average::inputVal(/* int uVal[4]*/ int uu)
{
	/*for (int i = 0; i < 5; i++){
		userValues[i] = uVal[i];
	}*/
	sumFn(uu);
}

float Average::printAvg()
{
	/*for (int i = 0; i <= 5; i++)

		return (userValues[i]); */
		
	return (avg = (sum/4));
}

int main()
{
	//int v1[4] = 0;
	int v2 = 0;
	//Average av[2]
		Average a2;
	//int uVal[4];

		//a2.~Average();
	
	for (int i = 0; i < 4; i++)
	{
		cout << "\nEnter marks of subject: " << i <<" : ";
		cin >> v2;
		//av[i].inputVal(v2);
		a2.inputVal(v2);

	}// end of for loop

	/* 
	a1.inputVal(v1, v2);
	cout << "\nThe Quotient is:" << d1.printQuote();*/

	cout << "\nThe Average is: " << a2.printAvg()<<endl;
	
	system("pause");

}// end of main

[Edit by Jochen Arndt: Added code block and removed empty lines]
Posted
Updated 13-May-15 2:44am
v2
Comments
Richard MacCutchan 13-May-15 9:07am    
Firstly, please remove all the comment lines that have nothing to do with the problem. And secondly, show us exactly where the error occurs.

This is because you are using the single precision float type. To avoid the error, change all variables, operations and return values to double or cast values.

In your code the error occurs when performing operations with integer arguments that must be converted to float. Integers can represent numbers with more digits than can be hold by single precision floating values. The max. 32-bit signed integer value INT_MAX is 2147483647 which has ten decimal digits. But the mantissa of single precision floating values has only 24 bits (can only hold 6 decimal digits, see definition of FLT_DIG and FLT_MANT_DIG in float.h). So some digits may get loss when converting large integer values to float.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-May-15 10:30am    
5ed.
—SA
You are possibly loosing some decimals. Change from
Quote:
return (avg = (sum/4));

to
C++
return (avg = ((float)sum/4));


It is worthy noting your code could be more concise, e.g.

C++
#include <iostream>
using namespace std;


class Average
{
  int sum, items;
public:
  Average():sum(0), items(0){}
  void add(int v){ sum += v; ++items; }
  double avg()
  {
    if (items==0) throw("sorry, no items");
    return (double)sum/items;
  }
};

int main()
{
  int marks;
  Average a;

  while (true)
  {
    cout << "plaese input marks (-1 to stop): " << endl;
    cin >> marks;
    if ( marks < 0) break;
    a.add(marks);
  };
  try
  {
    cout << "the average is " << a.avg() << endl;
  }
  catch( const char * msg )
  {
    cout << msg << endl;
  }
}
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 13-May-15 10:30am    
5ed.
—SA
CPallini 13-May-15 11:06am    
Thank you, Sergey.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900