#include <iostream>
using namespace std;
class Fraction {
private:
int num;
int denom;
public:
Fraction() {
num = 0;
denom = 1;
}
Fraction(const Fraction& ref) {
num = ref.num;
denom = ref.denom;
}
Fraction(int arg) {
num = arg;
denom = 1;
}
Fraction(int arg, int arg2) {
num = arg;
if (arg2 == 0)
denom = 1;
else
denom = arg2;
}
~Fraction() {
}
void setnum(int arg) {
num = arg;
return;
}
void setdenom(int arg) {
if(arg) {
denom = arg;
} else {
denom = 1;
}
return;
}
int getnum() const {
return num;
}
int getdenom() const {
return denom;
}
};
int main() {
menu();
return 0;
}
Fraction::Fraction add(Fraction& arg1, Fraction& arg2) {
Fraction temp;
temp.setnum((arg1.getnum() * arg2.getdenom()) + (arg1.getdenom() * arg2.getnum()));
temp.setdenom(arg1.getdenom() * arg2.getdenom());
return temp;
}
Fraction::Fraction init() {
int num;
int denom;
cout << "num: ";
cin >> num;
cout << "denom: ";
cin >> denom;
Fraction info(num, denom);
return info;
}
void print(Fraction& info) {
cout << "num: " << info.getnum() << endl;
cout << "denom: " << info.getdenom() << endl;
return;
}
void menu() {
int option;
do {
cout << "Select an option (use integer value only): ";
cin >> option;
switch(option) {
case 1:
initializingMenu();
break;
case 2:
addingMenu();
break;
case 3:
printingMenu();
break;
case 4:
cout << "Have Fun!" << endl;
break;
default:
cout << "Wrong option!" << endl;
}
} while (option != 0);
return;
}
void initializingMenu() {
Fraction a;
Fraction b;
Fraction c;
int option;
do {
cout << "Select an option (1, 2, or 3): ";
cin >> option;
switch(option) {
case 1:
cout << "\nCalling init() - Stand Alone...\n" << endl;
a = init();
b = init();
break;
case 2:
menu();
break;
default:
cout << "\nWrong option!" << endl;
initializingMenu();
}
} while (option != 0);
return;
}
void addingMenu() {
Fraction a;
Fraction b;
Fraction c;
int option;
do {
cout << "Select an option (1, 2, 3 or 4): ";
cin >> option;
switch(option) {
case 1:
cout << "\nCalling add() - Stand Alone...\n" << endl;
c = add(a,b);
break;
case 2:
menu();
break;
default:
cout << "\nWrong option!" << endl;
addingMenu();
}
} while (option != 0);
return;
}
void printingMenu() {
Fraction a;
Fraction b;
Fraction c;
int option;
do {
cout << "Select an option (1, 2, or 3): ";
cin >> option;
switch(option) {
case 1:
cout << "\nCalling print() - Stand Alone...\n" << endl;
print(c);
break;
case 2:
menu();
break;
default:
cout << "\nWrong option!" << endl;
printingMenu();
}
} while (option != 0);
return;
}
I am making a program to add up two fractions
I am trying to make it with using menu
I have four menus:
main menu: give option to go to other menus
initializing menus: input num and denom
add menu: add up to fractions
print menu: print the added fraction
Trouble:
So I ran the program and went through the menus orderly: main > init > add > print
but when I print, the result is wrong,
for example, 1/2 and 1/2
suppose to give me 2/2
but when I print, it give me 0/1, which the default constructor
I have another program and it was work:
Add two fraction
from the program in the website, I think it worked because I put them in the same menu
but for this one, I think because I separated, so the print didn’t get the num and denom value from the class
So I want to ask, How can I fix this?
and why I couldn’t get the real result when I separated them with menu
Sorry for my poor English and so many codes
You are declaring your fractions as local variables in each function, they have no connection to those declared in other functions. The best approach would be to change the entire menu framework into a class, something like this:
Then, in the menu member functions, use the member variables of the class – these will retain their value, just what you need.
Your main() would then look like this:
I also suggest having mainMenu() run in a loop until quit and having the other menus return to it rather than calling it each time. That just needlessly deepens your call stack.