基本控制台计算器(将字符串存储在变量中)C++; 我试图在C++中创建一个基本控制台计算器。在cin命令的变量中存储字符串时遇到了一些问题
以下是一些澄清的程序:基本控制台计算器(将字符串存储在变量中)C++; 我试图在C++中创建一个基本控制台计算器。在cin命令的变量中存储字符串时遇到了一些问题,c++,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 \
#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;
}
}
#包括
使用名称空间std;
int main()
{
字符串类型;
coutif(type_cal=“Addition”或“1”)根本没有意义
if(type_cal == "Addition" || type_cal == "1") {
}
OK,我发现了这个问题,或者实际上是C++中使用的(谢谢AcKeLeIsIn),和=不等于“===”(另一个是由于LoReHe)。程序现在工作得很好。< / P> < P>对于那些好奇的人来说,这是我的简单计算器的新版本和修订版:
#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;
}
#包括
使用名称空间std;
浮点数加法();
浮点减法();
浮点乘法();
浮点数除法();
int main()
{
字符串类型;
首先,我的编译器给了我一个答案。一个问题是=
不是=
。编译器警告应该注意到这一点,但一个好习惯是总是将常量放在左边的任何比较中,这样,如果你错误地键入=
,它将总是无法编译。也就是说,“1”如果你犯了同样的错误,就不能编译Type,否则你就不能从“代码> CIN <代码>中读取一个字符串,把它放在<代码> Type C++ >代码中。你怎么认为它没有被正确地阅读?谢谢Lorehead,这是我没看到的问题。很好!你忘了告诉任何人问题是什么。好吧,谢谢你,我忘记了如何使用or运算符。这不是唯一的问题。x==y | | z
是错误的(通常源于英语中的一个常见错误);你需要x==y | | x==z
。谢谢你!我一天来一直在想这个问题!不知道为什么,因为评论中的每个人当时都告诉了你,更不用说其他答案(你当时评论过)。