C ++上的指数的LLDB错误 [英] LLDB Error with Exponents on C++
问题描述
我更新了计算器代码,并添加了指数函数。但是,当我试图得到的方程的答案,我得到这个错误:
(lldb)
任何帮助将非常感谢,因为这是我的第一天与C + +!
是的,这就是!
这是我的代码!
I have updated my calculator code, and am adding an exponent function. However, when I try to get the answer to the equation, i get this error: (lldb) Any help would be greatly appreciated, as This is my first day with C++! Yep, that's all! Here's my code!
#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!
Please help! I will probably ask a lot of questions aswell, so please dont get mad! I am also on a Mac, using Xcode 4!
推荐答案
将此行添加到您的包含:
Add this line to your includes:
#include< string>
获取代码编译并运行正确的输出为2 ^ 3,在Visual Studio和GCC 4.7.2使用ideone.com(点击这里查看输出)。但是,我的编译器仍会发出警告,因为从 double
到 int
。更改此:
With that done, I am able to get the code to compile and run with the correct output for 2^3, in both Visual Studio, and in GCC 4.7.2 using ideone.com (click here to see the output). However, my compiler still emits a warning because of a conversion from double
to int
which you should probably attend to by casting. Change this:
answerExponent =(pow(a,b));
到此:
answerExponent = static_cast< int>(pow(a,b));
有了这个说法,编译器会发出这个警告的原因,通过投射你基本上只是告诉编译器关闭, 。一个更好的方法是避免铸造的需要。不要执行上述更改,请更改此行:
With that said, the compiler is emitting that warning for a reason, and by casting you are basically just telling the compiler to "shut up and do it anyway". A better approach is to avoid the need for a cast. Instead of doing the above change, change this line:
int answerExponent;
到此:
double answerExponent;
这样更有意义,因为调用 pow
和 double
s作为参数没有什么意义如果你打算丢弃数字的小数部分后。
This makes more sense, because there is little point in calling pow
with double
s as arguments if you are going to throw away the fractional part of the number afterwards.
这篇关于C ++上的指数的LLDB错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!