Passing object into object

Hi guys,

Need a little help. I am trying to pass the "sum" object into the "get_average" object, but it's not passing the "sum" member variables "numerator" and "denominator". Instead, the compiler is changing these numbers to so very large ints which don't make sense. If anyone knows what I am doing wrong please let me know.

Just in case it's relevant, the program takes in an arbitrary list of fractions and averages them.

Thanks!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <string>
#include <cctype>//library to use 'toupper', 'tolower'....
#include <vector>
#include <fstream>
using namespace std;
using std::ifstream;
using std::ofstream;
using std::endl;
using std::ios;


const int MAX=10000;

class Fraction//works only for positive fraction
{
public:
	Fraction();
	Fraction(int n, int d);
	void input();
	void print_list(Fraction& f,const int count) const;
	void print_normalized() const;
	void normalize();
	void add_fractions(Fraction& f1,Fraction& f2);
	void get_average(Fraction& f,const int c);

private:
	unsigned int numerator, denominator;

};	
	
	
int main()
{	
	
	char choice;  
	int count=0;
	Fraction fraction[MAX];
	Fraction list, sum(0,0), average;

	do{    
		
		do
		{
			fraction[count].input();		
			fraction[count].normalize();
			fraction[count].print_normalized();

			
			count++;
			cout<<"Another fraction (y/n)? : ";
			cin>>choice;
			
		}while(count<MAX && choice!='n');

		cout<<"\n\nYou have entered the following list of fractions\n\n";
		cout<<"++++++++++++++++++++++++++++++++++++++++\n\n";
		for(int i=0;i<count;i++)
			list.print_list(fraction[i],count);
		cout<<"++++++++++++++++++++++++++++++++++++++++\n\n";
		for(int j=0;j<count;j++)
			sum.add_fractions(sum,fraction[j]);
		cout<<"Sum is: ";
		sum.print_normalized();
		average.get_average(sum,count);//Problem here!!!!!! HERE!!!!!
		average.normalize();
		cout<<"Average is: ";
		average.print_normalized();
		

		cout<<"\n\nDo you wish to get another fraction average? (y/n): ";
		cin>>choice;
	}while(choice=='y');
	cout<<"Have a nice day!!\n";
	exit(1);
	
	_getch();
}
Fraction::Fraction(){}
Fraction::Fraction(int n, int d):numerator(n),denominator(d){}
void Fraction::print_normalized() const
{
	cout<<"Normalized: "<<numerator<<"/"<<denominator<<endl;
}
void Fraction::input()
{
	char slash;
	do
	{
		cout<<"Enter the fraction like 'a/b': ";
		cin>>numerator>>slash>>denominator;
			if(denominator==0)
				cout<<"You noob, denominator can't be zero!!!\n";
	}while(denominator!=0 && slash!='/');
}
void Fraction::print_list(Fraction& f,const int count) const 
{
		cout<<f.numerator<<"/"<<f.denominator<<endl;
}
void Fraction::normalize()
{
	int gcd, num, den, temp;
	num=numerator;
	den=denominator;

	while(num!=0)
	{
		if(num<den)
		{
			temp=num;
			num=den;
			den=temp;
		}
		num=num-den;
	}
	gcd=den;
	numerator=numerator/gcd;
	denominator=denominator/gcd;
}
void Fraction::get_average(Fraction& f, const int c)
{
	unsigned int factor=1000000;
	cout<<"Numerator is: "<<numerator<<endl;
	cout<<"Denomniator is: "<<denominator<<endl;
	double decimal=static_cast<double>(numerator/denominator);
	cout<<"Decmial is: "<<decimal<<"!!!!";
	decimal=decimal/c;
	decimal=decimal*factor;
	numerator=int(decimal);
	denominator=factor;
}
void Fraction::add_fractions(Fraction& f1,Fraction& f2)
{
	if(denominator==0 && numerator==0)
	{
		denominator=f2.denominator;
		numerator=f2.numerator;
	}
	else
	{
		numerator=f1.numerator* f2.denominator+f1.denominator*f2.numerator;
		denominator=f1.denominator*f2.denominator;
	}	
}
On line 125 you'r not using Fraction& f. Since average is created using the standard constructor where numerator and denominator are left unintialzed you will get arbitrary values

EDIT: On line 130 the result is casted to double, but the division is still interger -> no decimal place
Last edited on
line 128,129,130
1
2
3
cout<<"Numerator is: "<<numerator<<endl;
	cout<<"Denomniator is: "<<denominator<<endl;
	double decimal=static_cast<double>(numerator/denominator);

u are directly using numerator,denominator(indirectly this pointer) it uses those variables of the calling object ie average object,
use f1.numerator, f1.denominator instead.
Thanks a lot guys. I am embarassed I did not catch that.

Mike
There is nothing to be embarassed, to error is human:):)
Topic archived. No new replies allowed.