Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8790005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:32:40+00:00 2026-06-13T22:32:40+00:00

#include <iostream> using namespace std; class Fraction { private: int num; int denom; public:

  • 0
#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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:32:41+00:00Added an answer on June 13, 2026 at 10:32 pm

    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:

    class Menus
    {
      Fraction a, b, c;
    public:
      void mainMenu();
      void initializingMenu();
      void addingMenu();
      void printingMenu();
    };
    

    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:

    int main() {
        Menus menus;
        menus.mainMenu();
    
        return 0;
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include<iostream> using namespace std; class A { private: const int a=9; public: void display()
#include<iostream> using namespace std; class A { private: int value; public: A(int init):value(init){} void
#include <iostream> using namespace std; class A{ public: A() {std::cout<<A() ;} A(const A& a){cout<<A(const
#include <iostream> using namespace std; struct testarray{ int element; public: testarray(int a):element(a){} }; class
#include <iostream> using namespace std; class A{ int b; public: A(){ cout<<Constructor for class
#include<iostream> using namespace std; class Abc { public: int a; Abc() { cout<<Def cstr
#include <iostream> using namespace std; class Imp { public: int X(int) {return 50;} int
#include <iostream> using namespace std; class B { public: int getMsg(int i) { return
#include <iostream> using namespace std; class dummyA { int x; public: dummyA () {
#include <iostream> using namespace std; class t { public: int health; //its members int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.