LLDB Error with Exponents on C++ - 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!");
}
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>
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));
To this:
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;
To this:
double answerExponent;
This makes more sense, because there is little point in calling pow with doubles as arguments if you are going to throw away the fractional part of the number afterwards.

Related

One of my variables in my simple calculator says that it is not being initialized. Why?

I am a noob to C++, and as a part of homework I had to create a simple calculator with four functions within C++. I have done this and it works, however, I am now trying to loop it so that a user can have infinite attempts at using it, however, I am having struggles. Basically, when I run my program and tell the program which operation I'd like to use, it tells me that my variable "sum" is not being initialized. Im not quite sure what this is, or how to fix it. Any ideas? Here is my code -
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
while (true)
{
int num1, num2, r;
double sum;
cout << "Enter a number\n";
cin >> num1;
cout << "Enter another number\n";
cin >> num2;
cout << "Please enter an operator (+ , * , /, - or End)\n";
cin >> r;
if (r == 'End') break;
if (r == '+') sum = num1 + num2;
if (r == '-') sum = num1 - num2;
if (r == '*') sum = num1 * num2;
if (r == '/') sum = num1 / num2;
cout << r;
cout << "The answer is \n" << sum << endl;
system("pause");
return 0;
}
}
If the user enters 'a' as operator for example (something else than the valid choices), sum is never assigned a value, but sum is printed.
As others have said, the variable sum remains uninitialized if the user enters an invalid choice for r. Just set double sum=0; and you're good to go.
In addition, 'End' is not a char, so you can't compare it to r. You'll have to use some other option for ending.
The compiler says that you are trying to use an unintialized variable, sum.
If you think you initialize it, think again: You only assign a value if r is +, -, * or /. But what if r is a? 'End' is not a character, and thus invalid
Then sum is never initialized/has a value, and so the compiler complains.

Can't get if (number == 1) comparison to work

I am working at a vending machine software. I cannot get the if statement to work. It just simply ignores it.
// Program.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
char number;
cout << "Hello. Please choose a drink type ! \n\n1.Coca-Cola \n2.Coca-Cola ZERO \n3.Pepsi\n" ;
cin >> number;
if (number == 1)
cout << "Please tip in 8$";
}
You're comparing a char variable to an int value. You need to change one or the other so they match. Either make number an int:
int number;
...
if (number == 1)
or compare it to the character '1':
char number;
...
if (number == '1')
The distinction here is between the number 1 and the character '1' which has an ASCII value of 49.
As number is a character, you want the comparison to use a character and not the integer value 1. i.e.
if (number == '1')

Getting identifier choice is undefined

I'm working on a calculator console app. I'm a beginner in C++ I want the program to ask a question after I'm done printing the previous result. I get the following error though and I have no idea how to fix it.
identifier "choice" is undefined
Here is my source code:
#include "stdafx.h"
#include <iostream>
int getUserInput() {
std::cout << "Please enter an integer: ";
int value;
std::cin >> value;
return value;
}
int getConversionFormula() {
std::cout << "What do you want to convert? (1 for Meters to feet, 2 for kilometers to miles, 3 for kilagrams to pounds.): ";
int op;
std::cin >> op;
//user might select an invalid operation that isnt there
//Implement a way to avoid this.
return op;
}
int getUserInput2() {
std::cout << "Do you want to convert anything else? (hit 1 for yes, hit 2 for no): ";
int choice; //for the user if they hit yes or no to the question above.
std::cin >> choice;
return choice;
}
int getUserChoice(int choice) {
//If user selects 1 show the converstion formula screen
if (choice == 1)
getConversionFormula();
if (choice == 2)
std::exit;
return -1;
}
int calculateResult(int x, int op) {
//we will use == to compare two variables to see if they are true or not
if (op == 1)
return x * 3.280839895; //meters to feet
if (op == 2)
return x / 1.609344; //km to miles
if (op == 3)
return x * 2.2046; //kg to pounds
return -1; //If the user entered an invalid operation
}
void printResult(int result) {
std::cout << "Your result is: " << result << std::endl;
}
int main()
{
int input1 = getUserInput(); //Gets the users input
int op = getConversionFormula();
int result = calculateResult(input1, op);
printResult(result);
int input2 = getUserInput2(); //Asks the user if they want to convert anything else
int input3 = getUserChoice(input2, choice);
std::cin.clear(); // reset any error flags
std::cin.ignore(32767, '\n'); // ignore any characters in the input buffer until we find an enter character
std::cin.get(); // get one more char from the user
}
Can someone please tell me how I can fix it? I get the error in the main function just to let everyone know.
choice is not declared in main, it's only in getUserInput2, which it returns. getUserChoice doesn't even take two arguments, so just do:
getUserChoice(input2);
You could start chaining your function calls a bit, e.g.: getUserChoice(getUserInput2()); and thus eliminate a few local variables.

