2013-04-17 92 views
1

我今天剛剛啓動了C++,並且正在開發基於高級文本的計算器。無論如何,我正在研究指數,但是當我開始我的程序,並輸入啓動Exponent模式的字符串時,它不會進入expponent模式,只能進入常規計算器模式。這裏是我的代碼:未正確檢測輸入

// 
// 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!"); 
} 

只有幫助提示,請記住這是我第一天用C++。另外如果它有任何幫助,我使用XCode 4!

+0

其他一些技巧,不相關的問題(已經被充分地回答):選擇一個[括號風格(HTTP:/ /en.wikipedia.org/wiki/Indent_style)並堅持不懈; 'std :: cout'(或者''使用std :: cout')比''using namespace std''更'';除非絕對必須避免全局變量(在'main'之前聲明的所有內容都是全局變量);最後,請記住'int/int2'正在執行* integer division *,這可能會給您帶來意想不到的結果(它會在小數點後面截斷所有內容,而不是四捨五入)。 – JBentley

回答

4

你可能想看看你是否表達

if(typeOfMath == "Basic" || "basic" || "b" || "B")

之間的每件事||被評估爲條件。因此,嘗試這樣的:

if(typeOfMath == "Basic" || 
    typeOfMath == "basic" || 
    typeOfMath == "b" || 
    typeOfMath =="B") { 
// do basic 

進行相同的修改對else if(typeOfMath == "Exp" || "E" || "e" || "Exponent")

+0

偉大的答案解決了我的問題。但是現在,當它對指數進行方程時,我會得到一個(lldb)誤差。任何線索? –

+0

@DtrollMC這可能是因爲'answerExponent'是一個'int',而你試圖分配一個'double'? – MysticXG

+0

@DtrollMC改變'answerExponent = double(pow(a,b));'to'answerExponent = static_cast (pow(a,b));'但是請記住,在小數點後你將失去所有數字(在適用的情況下),因爲您正在調用'double'的函數,但將結果存儲在'int'中。爲了獲得更好的結果,將'answerExponent'聲明爲'double'並且執行'answerExponent = pow(a,b);' – JBentley