Please Help

I am working on an assignment that requires me to display a users full name and multiply two fractions however I do not understand how I get fractions to display. If someone could please help me with this I would greatly appreciate it. Here is what I have done so far:
#include <iostream>

using namespace std;

int main ()
{
string firstname, lastname ;
float fraction1 (1.0/4.0), fraction2 (1.0/2.0), product ;


cout << "Please enter your name: " ;
cin >> firstname >> lastname ;
cout << firstname << " " << lastname << endl ;

product = fraction1 * fraction2 ;
cout << fraction1 << " * " <<fraction2 << " = 1/8 and" << product << endl ;


Here is my problem statement, if this helps, as well:
Write a program that reads a users first and last name. The program should also read and store the numerators and denominators of two fractions as float values. For example, if the numbers 1 and 4 are entered for a fraction, the fraction is 1/4. The program should combine the users first and last names into one string variable and display the whole name. The program should calculate the product of the two fractions and display the result as a fraction and as a decimal value.
Display example:
FirstName LastName
1/4 * 1/2 = 1/8 and 0.125
Last edited on
closed account (zybCM4Gy)
Fractions huh.

Well, last I checked we can't do that....

what you can do instead however is take your decimal... and...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
float decimal = 0.125;
float numerator = 0;
float denominator = 0;

numerator = decimal;
denominator = 1;

numerator *= 1000
demoninator *= 1000

cout << numerator << " \\ " << denominator << endl;

//Of course you'll need to simplify it yourself. 

string firstname;
string secondname;
string fullname = firstname + secondname; //Concatenation ftw



http://www.mathsisfun.com/converting-decimals-fractions.html
Last edited on
You'll have to read and store the numerators and denominators separately.

If you're not required to simplify the fraction (the problem statement doesn't say anything about that), this should actually be fairly easy -- to multiply, you simply multiply the numerators and multiply the denominators, then just display those two values separated by a '/'.
For the decimal value, you just do the same, except you actually divide instead of printing a '/' character.
Thank you guys very much for the help, it took me a little while but I was finally able to work it out :)
Topic archived. No new replies allowed.