structure data type program that adds fractions

Can anyone help me with this function I made that adds 2 fractions utilizing the structure data type. I am using microsoft visual c++ 2010 express
my questions are:
>why is it that when I change the type of dDecimalApproximation(within the function multiplyfractions) to long and the cast type of the result of the multiplication of the numerators to long my program outputs 0.0? it works fine as it is right now for double(as well as float).
>why does my program list compiler errors when I change the function multiplyfractions to type void and delete the return 0; at the end of it?
thankyou to anyone who helps

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
#include "stdafx.h"
#include <iostream>

using namespace std;

struct Fraction
{
	int nNum;
	int nDenom;
};

int multiplyfractions(Fraction sFirstFrac, Fraction sSecondFrac)
{
	double dDecimalApproximation = ((double(sFirstFrac.nNum * sSecondFrac.nNum))/((sFirstFrac.nDenom * sSecondFrac.nDenom)));
	cout << "(" << sFirstFrac.nNum << "/" << sFirstFrac.nDenom << ")" << "*" << "(" << sSecondFrac.nNum << "/" << sSecondFrac.nDenom << ") is: " 
		<< (sFirstFrac.nNum * sSecondFrac.nNum) << "/"<< (sFirstFrac.nDenom * sSecondFrac.nDenom) << ", which is approximately equal to: " << dDecimalApproximation
		<< " ." << endl;
	return 0;
}
int main()
{
	cout << "Please enter 2 separate fractions to multiply.\n" << endl;
	
	Fraction sUniqueFirstFrac;
	cout << "Enter the numerator of the first fraction: ";
	cin >> sUniqueFirstFrac.nNum;
	cout << "Enter the denominator of the first fraction: ";
	cin >> sUniqueFirstFrac.nDenom;
	cout << "First fraction is: " << sUniqueFirstFrac.nNum << "/" << sUniqueFirstFrac.nDenom << " .\n" << endl;
	
	Fraction sUniqueSecondFrac;
	cout << "Enter the numerator of the second fraction: ";
	cin >> sUniqueSecondFrac.nNum;
	cout << "Enter the denominator of the second fraction: ";
	cin >> sUniqueSecondFrac.nDenom;
	cout << "Second fraction is: " << sUniqueSecondFrac.nNum << "/" << sUniqueSecondFrac.nDenom << " .\n" << endl;

	cout << multiplyfractions(sUniqueFirstFrac, sUniqueSecondFrac);
	
	return 0;
}
Last edited on
nickcoder wrote:
why is it that when I change the type of dDecimalApproximation(within the function multiplyfractions) to long and the cast type of the result of the multiplication of the numerators to long my program outputs 0.0? it works fine as it is right now for double(as well as float).
Do you understand how integer division works?
nickcoder wrote:
why does my program list compiler errors when I change the function multiplyfractions to type void and delete the return 0; at the end of it?
Because the rest of your program tries to use the return value.
Do you understand how integer division works?

sort of i just started learning c++ a week ago. can you explain further? I don't see why it works for float and double but not long double. I used used the double cast to make one of the terms a double so then when they are divided the int would be promoted to a double, due to the double being higher in the hierarchy, and thus the there is no loss of data when the variable is saved as double. this works completely fine and I get decimal values that i have checked with calculators to confirm are correct. When i do the same thing with float it works correctly as well. When i try to use long double though it doesn't work.
Because the rest of your program tries to use the return value.

