Fraction calculator problems

I am having problems with my faction calculator. The problem is to create a menu for the calculator and to use classes for the fraction. The calculator should add, subtract, multiply, divide, reduce, and convert to decimal. Any help would be nice.

Header file
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
Fraction.h
#pragma once

#include <iostream>
#include <fstream>
using namespace std;

class Fraction{
private:
	int num; //numerator
	int den; //denominator

public:
	Fraction(void); //constructor
	Fraction(int n, int d); //initializong constructor
	~Fraction(void); //destructor
	void print(ostream & out);//print a fraction to an output stream
	void plusEquals(const Fraction & second);//adds second fracton to this fraction
	bool scan(istream & in);//scans a fraction from an input stream
	void minusEquals(Fraction & second);
	void timesEquals(Fraction & second);
	void dividesEquals(Fraction & second);
	void reduce(void);
	double todecimal(void);
};


Fraction
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
Fraction.cpp
#include "Fraction.h"

Fraction::Fraction(void){
	num = 0;
	den = 1;
}


Fraction::~Fraction(void){
	cout << "Destroying fraction" << num << "/" << den << endl;
}


Fraction::Fraction(int n, int d){
	num = n;
	den = d;
}


void Fraction::print(ostream & out){
	out << num << "/" << den;
}


void Fraction::plusEquals(const Fraction & second){
	num = num * second.den + den * second.num;
	den = den * second.den;
}


bool Fraction::scan(istream & in){
	int n,d;
	char slash;
	in >> n >> slash >> d;
	if(slash == '/'){
		num = n;
		den = d;
		return true;
	}
	else{
		return false;
	}
}


void Fraction::minusEquals(Fraction & second){
	num = num * second.den - den * second.num;
	den = den * second.den;
}


void Fraction::timesEquals(Fraction & second){
	num = num * second.num;
	den = den * second.den;
}


void Fraction::dividesEquals(Fraction & second){
	num = num * second.den;
	den = den * second.num;
}


void Fraction::reduce(void){

}


double Fraction::todecimal(void){
	return 0;
}


Main
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
#include "Fraction.h"

int main(){
	int choice;
	Fraction first;
	Fraction second;
	Fraction one;
	cout << "----------------- Fraction Calculator - *********** -----------------\n";
	cout << "1. Add two fractions\n";
	cout << "2. Subtract two fractions\n";
	cout << "3. Multiply two fractions\n";
	cout << "4. Divide two fractions\n";
	cout << "5. Reduce a fraction\n";
	cout << "6. Convert a fraction to a decimal\n";
	cout << "Enter your choice: ";
	cin >> choice;
	if(choice != 5 && choice !=6){
		cout << "Enter the first fraction: ";
		first.scan(cin);
		cout << "Enter the second fraction: ";
		second.scan(cin);
		if(choice > 0){
			switch(choice){
				case '1':
					first.plusEquals(second);
					cout << "The sum of the two fractions is: ";
					first.print(cout);
					cout << endl;
					break;
				case '2':
					first.minusEquals(second);
					cout << "The difference of the two fractions is: ";
					first.print(cout);
					cout << endl;
					break;
				case '3':
					first.timesEquals(second);
					cout << "The product of the two fractions is: ";
					first.print(cout);
					cout << endl;
					break;
				case '4':
					first.dividesEquals(second);
					cout << "The quotient of the two fractions is: ";
					first.print(cout);
					cout << endl;
					break;
				default:
					break;
			}
		}
	}
	else{
		if(choice > 0){
			cout << "Enter the fraction: ";
			one.scan(cin);
			switch(choice){
				case '5':
					one.reduce();
					cout << "The reduced fraction is: ";
					one.print(cout);
					break;
				case '6':
					one.todecimal();
					cout << "The fraction as a decimal is: ";
					one.print(cout);
					break;
				default:
					break;
			}
		}
	}
	
}


When I use the switch, nothing is printed at all except for the destructor. Obviously I haven't finished my reduce or todecimal functions.
Last edited on
Topic archived. No new replies allowed.