Reputation:
I've JUST started C++ today, and I am working on an Advanced Text-based calculator. Anyways, I'm working on exponents, but when I start my program, and enter the string that starts Exponent mode, It doesn't go into expponent mode, only into the regular calculator mode. Here is my Code:
//
// main.cpp
// C++ Calculator
// This is just a basic Calculator Application to be run through the command line.
// v.0.02 - Second version of calculator, basic text, command line interface, loop.
// Created by Johnny Carveth on 2013-04-17.
// Copyright (c) 2013 Johnny Carveth. All rights reserved.
//
#include <math.h>
#include <iostream>
int int1, int2, answer;
bool bValue(true);
std::string oper;
std::string cont;
using namespace std;
std::string typeOfMath;
double a;
double 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" || "basic" || "b" || "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" || "E" || "e" || "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 = double (pow(a,b));
} else(cout<<"Wrong String!");
}
Helpful tips only, remember this is my first day with C++. Also If it is of any help, I'm using XCode 4!
Upvotes: 1
Views: 84
Reputation: 22914
You may want to look at your if expression
if(typeOfMath == "Basic" || "basic" || "b" || "B")
Each thing between the || is evaluated as a conditional. So try something like:
if(typeOfMath == "Basic" ||
typeOfMath == "basic" ||
typeOfMath == "b" ||
typeOfMath =="B") {
// do basic
Make the same modifications for else if(typeOfMath == "Exp" || "E" || "e" || "Exponent")
Upvotes: 4