我只是从 c 迁移到 C++,并且正在尝试构建一个计算器。Int 'result' 不会被数学运算初始化。逻辑是根据操作's'将分配给'result'不同的值。这似乎不起作用。
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int main ()
{
int n1, n2;
char s,r;
int result = 0;
cout<< "Enter a calculation? (y/n)"<<endl;
cin>>r;
while(r=='y')
{
cout <<"Enter the first number"<<endl;
cin>>n1;
cout<<"Enter the operator"<<endl;
cin>>s;
cout<<"Enter the second number"<<endl;
cin>>n2;
if ('s' == '*')
{
result = n1*n2;
}
if ('s' =='+')
{
result = n1+n2;
}
if ('s' =='-')
{
result = n1-n2;
}
if ('s' =='/')
{
result = n1/n2;
}
cout << result<<endl;
cout<< "Enter a calculation? (y/n)"<<endl;
cin>>r;
}
return 0;
}