I have created class menu and class number. I would like that my functions in class do sum, subtract, multiply, and divide. I've done sum, but I have problems with other functions. When I do difference, it is actually sum my numbers, and result of multiplication and division is 0 all the time. Could somebody help me with this problem. I have attached my implementation files and header files.

Recommended Answers

All 10 Replies

I would try to simplify the problem down to < 50 compilable lines that you can post directly to daniweb (using code tags). It is much easier for people to review than having to download your files.

#include "StdAfx.h"
#include <iostream>
#include <string>




 using namespace std;

classMenu::classMenu( void ) {

userMenuSelection = Quit;

} 

classMenu::~classMenu( void ) {
cout << "Destructor ~Menu invoked." << endl;

} 
MenuChoices classMenu::Get( ) {

return userMenuSelection;

} 
void classMenu::Set( MenuChoices newValue ) {

userMenuSelection = newValue;

} 
void classMenu::Display( ) {

cout << "1: Quit" << endl;
cout << "2: Sum"<< endl; 
cout <<	"3: Difference" << endl;
cout << "4: Product" << endl;
cout << "5: Quotient" << endl;

} 
void classMenu::QueryUser( ) {

int selection;

cout << "Enter Menu Selection: ";


cin >> selection;

switch (selection){
case 1: userMenuSelection = Quit;
break;

case 2: userMenuSelection = Sum;
break;

case 3: userMenuSelection = Difference;
break;

case 4: userMenuSelection = Product;
break;

case 5: userMenuSelection = Quotient;
break;

default: userMenuSelection = Quit;
} 

cout << endl;

} 
bool classMenu::Continue( ) {

return userMenuSelection != Quit;

} 
void classMenu::ProcessCommand( ) {

int numA;
int numB;
Number numberA;
Number numberB;
Number numberC(0);

if ( userMenuSelection != Quit ){


cout << "Enter an integer value: ";
cin >> numA;
numberA.Set(numA);

cout << "Enter an integer value: ";
cin >> numB;
numberB.Set(numB);
cout << endl;

switch ( userMenuSelection ) {

case Sum: numberC.Add(numberA);
numberC.Add(numberB);
cout << numberA.Get() << " + " << numberB.Get() << " = "// This part is work
 << numberC.Get() << endl;
break;



case Difference: numberC.Subtract(numberA);
numberC.Subtract(numberB);
cout << numberA.Get() << " - " << numberB.Get() << " = "  // this part does not work //properly
 << numberC.Get() << endl; 
break;
// The rest part does not work at all. Multiplication and Division
case Product: numberC.Multiply(numberA);
	numberC.Multiply(numberB);
	cout << numberA.Get() << " * " << numberB.Get() << " = "
 << numberC.Get() << endl;
break;

case Quotient: numberC.Divide(numberA);
numberC.Divide(numberB);
cout << numberA.Get() << " / " << numberB.Get() << " = "
 << numberC.Get() << endl;
break;

default: cout << "Warning: error state encountered." << endl;
} 

cout << endl;

} 

}
#include "StdAfx.h"
#include <string>
#include <iostream>



using namespace std;

Number::Number( void ) {

cout << "Default constructor invoked: Number( void )." << endl;
number = 0;
} 
Number::Number( int integerValue ) {

cout << "Non-Default constructor invoked: Number( int )." << endl;
number = integerValue;
} 
Number::~Number( void ) {

cout << "Destructor ~Number invoked." << endl;

} 
int Number::Get( ) {

return number;

} 


void Number::Set( int integerValue ) {

number = integerValue;

} 


void Number::Output( ) {
cout << number << endl;
cout << endl;

}
void Number::Add( Number otherNumber ) {

number = number + otherNumber.Get();

}



void Number::Subtract( Number otherNumber ) {
	
	number = number - otherNumber.Get();
}

int Subtract(Number numA, Number numB){
	int answer;
	answer = numA.Get() - numB.Get();
	return answer;
}

void Number::Multiply(Number otherNumber) {
	
	number = number * otherNumber.Get();
}




void Number::Divide(Number otherNumber) {
	
	number = number /  otherNumber.Get();

}

What kind of tags did you use? You should use

[ code ] your code here [ /code ] (without the spaces between the word 'code' and the brackets. Also, if you remove the massive comments the code will be much more manageable for the forum :)

Is it work :)

