2013-04-17 27 views
0

我更新了我的計算器代碼,並添加了指數函數。然而,當我嘗試獲得方程的答案時,我得到這個錯誤: (lldb) 任何幫助將不勝感激,因爲這是我第一天用C++!是的,就是這樣! 這是我的代碼!在C++上指數的LLDB錯誤

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

+0

你能發佈完整/確切的錯誤消息嗎? – JBentley

+0

完全確切的錯誤信息是(lldb)那就是我給的全部!______________________________________________ |歡迎來到ExpCalc!你想要做什麼|指數數學或基礎數學(+, - ,X,%)| |輸入'B'代表基本數學,'E'代表| |指數數學!請享用! (C)John L. Carveth | | ____________________________________________ | e 輸入所需的基準。例如:2^3,其中2是基數。 現在基地的期望指數/功率是多少?防爆。 2^3其中3是指數! (lldb) –

+1

爲防萬一有些混淆,(lldb)不是錯誤文本,它是調試器的提示符。低級虛擬機DeBugger。類似於GDB。 – Will

回答

4

添加此行到您的包括:

#include <string>

做完這些後,我能得到的代碼編譯並使用正確的輸出2^3上運行,在這兩個Visual Studio中,並在GCC 4.7.2中使用ideone.com(click here查看輸出)。但是,由於從doubleint的轉換,我們的編譯器仍然會發出警告,您應該通過轉換來處理。更改此:

answerExponent = (pow(a,b));

要這樣:

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

雖這麼說,編譯器發出的警告是有原因的,並通過鑄造你基本上只是告訴編譯器「無論如何閉嘴,並做它「。更好的方法是避免需要演員。而不是做上述變化,改變這一行:

int answerExponent;

要這樣:

double answerExponent;

這更有意義,因爲很少有一點呼籲powdouble S作爲參數如果你打算扔掉數字的小數部分。

+0

格麗塔謝謝你!真棒!但如果我可能會問,#include 是如何解決它的? –

+1

@DtrollMC你的代碼使用'std :: string',它是''頭部分的一部分。無論何時你想使用'std :: string',你都必須包含這個頭文件。我無法解釋爲什麼錯誤消息是如此無用 - 雖然我不使用XCode。 – JBentley