C++ Program Problem

Hello, I am trying to create a program that multiplies fractions and gives me a fraction as an answer and here is what I have so far. Also the answer i get is always 0. Any help would be great.

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
#include <iostream>
using namespace std;

void main()
{
	int n1;
	int n2;
	int d1;
	int d2;
	int n;
	int d;
	float f;
	cout<<"Enter numerator 1:";
	cin>>n1;
	cout<<"Enter denominator 1:";
	cin>>d1;
	cout<<"Enter numerator 2:";
	cin>>n2;
	cout<<"Enter denominator 2:";
	cin>>d2;
	n=n1*n2;
	d=d1*d2;
	f=n/d;
	cout<<"Answer = "<<f;
	cin.get();
	system("pause");
	return;
	
}
Last edited on
You say you want the output to be shown as a fraction, but then you output a float; a float is not a fraction. If you want to output something that looks like "2/3" rather than "0.66666666" you will have to do it yourself.
Post your code in the code tags it makes reading easier (the <> sign on the format menu)

Avoid using system("pause"); see here http://www.cplusplus.com/forum/beginner/1988/

main should be int main and not void main. Do you have any specific problem/question regarding this program?
Last edited on
Ok but even with the change to int main what shoud I use to pause it? eg. system ("pause<nul"), getchar(); , or system("read -p \"Press a key to continue...\" -n 1 -s");. Also if I'm not supposed to use float to get a fraction should I do something like Num1/Den1*Num2/Den2 = (Num1*Num2)/(Den1*Den2);
Last edited on
For system alternatives read the thread http://www.cplusplus.com/forum/beginner/1988/ with the reasons to do so

You are supposed to use float for holding fraction, what Moschops means, I think, is you cannot output 2/3, 4/5, 7/9 etc the output will be 0.444, 3.889, .00004 etc.
Well, the answer to why you always get 0 is that dividing 2 integers will never give a non-integer. So 1/2 = 0. and 4/3 = 1. You could see the 'correct' output, at least of what you wrote, by changing line 23 of what you posted to f=(float)n/d, but then you'll see that the results aren't what you want. This is called an explicit cast and makes n be converted to a floating point number before being divided by d. (A float divided by an int will give a float.) More info about type-casting in C++ here: http://www.cplusplus.com/doc/tutorial/typecasting/

For what you want to do, you're going to need to store the numerator and denominator separately all the way through. Once you have them, you should be able to reduce them if you want, or just display them unreduced. (You could do this already by changing line 24 of what you posted to cout<<"Answer = "<< n << "/" << d << endl;) If you're interested in reducing them, the modulo mathematical operation might interest you. It returns the remainder after division, and in C++ is represented by a %. Ex: 4%3 = 1, because the remainder of 4/3 is 1. 6%3 = 0, because the remainder of 6/3 is 0. And so on.

floating point numbers are useful, just not in the case of what you're trying to do. Hope this helps.
Last edited on
Topic archived. No new replies allowed.