我正在尝试创建一个简单的计算器,使用加法时已经遇到了问题。我创建了一个加法函数,每当传入两个值时,我都会得到不同的答案。例如,当我加4,5时,我希望得到9,但得到的答案是 0029144C 。我仍然是一个初学者,所以起初我不确定将bool类型用于添加函数是否会影响我的结果,但是我将其更改为float类型,并且仍然得到相同的结果(以防有人问)。

#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;

}

最佳答案

makeSum()应该返回浮点数,因为您要返回两个浮点数的总和。

您没有得到正确的结果,因为您正在打印makeSum,这是函数的地址。您要打印makeSum(firstNum, SecondNum)的值。

关于c++ - 创建一个简单的计算器,附加问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50707809/

10-14 16:45
查看更多