c++上的指数LLDB错误

LLDB Error with Exponents on C++

本文关键字:LLDB 错误 指数 c++      更新时间:2023-10-16

我已经更新了我的计算器代码,并添加了一个指数函数。然而,当我试图得到方程的答案时,我得到了这个错误:(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!");
}

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

添加这一行到你的include:

#include <string>

完成后,我能够使用ideone.com在Visual Studio和GCC 4.7.2中获得编译和运行2^3的正确输出的代码(点击这里查看输出)。然而,我的编译器仍然发出一个警告,因为从doubleint的转换,你可能应该通过强制转换来注意。改变这个:

answerExponent = (pow(a,b));

:

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

这样说,编译器发出警告是有原因的,通过强制类型转换,你基本上只是告诉编译器"闭嘴,不管怎样都要这样做"。更好的方法是避免使用石膏。不做上面的修改,修改这一行:

int answerExponent;

:

double answerExponent;

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