Function trouble

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
#include <iostream>
#include <iomanip>
int fixzero(int); // prototype
using namespace std;

int main()
{
	int numerator_One; 
	int denominator_One; // first fraction (input)
	int numerator_Two;
	int denominator_Two; // second fraction (input)

	cout << "Enter the numerator of the first fraction: ";
	cin >> numerator_One; // input the numerator for first fraction

	cout << "Enter the denominator of the first fraction: "; 
	cin >> denominator_One; // input the denominator for the first fraction

	cout << "Enter the numerator of the second fraction: ";
	cin >> numerator_Two; //input the numerator for the second fraction

	cout << "Enter the denominator of the second fraction: ";
	cin >> denominator_Two; // input the denominator for the second fraction

	cout << endl;

	fixzero(denominator_One);
	fixzero(denominator_Two);

	float value_One;
	float value_Two;

	value_One = float(numerator_One / denominator_One); //value of the first fraction in decimals

	value_Two = float(numerator_Two / denominator_Two); //value of the second fraction in decimals

	float sum_Decimal = float(value_One + value_Two); 
	float sum_Denominator = float(denominator_One * denominator_Two);
	float new_Numerator_One = float((sum_Denominator / denominator_One) * numerator_One);
	float new_Numerator_Two = float((sum_Denominator / denominator_Two) * numerator_Two);
	float added_Numerator = float(new_Numerator_One + new_Numerator_Two); //this bit of code adds the two fractions

	cout << "The sum of the fractions is " << (int)added_Numerator << " / " << (int)sum_Denominator << " or " << setprecision(3) << sum_Decimal << endl;
	cout << endl;

	float difference_Decimal = float(value_One - value_Two);
	float difference_Denominator = float(denominator_One * denominator_Two);
	float new_Numerator_Three = float((difference_Denominator / denominator_One) * numerator_One);
	float new_Numerator_Four = float((difference_Denominator / denominator_Two) * numerator_Two);
	float subtracted_Numerator = float(new_Numerator_Three - new_Numerator_Four); // this bit of code subtracts the two fractions

	cout << "The difference of the fractions is " << (int)subtracted_Numerator << " / " << (int)difference_Denominator << " or " << difference_Decimal << endl;
	cout << endl;

	float product_Decimal = float(value_One * value_Two);
	float product_Denominator = float(denominator_One * denominator_Two);
	float product_Numerator = float(numerator_One * numerator_Two); // this bit of code multiplies the two fractions

	cout << "The product of the fractions is " << (int)product_Numerator << " / " << (int)product_Denominator << " or " << product_Decimal << endl;
	cout << endl;

	float quotient_Decimal = float(value_One / value_Two);
	float quotient_Denominator = float(denominator_One * numerator_Two);
	float quotient_Numerator = float(numerator_One * denominator_Two); // this bit of code divides the two fractions 

	cout << "The quotient of the fractions is " << (int)quotient_Numerator << " / " << (int)quotient_Denominator << " or " << quotient_Decimal << endl;
	cout << endl;

	if(value_One >= value_Two)
	{
		char response;
		cout << "Is fraction one greater than or equal to fraction two? (y or n): ";
		cin >> response;

		if(response == 'n')
		{
			cout << "Sorry! Fraction one is greater than or equal to fraction two!" << endl;
		}
		if(response == 'y')
		{
			cout << "Correct! Fraction one is greater than or equal to fraction two!" << endl;
		}
	}
	if(value_One < value_Two)
	{
		char response;
		cout << "Is fraction one greater than or equal to fraction two? (y or n): ";
		cin >> response;

		if(response == 'y')
		{
			cout << "Sorry! Fraction one is less than fraction two!" << endl;
		}
		if(response == 'n')
		{
			cout << "Correct! Fraction one is not greater than or equal to fraction two!" << endl;
		}
		
	}
	

}

int fixzero(int number)
{
	if(number == 0)
	{
		return number + 1;
	}
}


Can someone please explain why my function isn't changing the numbers from zero to one?
fixzero returns the result, but you ignore the return code where you call it.

 
denominator_One = fixzero( denominator_One );


is what you want.

By the way....In the function fixzero() u must "return number" before the end of the function. if the number==0, it returns number+1, but if number!=0 the function does not do anything (it is an int function). Maybe your compiler doesn't give u any warning, but it's better to make this change.
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
#include <iostream>
#include <iomanip>
int fixzero(int); // prototype
using namespace std;