why does this affect the program in such a way? why does the function multiplyfractions have to give a return value because i have a return value in other parts. all multiplyfractions does is uses cout and does some division and multiplication. it doesn't return a value that is inputted to an expression like if i didnt have the cout stuff i just returned the value of the final fraction only and did cout << multiplyfractions(x, y) +2 ;(for example) in main().
nickcoder wrote:
sort of i just started learning c++ a week ago. can you explain further? I don't see why it works for float and double but not long double. I used used the double cast to make one of the terms a double so then when they are divided the int would be promoted to a double, due to the double being higher in the hierarchy, and thus the there is no loss of data when the variable is saved as double. this works completely fine and I get decimal values that i have checked with calculators to confirm are correct. When i do the same thing with float it works correctly as well. When i try to use long double though it doesn't work.
I misunderstood - you said long but I see now you meant long double. It works for me: http://ideone.com/HEyBxs
nickcoder wrote:
why does this affect the program in such a way? why does the function multiplyfractions have to give a return value because i have a return value in other parts. all multiplyfractions does is uses cout and does some division and multiplication. it doesn't return a value that is inputted to an expression like if i didnt have the cout stuff i just returned the value of the final fraction only and did cout << multiplyfractions(x, y) +2 ;(for example) in main().
What do you think line 38 does? It uses the return value of the function, and if the function doesn't return anything (return type of void) then that line will cause an error because it tries to use a value that isn't there.
I misunderstood - you said long but I see now you meant long double. It works for me: http://ideone.com/HEyBxs

oh I am sorry it works now. I thought long was an acceptable abbreviation for long double. My mistake.

What do you think line 38 does? It uses the return value of the function, and if the function doesn't return anything (return type of void) then that line will cause an error because it tries to use a value that isn't there.

I thought line 38 just outputs whatever multiplyfractions says. if it returns 0 why does cout <<multiplyfractions(sUniqueFirstFrac, sUniqueSecondFrac); output the sentence and fraction value and not just 0? same thing with the main function i understand if it is of type int it needs to return an integer, but why can't it be a different type of function where it does everything in it without return 0; what is the purpose of the void type function if it returns nothing when can it be used?
Last edited on
nickcoder wrote:
I thought line 38 just outputs whatever multiplyfractions says. if it returns 0 why does cout <<multiplyfractions(sUniqueFirstFrac, sUniqueSecondFrac); output the sentence and fraction value and not just 0? same thing with the main function i understand if it is of type int it needs to return an integer, but why can't it be a different type of function where it does everything in it without return 0; what is the purpose of the void type function if it returns nothing when can it be used?
You have a fundamental misunderstanding of functions. Have a look at this: http://ideone.com/gHRqzJ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int multiply_by_two(int v)
{
	return v * 2;
}
void print_hello_world()
{
	std::cout << "Hello, World!" << std::endl;
}

int main()
{
	print_hello_world();

	std::cout << "The meaning of life is " << multiply_by_two(21) << std::endl;
}













Hello, World!

The meaning of life is 42
By the way: although main looks like a function, it is not a function and should not be treated as such. It just re-uses the syntax of functions so that new syntax does not need to be invented.
Last edited on
Have a look at this: http://ideone.com/gHRqzJ

i see. so void is only used with sentences and can't contain integers and other number that are outside the quotes? in this case int main() contains multiply_by_two(21) = 42 and therefore contains an int and the int main() does not require return 0;? what does return 0; do when it is as the end of an int type "function"?why does it not appear in the console?like if you had int hello(){cout << "hello"; return 0;} then you called it in main with cout << hello(); this only outputs hello and not 0 whereas in you example it outputted 42 the value returned. sorry i see that 0 appears in the console. but why does it is there a way to have it not appear like at the end of my program it says 0press any key to continue
Last edited on
You are still not understanding how functions work.

Don't use the phrase "void function". All functions are created equal (except for main) and a function with return type void can do anything that any other function can do. You can also ignore the return value of functions.

If you don't want that 0 to appear then don't send the return value to output.
You are still not understanding how functions work.

Don't use the phrase "void function". All functions are created equal (except for main) and a function with return type void can do anything that any other function can do. You can also ignore the return value of functions.

If you don't want that 0 to appear then don't send the return value to output.


Ah I see what you mean. Thank you for your help.Now I have a better understanding. I changed multiplyfractions() to type void and instead of using cout << multiplyfractions(); in main() I just called the multiplyfractions() function.
Last edited on
Topic archived. No new replies allowed.