我想在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;
}
}
目前我在另外相,因爲我最近就遇到了這個問題。快速回答將不勝感激,謝謝!
對於初學者來說,我的編譯器給了我[一個有用的警告(HTTP://coliru.stacked-crooked的.com /一個/ 0084edc5fb537aa7)。 – chris
一個問題是'='不是'=='。一個編譯器警告應該能夠解決這個問題,但是一個好習慣總是把常量放在** left **的任何比較中,這樣如果你錯誤地輸入'=',它總是會編譯失敗。也就是說,如果你犯了同樣的錯誤,''1「== type_cal'就不能編譯。 – Davislor
看起來您已成功從'cin'中讀取一個字符串,並將其放入'type_cal'中。是什麼讓你覺得它沒有被正確讀取? –