So, nobody wants to help me? :(

I have done this program, I put it below, so if somebody interested can use it.

My main function

// calcumenu.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


// ================
 // Class Inclusions
 // =====================

 // Include MenuClass.h in the stdafx.h file instead of here.
 // #include "MenuClass.h"
 // =====================

 // =============
 int main( ) {

 // ================================
 // Variable and Object Declarations
 // ================================
      classMenu menu;
 // ===============

 // ======
 // NOTES:
 //
 // 1) Since Display(), QueryUser(), and ProcessCommand() are all
 // members of the Menu class, they are able to obtain the
 // necessary data to be able to perfrom their function directly
 // from the private member variable userMenuSelection. No
 // values need be passed via arguments.
 // 2) Now, and anon, does this main() function behave as the conductor
 // of an orchestra. Never playing an instrument, the conductor,
 // using a little baton, simply directs those playing instruments.
 // And main(), likewise, executes at an abstract level, while the
 // various member-functions invoked by main() perform the detailed,
 // low-level, and intricate tasks of program execution.
 // =======================================================
     do {
         menu.Display();
         menu.QueryUser();
         menu.ProcessCommand();

        } while ( menu.Continue() ); // do ... while loop
// =================================================

    return 0;

} // Function main()
// ====================

Here is my class Menu:

#include "StdAfx.h"
#include <iostream>
#include <string>

// Include Number.h and MenuClass.h in the stdafx.h file instead of here.

// ==================

// ====================
 using namespace std;
// ====================

// ============
// Constructors
// ============

// ===================
// Default Constructor
// ==============================
        classMenu::classMenu( void ) {

           userMenuSelection = Quit;

        } // Constructor Menu
// =====================

// ================
// End Constructors
// ================

// ==========
// Destructor
//
// NOTES:
// 1) The Menu constructor does not dynamically allocate space in the freestore.
// Thus, the destructor does not need to deallocate memory.
// ===========================================================
         classMenu::~classMenu( void ) {
             cout << "Destructor ~Menu invoked." << endl;

         } // Destructor ~Menu
// =====================

// ==============
// End Destructor
// ==============

// ================
// Member-Functions
// ================

// ==============================
// Accessor Member-Function Get()
// ==========================
          MenuChoices classMenu::Get( ) {

                return userMenuSelection;

          } // Accessor Method Get
// ========================

// =============================
// Mutator Member-Function Set()
// =============================================
          void classMenu::Set( MenuChoices newValue ) {

                userMenuSelection = newValue;

          } // Mutator Method Set
// =======================
// ==========================
// Member-Function Display( )
// ============================
          void classMenu::Display( ) {

               cout << "1: Quit" << endl;
               cout << "2: Sum"<< endl; 
               cout <<	"3: Difference" << endl;
               cout << "4: Product" << endl;
               cout << "5: Quotient" << endl;

          } // Member-Function Display
// ============================

// =========================
// Member-Function QueryUser
// ==============================
          void classMenu::QueryUser( ) {

              int selection;

              cout << "Enter Menu Selection: ";

			  cin >> selection;

                  switch (selection){
                         case 1: userMenuSelection = Quit;
                         break;

                         case 2: userMenuSelection = Sum;
                         break;

                         case 3: userMenuSelection = Difference;
                         break;

                         case 4: userMenuSelection = Product;
                         break;

                         case 5: userMenuSelection = Quotient;
                         break;

                         default: userMenuSelection = Quit;
                      } // switch
// ===========

           cout << endl;

          } // Method QueryUser()
// =======================

// =================
// Method Continue()
// =============================
          bool classMenu::Continue( ) {

               return userMenuSelection != Quit;

          } // Method Continue
// ====================

// ==============================
// Member-Function ProcessCommand
// ===================================
          void classMenu::ProcessCommand( ) {

          int numA;
          int numB;
          Number numberA;
          Number numberB;
          Number numberC;

          if ( userMenuSelection != Quit ){
// =====
// Input
// ===================================
             cout << "Enter an integer value: ";
             cin >> numA;
             numberA.Set(numA);

             cout << "Enter an integer value: ";
             cin >> numB;
             numberB.Set(numB);
             cout << endl;
// =============

// ==============================
          switch ( userMenuSelection ) {

                  case Sum: numberC.Add(numberA);
                       numberC.Add(numberB);
                       cout << numberA.Get() << " + " << numberB.Get() << " = "
                       << numberC.Get() << endl;
                  break;

// =============
// STUDENT WORK:
                 case Difference: numberC.Subtract(numberA);
                      numberC.Subtract(numberB);
                      cout << numberA.Get() << " - " << numberB.Get() << " = "
                      << numberC.Get() << endl; 
                 break;

                 case Product:
                      numberC.Multiply(numberA, numberB);
	                  cout << numberA.Get() << " * " << numberB.Get() << " = "
                      << numberC.Get() << endl;
                 break;

                 case Quotient: 
					  cout << numA << " / " << numB << " = " 
					  << numberC.Divide(numA,numB);
                 break;

                 default: cout << "Warning: error state encountered." << endl;
            } // switch
// ===========

         cout << endl;

       } // then

   } // Method ProcessCommand( )
// =============================

// ====================
// End Member-Functions
// =====================

Next, header file for Menu class

#pragma once


// ================================
// Class and Header File Inclusions
// ================================
#include <string>

// Include Types.h in the stdafx.h file instead of here.
// #include "Types.h"
// =================
// ====================
using namespace std;
// ====================
// =================
      class classMenu {
            public:

// ============
// Constructors
// ==================
            classMenu( void );
// ==================
// ==========
// Destructor
// ================
            ~classMenu(void);
// ================

// ==========================
// Member-Function Prototypes
//
// NOTES:
//
// 1) The names for the accessor and mutator member-functions
// will make use of the words Get and Set followed by the
// appropriate member-variable identifier.
// =========================================================
        MenuChoices classMenu::Get( ); // Accessor
        void classMenu::Set( MenuChoices ); // Mutator
        void classMenu::Display( );
        void classMenu::QueryUser( );
        bool classMenu::Continue( );
        void classMenu::ProcessCommand( );
// =========================================
            private:

// ================
// Member-Variables
//
// NOTES:
//
// 1)
// ==============================
              MenuChoices userMenuSelection;
// ==============================
   }; // Class classMenu
// =====================

Following code for implementation file Number.cpp

#include "StdAfx.h"
#include <string>
#include <iostream>



using namespace std;
// ====================

// ============
// Constructors
// ============

// ============================
// Default Constructor Number()
// ============================
Number::Number( void ) {

cout << "Default constructor invoked: Number( void )." << endl;
number = 0;
} // Constructor Number
// =======================

// ================================
// Non-Default Constructor Number()
// ====================================
Number::Number( int integerValue ) {

cout << "Non-Default constructor invoked: Number( int )." << endl;
number = integerValue;
} // Constructor Number
// =========================

// ================
// End Constructors
// ================

// ====================
// Destructor ~Number()
//
// NOTES:
//
// 1) The Number program does not dynamically allocate space in the freestore.
// Thus, the destructor does not need to deallocate memory.
// ===========================================================
Number::~Number( void ) {

cout << "Destructor ~Number invoked." << endl;

} // Destructor ~Number
// =======================

// ==============
// End Destructor
// ==============

// ================
// Member-Functions
// ================

// =======================
// Accessor Function Get()
// ====================
int Number::Get( ) {

return number;

} // Accessor Function Get
// ==========================

// ======================
// Mutator Function Set()
//
// NOTES:
//
// 1) Each time the massn is changed, a new value for the relativistic
// mass must be recalculated.
// ======================================
void Number::Set( int integerValue ) {

number = integerValue;

} // Mutator Function Set
// =========================

// ========================
// Member-Function Output()
// ========================
void Number::Output( ) {
cout << number << endl;
cout << endl;

} // Member-Function Output
// ===========================
// ======================
// Member-Function Add( )
// ========================================
void Number::Add( Number otherNumber ) {

number = number + otherNumber.Get();

} // Member-Function Add()
// ==========================

void Number::Subtract( Number otherNumber ) {

number = -number - otherNumber.Get();

}


void Number::Multiply(Number otherNumberA,
	                  Number otherNumberB) {
	
	
	number = number + otherNumberA.Get()*otherNumberB.Get();
}




int Number::Divide(int a, int b) {
	
	int number = 0;
	
	if (a != 0 && b != 0){
		number = a / b;
		cout << "Division = " << number << endl;
	}
	
	else
		cout << "Cannot divide by zero" << endl;
	return number;
}
	


// ====================
// End Member-Functions
// =====================

And header file for Number:

#pragma once
class Number
{
public:
	// ============
// Constructors
// ===============
Number( void );
Number( int );
// ==============
// ==========
// Destructor
// ===============
~Number( void );
// ===============
// ================
// Member-Functions
//
// NOTES:
//
// 1) The names for the accessor and mutator member-functions
// will make use of the words Get and Set followed by the
// appropriate member-variable identifier. If there is only
// a single member-variable, then Get and Set are all that is
// required.
// ======================================
int Number::Get( ); // Accessor
void Number::Set( int ); // Mutator
void Number::Output();
void Number::Add( Number );

void Number::Subtract( Number );
void Number::Multiply( Number, Number );
int Number::Divide( int a, int b );
// ===========================
private:
// ================
// Member-Variables
//
// NOTES:
//
// 1)
// ===========
int number;
// ===========
}; // Class Number
// ==================

Header file for STDAFX.h

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>
#include "Types.h"
#include "Number.h"
#include "classMenu.h"

There is header file menu types:

// ================
// Enumerated Types
// =============================================================
enum MenuChoices {Quit, Sum, Difference, Product, Quotient };
// =============================================================

I hope it will help any body to get some ideas.

What kind of tags did you use? You should use

[ code ] your code here [ /code ] (without the spaces between the word 'code' and the brackets. Also, if you remove the massive comments the code will be much more manageable for the forum :)

Thank you for your "help" it was very "valuable". :icon_frown:

Your sarcasm is unwarranted. You have to "help us help you". You are not paying for this help, so if it is annoying, no one will help you (as you noticed).

Your sarcasm is unwarranted. You have to "help us help you". You are not paying for this help, so if it is annoying, no one will help you (as you noticed).

First, I think, I have showed some effort. I put my code inside code tags, when you asked. I have cleaned my code from comments, when you asked. Finally I put beginning of my code. And what I have got just "put code tags" and this is it. It was like I sink and you said clean your teeth and then I will help you. I was really needed help. You didn't give me any advice like put ";", if I missed it. It is not annoying, if after this, help follows. Yes, I have not paid for this help, but nobody asked me about money. I think not everything can be paid by money.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.