Problems with Functions in Class
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Problems with Functions in Class

  1. #1
    Join Date
    Oct 2010
    Posts
    5

    Problems with Functions in Class

    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.

  2. #2
    Join Date
    Oct 2010
    Posts
    5

    Re: Problems with Functions in Class

    This is one of my classes

    #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:isplay( ) {

    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;

    }

    }

  3. #3
    Join Date
    Oct 2010
    Posts
    5

    Re: Problems with Functions in Class

    This is another class

    #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:ivide(Number otherNumber) {

    number = number / otherNumber.Get();

    }

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,301

    Re: Problems with Functions in Class

    Don't you want to edit your posts adding Code tags around code snippets? See Announcement: Before you post....
    Victor Nijegorodov

  5. #5
    Join Date
    Oct 2010
    Posts
    5

    Re: Problems with Functions in Class

    I reformat my class
    Code:
    #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:isplay( ) {
    
    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;
    
    }
    
    }

  6. #6
    Join Date
    Oct 2010
    Posts
    5

    Re: Problems with Functions in Class

    This is another class

    Code:
    #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:ivide(Number otherNumber) {
    
    number = number / otherNumber.Get();
    
    }

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,301

    Re: Problems with Functions in Class

    Quote Originally Posted by giftalp View Post
    I reformat my class
    No, you didn't!
    Please edit your OP adding Code tags instead of copy/paste the unformatted code block in a new one!
    Victor Nijegorodov

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center