c++上的指数LLDB错误
LLDB Error with Exponents on C++
我已经更新了我的计算器代码,并添加了一个指数函数。然而,当我试图得到方程的答案时,我得到了这个错误:(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的正确输出的代码(点击这里查看输出)。然而,我的编译器仍然发出一个警告,因为从double
到int
的转换,你可能应该通过强制转换来注意。改变这个:
answerExponent = (pow(a,b));
:
answerExponent = static_cast<int>(pow(a,b));
这样说,编译器发出警告是有原因的,通过强制类型转换,你基本上只是告诉编译器"闭嘴,不管怎样都要这样做"。更好的方法是避免使用石膏。不做上面的修改,修改这一行:
int answerExponent;
:
double answerExponent;
这更有意义,因为如果之后要扔掉数字的小数部分,那么用double
s作为参数调用pow
就没有什么意义了。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- 发生错误时如何停止 LLDB
- XCode调试器lldb中断,没有错误输出,但程序运行良好
- 在lldb中使用匿名枚举作为函数参数错误
- c++上的指数LLDB错误