int main()
{
	int numerator_One; 
	int denominator_One; // first fraction (input)
	int numerator_Two;
	int denominator_Two; // second fraction (input)

	cout << "Enter the numerator of the first fraction: ";
	cin >> numerator_One; // input the numerator for first fraction

	cout << "Enter the denominator of the first fraction: "; 
	cin >> denominator_One; // input the denominator for the first fraction

	cout << "Enter the numerator of the second fraction: ";
	cin >> numerator_Two; //input the numerator for the second fraction

	cout << "Enter the denominator of the second fraction: ";
	cin >> denominator_Two; // input the denominator for the second fraction

	cout << endl;

	denominator_One = fixzero( denominator_One );
	denominator_Two = fixzero( denominator_Two );

	float value_One;
	float value_Two;

	value_One = float(numerator_One / denominator_One); //value of the first fraction in decimals

	value_Two = float(numerator_Two / denominator_Two); //value of the second fraction in decimals

	float sum_Decimal = float(value_One + value_Two); 
	float sum_Denominator = float(denominator_One * denominator_Two);
	float new_Numerator_One = float((sum_Denominator / denominator_One) * numerator_One);
	float new_Numerator_Two = float((sum_Denominator / denominator_Two) * numerator_Two);
	float added_Numerator = float(new_Numerator_One + new_Numerator_Two); //this bit of code adds the two fractions

	cout << "The sum of the fractions is " << (int)added_Numerator << " / " << (int)sum_Denominator << " or " << setprecision(3) << sum_Decimal << endl;
	cout << endl;

	float difference_Decimal = float(value_One - value_Two);
	float difference_Denominator = float(denominator_One * denominator_Two);
	float new_Numerator_Three = float((difference_Denominator / denominator_One) * numerator_One);
	float new_Numerator_Four = float((difference_Denominator / denominator_Two) * numerator_Two);
	float subtracted_Numerator = float(new_Numerator_Three - new_Numerator_Four); // this bit of code subtracts the two fractions

	cout << "The difference of the fractions is " << (int)subtracted_Numerator << " / " << (int)difference_Denominator << " or " << difference_Decimal << endl;
	cout << endl;

	float product_Decimal = float(value_One * value_Two);
	float product_Denominator = float(denominator_One * denominator_Two);
	float product_Numerator = float(numerator_One * numerator_Two); // this bit of code multiplies the two fractions

	cout << "The product of the fractions is " << (int)product_Numerator << " / " << (int)product_Denominator << " or " << product_Decimal << endl;
	cout << endl;

	float quotient_Decimal = float(value_One / value_Two);
	float quotient_Denominator = float(denominator_One * numerator_Two);
	float quotient_Numerator = float(numerator_One * denominator_Two); // this bit of code divides the two fractions 

	cout << "The quotient of the fractions is " << (int)quotient_Numerator << " / " << (int)quotient_Denominator << " or " << quotient_Decimal << endl;
	cout << endl;

	if(value_One >= value_Two)
	{
		char response;
		cout << "Is fraction one greater than or equal to fraction two? (y or n): ";
		cin >> response;

		if(response == 'n')
		{
			cout << "Sorry! Fraction one is greater than or equal to fraction two!" << endl;
		}
		if(response == 'y')
		{
			cout << "Correct! Fraction one is greater than or equal to fraction two!" << endl;
		}
	}
	if(value_One < value_Two)
	{
		char response;
		cout << "Is fraction one greater than or equal to fraction two? (y or n): ";
		cin >> response;

		if(response == 'y')
		{
			cout << "Sorry! Fraction one is less than fraction two!" << endl;
		}
		if(response == 'n')
		{
			cout << "Correct! Fraction one is not greater than or equal to fraction two!" << endl;
		}
		
	}
	

}

int fixzero(int number)
{
	if(number == 0)
	{
		return number + 1;
	}
	else
	{
		return number;
	}
}


When the quotient_Decimal is printed it says "1.#J" what does this mean?
Last edited on
value_One = float(numerator_One )/float( denominator_One);
value_Two = float(numerator_Two )/float( denominator_Two);

That will solve ur issue.

Regard!


By the way: watch out for the second number; let's say u have 1/2 and 0/2
At this point (1/2) / (0/2)= (1/2) / 0.....so u should call fixzero() again . ;)
Last edited on
Thank you so much Smok006! That's exactly how it was fixed. How did you figure that out?
value_One=float(numerator_One/denominator_One);

First it divides 2 integer numbers (let's say 1/2=0 , 1%2=1) and after that it transforms the result in a float ( 0 =0.0 <-float number). U must transform the 2 number into float before executing a float operation....that's the explanation.
Topic archived. No new replies allowed.