300SRT
300SRT

Reputation: 59

Creating a Simple Calculator, having an issue with the addition

I'm trying to create a simple calculator and i already encountered an issue when addition is being used. I created a function for addition and whenever i pass in two values i get a different answer. For Example when i add 4,5 i would expect to get 9 but the answer i get is 0029144C . Im still a beginner, so at first i wasn't sure if using type bool for the adding function would affect my result, but i changed it to type float and still getting the same result (in case anyone asks).

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


void SimCalcMenu();
void additionSign();
bool makeSum(float num1, float num2);

int main() {

    float firstNum, SecondNum;
    char operationLetter;



    SimCalcMenu();
    cout << " Please Select an Operation You Would Like to Perform ";
    cin >> operationLetter;

    if (operationLetter == 'a' || operationLetter == 'A')
    {
        additionSign();
        cout << " Enter the First Number : ";
        cin >> firstNum;

        cout << " Enter the Second Number: ";
        cin >> SecondNum;

        makeSum(firstNum, SecondNum);


        cout << " The Sum of " << firstNum << " and " << SecondNum << " is :" << makeSum << endl;




    }

    else
    {
        cout << " Error ";
    }



    return 0;
}

void SimCalcMenu() {

    cout << "------------------------------------------------------------------------------" << endl;
    cout << "        WELCOME TO SIM CALCULATOR          " << endl;
    cout << "------------------------------------------------------------------------------" << endl;
    cout << endl;
    cout << " Please Select an Operation :  " << endl;
    cout << " A.) Addition " << endl;
    cout << " B.) Subtraction " << endl;
    cout << " C.) Multiplication " << endl;
    cout << " D.) Division       " << endl;
    cout << " E.) Roots ( Only Positive Number)" << endl;
    cout << " F.) Power ( Only Positive Number " << endl;
    cout << " G.) Percentage                   " << endl;
    cout << " H.) Display functions execution  " << endl;
    cout << " I.) Quit                         " << endl;
    cout << "------------------------------------------------------------------------------" << endl;

}

void additionSign() {

    cout << "------------------------------------------------------------------------------" << endl;
    cout << "        ADDITION          " << endl;
    cout << "------------------------------------------------------------------------------" << endl;




}




bool makeSum(float num1, float num2) {

    float totSum;

    totSum = num1 + num2;

    return totSum;

}

Upvotes: 2

Views: 91

Answers (2)

pm100
pm100

Reputation: 50210

this line

  cout << " The Sum of " << firstNum << " and " << SecondNum << " is :" << makeSum << endl;

IS 'printing' 'makesum', makesum is a function so its printing the address of makesum

you need

  cout << " The Sum of " << firstNum << " and " << SecondNum << " is :" << makeSum(firstNum, SecondNum) << endl;

now at least it will print the result of makesum. As other have pointerd out that function is wrong (it returns a bool).

should be

float makeSum(float num1, float num2) {
    float totSum;
    totSum = num1 + num2;
    return totSum;
}

Upvotes: 4

jdc
jdc

Reputation: 742

makeSum() should return float, because you are returning the sum of two floats.

You are not getting the right result because you are printing makeSum, which is the address of the function. You want to print the value of makeSum(firstNum, SecondNum).

Upvotes: 6

Related Questions