the*_*ace -2 c++ initialization
我只是从c转到C++,我正在尝试构建一个计算器.Int'结果'不会通过数学运算初始化.逻辑是,根据操作'',将有一个不同的值分配给'结果'.这似乎不起作用.
#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;
}
Run Code Online (Sandbox Code Playgroud)
Ola*_*che 13
你必须与s不比较's'.所以你的代码应该是这样的
if (s == '*')
{
result = n1*n2;
}
Run Code Online (Sandbox Code Playgroud)
代码
if ('s' == '*')
Run Code Online (Sandbox Code Playgroud)
将字符s与字符进行比较,该字符's'始终为false.
| 归档时间: |
|
| 查看次数: |
131 次 |
| 最近记录: |