我已经更新了计算器代码,并添加了指数函数。 但是,当我尝试获得方程式的答案时,出现以下错误:(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!

===============>>#1 票数:4

将此行添加到您的包括:

#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毫无意义。

  ask by Dtroll MC translate from so

未解决问题?本站智能推荐: