0

我更新了我的计算器代码,并添加了一个指数函数。然而,当我试图得到等式的答案时,我得到了这个错误:(lldb)任何帮助将不胜感激,因为这是我使用 C++ 的第一天!是的,仅此而已!这是我的代码!

#include <math.h>
#include <iostream>
int int1, int2, answer;
bool bValue(true);
std::string oper;
std::string cont;
using namespace std;
std::string typeOfMath;
int a;
int b;
int answerExponent;


int main(int argc, const char * argv[]){

// Taking user input, the first number of the calculator, the operator, and second number. Addition, Substraction, Multiplication, Division
    cout<<"______________________________________________\n";
    cout<<"|Welcome to The ExpCalc! Do you want to do   |\n";
    cout<<"|Exponent Math, or Basic Math(+, -, X, %)    |\n";
    cout<<"|Type in 'B' for basic Math, and'E' for      |\n";
    cout<<"|Exponential Math! Enjoy! (C) John L. Carveth|\n";
    cout<<"|____________________________________________|\n";
    cin>> typeOfMath;
    if(typeOfMath == "Basic" ||
       typeOfMath == "basic" ||
       typeOfMath == "b" ||
       typeOfMath =="B")
    {
        cout << "Hello! Please Type in your first integer!\n";
        cin>> int1;
        cout<<"Great! Now Enter your Operation: ex. *, /, +, -...\n";
        cin>> oper;
        cout<<"Now all we need is the last int!\n";
        cin>> int2;

        if (oper == "+") {
            answer = int1 + int2;
        }
        if (oper == "-") {
            answer = int1 - int2;

        }if (oper == "*") {
            answer = int1 * int2;
        }if (oper == "/") {
            answer = int1 / int2;
        }
        cout<<answer << "\n";
        cout<<"Thanks for Using The ExpCalc!\n";

    }else if(typeOfMath == "Exp" ||typeOfMath == "E" ||typeOfMath == "e" ||typeOfMath == "Exponent"){
        cout<<"Enter the desired Base. Example: 2^3, where 2 is the base.\n";
        cin>> a;
        cout<<"Now what is the desired exponent/power of the base? Ex. 2^3 where 3 is the exponent!\n";
        cin>>b;
        answerExponent = (pow(a,b));
        cout<< answerExponent;
    } else(cout<<"Wrong String!");
}

请帮忙!我可能也会问很多问题,所以请不要生气!我也在使用 Xcode 4 的 Mac 上!

4

1 回答 1

4

将此行添加到您的包含:

#include <string>

完成后,我可以在 Visual Studio 和 GCC 4.7.2 中使用 ideone.com 编译和运行 2^3 的正确输出的代码(单击此处查看输出)。但是,我的编译器仍然会发出警告,因为您可能应该通过转换来处理转换doubleint改变这个:

answerExponent = (pow(a,b));

对此:

answerExponent = static_cast<int>(pow(a,b));

话虽如此,编译器发出该警告是有原因的,通过强制转换,您基本上只是在告诉编译器“闭嘴,无论如何都要这样做”。更好的方法是避免需要演员表。不要进行上述更改,而是更改此行:

int answerExponent;

对此:

double answerExponent;

这更有意义,因为如果您之后要丢弃数字的小数部分,那么pow使用s 作为参数调用就没有什么意义了。double

于 2013-04-17T23:16:11.360 回答