使用c++;代码 我学习C++,并用IF创建一个小型计算器,else if语句。程序应该是这样工作的。首先,你需要选择像加法或减法这样的运算,然后输入第一个参数,然后输入第二个参数,然后得到一个结果。我写了代码,但它没有正确显示地址。答案显示出与我预期不同的结果。倍数和除法给我正确的结果,但添加并将其减错。请帮忙 #include<iostream> using namespace std; int main() { int a = 1, b = 2, c = 3, d = 4; int first_argument, second_argument; cout << "Please choose the number: " <<endl<< "1.Multiple"<<endl<<"2.Divide" <<endl<<"3.Substract"<<endl<<"4.Add" << "\n"; cin >> a,b,c,d; cout << "Please choose the first argument" << "\n"; cin >> first_argument; cout << "Please choose the second argument" << "\n"; cin>>second_argument; if (a==1) cout <<"The answer is "<<first_argument*second_argument<< "\n"; else if (b == 2) cout << "The answer is " << first_argument / second_argument << "\n"; else if (c == 3) cout << "The answer is " << first_argument - second_argument << "\n"; else if (d == 4) cout << "The answer is " << first_argument + second_argument << "\n"; cin.ignore(); cin.get(); return 0; } #包括 使用名称空间std; int main() { INTA=1,b=2,c=3,d=4; int第一个参数,第二个参数; 库特

使用c++;代码 我学习C++,并用IF创建一个小型计算器,else if语句。程序应该是这样工作的。首先,你需要选择像加法或减法这样的运算,然后输入第一个参数,然后输入第二个参数,然后得到一个结果。我写了代码,但它没有正确显示地址。答案显示出与我预期不同的结果。倍数和除法给我正确的结果,但添加并将其减错。请帮忙 #include<iostream> using namespace std; int main() { int a = 1, b = 2, c = 3, d = 4; int first_argument, second_argument; cout << "Please choose the number: " <<endl<< "1.Multiple"<<endl<<"2.Divide" <<endl<<"3.Substract"<<endl<<"4.Add" << "\n"; cin >> a,b,c,d; cout << "Please choose the first argument" << "\n"; cin >> first_argument; cout << "Please choose the second argument" << "\n"; cin>>second_argument; if (a==1) cout <<"The answer is "<<first_argument*second_argument<< "\n"; else if (b == 2) cout << "The answer is " << first_argument / second_argument << "\n"; else if (c == 3) cout << "The answer is " << first_argument - second_argument << "\n"; else if (d == 4) cout << "The answer is " << first_argument + second_argument << "\n"; cin.ignore(); cin.get(); return 0; } #包括 使用名称空间std; int main() { INTA=1,b=2,c=3,d=4; int第一个参数,第二个参数; 库特,c++,visual-c++,C++,Visual C++,应该是 cin >> a >> b >> c >> d; 否则,您将调用臭名昭著的,具有的,因此您的表达式被翻译为 (cin >> a), b, c, d; 它只读取a,然后计算b、c和d,表达式的最终结果是d,这当然不会在以后使用。但在任何情况下,此任务不需要4个变量,只有一个就足够了。然后需要测试其值并根据它执行所需的操作激动 旁注:当你除以整数时,结果将被截断。如果你不想这样做,那么将其中一个整数强制转换为一个双精度,如 fi

应该是

cin >> a >> b >> c >> d;
否则,您将调用臭名昭著的,具有的,因此您的表达式被翻译为

(cin >> a), b, c, d;
它只读取
a
,然后计算
b
c
d
,表达式的最终结果是
d
,这当然不会在以后使用。但在任何情况下,此任务不需要4个变量,只有一个就足够了。然后需要测试其值并根据它执行所需的操作激动

旁注:当你除以整数时,结果将被截断。如果你不想这样做,那么将其中一个整数强制转换为一个
双精度
,如

first_argument / static_cast<double>(second_argument)
第一个参数/静态参数转换(第二个参数)
应该是

cin >> a >> b >> c >> d;
否则,您将调用臭名昭著的,具有的,因此您的表达式被翻译为

(cin >> a), b, c, d;
它只读取
a
,然后计算
b
c
d
,表达式的最终结果是
d
,这当然不会在以后使用。但在任何情况下,此任务不需要4个变量,只有一个就足够了。然后需要测试其值并根据它执行所需的操作激动

旁注:当你除以整数时,结果将被截断。如果你不想这样做,那么将其中一个整数强制转换为一个
双精度
,如

first_argument / static_cast<double>(second_argument)
第一个参数/静态参数转换(第二个参数)
没有任何意义。
您很可能只想将用户选择的操作读入单个变量,并在if-else块中使用该值

示例:

#include <iostream>
using namespace std;
int main() {
  int operation, first_argument, second_argument;

  cout << "Please choose the number: " << endl << "1.Multiple" << endl << "2.Divide" << endl << "3.Substract" << endl << "4.Add" << "\n";

  cin >> operation;

  cout << "Please choose the first argument"
       << "\n";
  cin >> first_argument;
  cout << "Please choose the second argument"
       << "\n";
  cin >> second_argument;

  if (operation == 1)
    cout << "The answer is " << first_argument * second_argument << "\n";

  else if (operation == 2)
    cout << "The answer is " << first_argument / second_argument << "\n";
  else if (operation == 3)
    cout << "The answer is " << first_argument - second_argument << "\n";
  else if (operation == 4)
    cout << "The answer is " << first_argument + second_argument << "\n";

  cin.ignore();
  cin.get();
  return 0;
}
#包括
使用名称空间std;
int main(){
int运算,第一个参数,第二个参数;
库特
没有任何意义。
您很可能只想将用户选择的操作读入单个变量,并在if-else块中使用该值

示例:

#include <iostream>
using namespace std;
int main() {
  int operation, first_argument, second_argument;

  cout << "Please choose the number: " << endl << "1.Multiple" << endl << "2.Divide" << endl << "3.Substract" << endl << "4.Add" << "\n";

  cin >> operation;

  cout << "Please choose the first argument"
       << "\n";
  cin >> first_argument;
  cout << "Please choose the second argument"
       << "\n";
  cin >> second_argument;

  if (operation == 1)
    cout << "The answer is " << first_argument * second_argument << "\n";

  else if (operation == 2)
    cout << "The answer is " << first_argument / second_argument << "\n";
  else if (operation == 3)
    cout << "The answer is " << first_argument - second_argument << "\n";
  else if (operation == 4)
    cout << "The answer is " << first_argument + second_argument << "\n";

  cin.ignore();
  cin.get();
  return 0;
}
#包括
使用名称空间std;
int main(){
int运算,第一个参数,第二个参数;

输入任何逗号之前无法阅读:输入任何逗号之前阅读:我尝试了cin>>a>>b>>c>>d,但在编译时它根本没有显示任何消息,屏幕上也没有任何事情发生。我尝试了cin>>a>>b>>c>>d,但在编译时它根本没有显示任何消息,屏幕上也没有任何事情发生。谢谢。我理解我的错误……太多变量了这让我很困惑。谢谢。我理解我的错误……太多的变量让我很困惑。