2015-09-27 76 views
-1

我想在C++中創建一個基本控制檯計算器。我在將一個字符串存儲在cin命令的變量中時遇到了一些問題。基本控制檯計算器(在一個變量中存儲字符串)C++

下面是一些澄清程序:

#include <iostream> 
using namespace std; 

int main() 
{ 
    string type_cal; 

    cout << "Please enter the type of calculation you would like to use: \n"; 
    cout << "1. Addition \n"; 
    cout << "2. Subtraction \n"; 
    cout << "3. Multiplication \n"; 
    cout << "4. Division \n \n"; 

    cin >> type_cal; 

    if (type_cal = "Addition" or "1") 
    { 
     int a; 
     int b; 
     int sum; 

     cout << "Please enter a number to add: \n"; 
     cin >> a; 

     cout << "Please enter another number: \n"; 
     cin >> b; 

     sum = a + b; 

     cout << "The sum of those numbers is: " << sum << endl; 

     return 0; 
    } 
} 

目前我在另外相,因爲我最近就遇到了這個問題。快速回答將不勝感激,謝謝!

+0

對於初學者來說,我的編譯器給了我[一個有用的警告(HTTP://coliru.stacked-crooked的.com /一個/ 0084edc5fb537aa7)。 – chris

+0

一個問題是'='不是'=='。一個編譯器警告應該能夠解決這個問題,但是一個好習慣總是把常量放在** left **的任何比較中,這樣如果你錯誤地輸入'=',它總是會編譯失敗。也就是說,如果你犯了同樣的錯誤,''1「== type_cal'就不能編譯。 – Davislor

+0

看起來您已成功從'cin'中讀取一個字符串,並將其放入'type_cal'中。是什麼讓你覺得它沒有被正確讀取? –

回答

1

if(type_cal =「Addition」或「1」)根本沒有意義。

if(type_cal == "Addition" || type_cal == "1") { 
} 
+0

好的,謝謝你,我忘記了如何使用or操作符。 – JDH

0

好的,我發現這個問題,或實際上用作||。在c + +(感謝aerkenemesis),和=是不一樣的==這意味着等於(另一個感謝Lorehed)。程序現在工作正常。

+0

這不是唯一的問題。 'x == y || z'是錯誤的(並且通常源於英語中所有事物的常見錯誤);你需要'x == y || x == z'。 –

+0

謝謝!我一直在試圖找出這個問題一天吧! – JDH

+0

不知道爲什麼,因爲當時每個人都在評論中告訴你,更不用說其他答案(你當時評論過)。 –

0

對於那些誰是好奇,這裏是我的簡單的計算器的新修訂版:

#include <iostream> 
using namespace std; 

float addition(); 
float subtraction(); 
float multiplication(); 
float division(); 

int main() 
{ 
    string type_cal; 

    cout << "Please enter the type of calculation you would like to use: \n"; 
    cout << "1. Addition " << endl; 
    cout << "2. Subtraction " << endl; 
    cout << "3. Multiplication " << endl; 
    cout << "4. Division" << endl << endl; 

    cin >> type_cal; 

    if(type_cal == "Addition") 
    { 
     addition(); 
    } 

    if(type_cal == "Subtraction") 
    { 
     subtraction(); 
    } 

    if(type_cal == "Multiplication") 
    { 
     multiplication(); 
    } 

    if(type_cal == "Division") 
    { 
     division(); 
    } 

    return 0; 
} 



float addition() 
{ 
    float a; 
    float b; 
    float sum; 

    cout << "Please enter a number to add: " << endl; 
    cin >> a; 

    cout << "Please enter another number: " << endl;; 
    cin >> b; 

    sum = a + b; 

    cout << "The sum of those numbers is: " << sum << endl; 
} 




float subtraction() 
{ 
    float c; 
    float d; 
    float difference; 

    cout << "Please enter a number to subtract: \n"; 
    cin >> c; 

    cout << "Please enter another number: \n"; 
    cin >> d; 

    difference = c - d; 

    cout << "The difference of those numbers is " << difference << endl; 
} 




float multiplication() 
{ 
    float e; 
    float f; 
    float product; 

    cout << "Please enter a number to multiply: \n"; 
    cin >> e; 

    cout << "Please enter another number: \n"; 
    cin >> f; 

    product = e * f; 

    cout << "The product of those numbers is " << product << endl; 
} 




float division() 
{ 
    float g; 
    float h; 
    float quotient; 

    cout << "Please enter a number to divide: \n"; 
    cin >> g; 

    cout << "Please enter another number: \n"; 
    cin >> h; 

    quotient = g/h; 

    cout << "The quotient of those numbers is " << quotient << endl; 
} 
相關問題