猿问

运行结果不对,哪里出错了?

#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace std;
int main()
{
 int n; int a; int b; int c;
 cout << " 1.加法运算\n";
 cout << " 2.减法运算\n";
 cout << " 3.乘法运算\n";
 cout << " 4.除法运算\n";
 cout << "Please select a type:\n";
 cin >> n;
 if (n = 1)
 {
  cout << "请输入两个数:\n";
  cin >> a;
  cin >> b;
  c = a + b;
  cout << "c=" << c << endl;
 }
 if (n = 2)
 {
  cout << "请输入两个数:\n";
  cin >> a;
  cin >> b;
  c = a - b;
  cout << "c=" << c << endl;
 }
 if (n = 3)
 {
  cout << "请输入两个数:\n";
  cin >> a;
  cin >> b;
  c = a * b;
  cout << "c=" << c << endl;
 }
 if (n = 4)
 {
  cout << "请输入两个数:\n";
  cin >> a;
  cin >> b;
  c = a / b;
  cout << "c=" << c << endl;
 }
 system("pause");
}

TiAmo_YF
浏览 790回答 1
1回答

AAnonymous

if (n = 1)it will always be TRUE, cause it's assignment statement,  n will be 1 which is not 0 (TRUE).you should use ` if (n == 1) `also ` n == 2 / 3 / 4 `
随时随地看视频慕课网APP
我要回答