Easy Grade Code with parameters

I am trying to make a function that has a int input parameter of a numerical grade and returns a letter grade with either a -, a +, or neither. I know how to do this without the use of reference parameters, but I am trying to use parameters and I am having some difficulty doing so.
One output parameter is the letter grade and the second output parameter is the + or - (+ if missed next grade by 1 or 2 points and - if just made the grade. Here is what I have:
#include <iostream>
using namespace std;
void letterGrade (int, float&, float&);
int main(){
int score;
float letter;
float sign;
cout << "Please input your grade (0-100): ";
cin >> score;
cout << endl;
if (score >=90 && score <=100){
letter == "Letter grade: A";
if (score == 90 ||
score == 91) {
sign = "-";
}
else if (score == 99 ||
score == 98) {
sign == "+";
}
else {
sign == " ";
}
}
letterGrade(score, letter, sign);
return 0;
}
If someone could point me in the right direction that still uses the parameters that would be very helpful. I think my main problem is I can't figure out how you make something = to a float within the if statement.
Thank you for any help or advise you might have!
You need to pass in the location at which the results will be stored.
So the minimal change to the current code would be to pass in an additional reference to a string that stores the results.
void letterGrade (int, float&, float&, std::string&);
Then when you call the function:
std::string results;//results are stored in here
letterGrade(score, letter, sign, results);//results are passed by reference here
Then in the letterGrade function you want to change whatever you were returning to be instead written to the results reference you passed in.
Additionally you are comparing floats with equality. Given that floats are not completely accurately stored in the computer this might end up not giving the behavior you wanted. If you compiled your code with all warnings enabled the compiler would have warned you about this. In future it's a good idea to compile with all warnings as you will quickly get feedback about potential issues. A quick way to fix your code would be to compare with less than and greater than operators. Read this for more info about the floats: https://ece.uwaterloo.ca/~dwharder/NumericalAnalysis/02Numerics/Double/paper.pdf

Refine my code/program where did I go wrong?

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int LordIronhead = 0;
char answer;
cout<<"Is Lord Ironhead present? Y/N.\n";
cin >> answer;
if (answer == 'Y')
{
LordIronhead=0;
}
else if (answer == 'N')
{
LordIronhead= LordIronhead+1;
}
cout<< ""<<LordIronhead<<"\n";
system("PAUSE");
return 0;
}
Every time I run the program and If I answer NO (N)
the result is always 0 instead of 1 (LordIronhead = LordIronhead + 1)
May I know where my error is?
Your code is fine in principle, but you might run into issues with the two-valued logic of 'answer' being checked against 'Y' and against 'N' with no fall-through case. I suspect you are running into EOL or case or character conversion issues, falling through both if's and thereby never changing the Lord.
For showing the problem, try an else statement:
else if (answer == 'N')
{
LordIronhead= LordIronhead+1;
} else {
std::cout << "Invalid answer '" << answer << "'" << std::endl;
}
Your code is correct but is sensitive to the case of user input (it treats user input of N and n differently). You'd remove a possible source of user confusion by converting the input to a known case before checking it. You can do this using either toupper or tolower
cin >> answer;
answer = toupper(answer);
I just tried this myself and found that if I answered N I got the expected answer (1). If I hit n, however, it came back as 0. Are you sure you're hitting N and not n?
Better using 1 and 0 instead of N and Y. Its more recognizable to the system