我正在编写一个简单的计算器,减去根据您输入1 2 3还是4来增加除法或乘法。我不断收到此错误。请记住,我是C ++的新手。它发生在Mode == 3和Mode == 4
的IF行中#include <iostream>
int main(){
using namespace std;
int x;
int y;
int x2;
int y2;
int x3;
int y3;
int x4;
int y4;
int Mode;
cout << "Welcome to Brian's Calculator!";
cout << endl;
cout << "Pick a mode. 1 is Addition. 2 Is Subtraction. 3 is Multiplacation. 4 is Division";
cin >> Mode;
if (Mode==1){
cout << "You chose addition.";
cout << endl;
cout << "Pick a number.";
cout << endl;
cin >> x;
cout << endl;
cout << "Pick another.";
cout << endl;
cin >> y;
cout << "The sum of the numbers you chose are: " << x+y <<".";
return 0;
};
if (Mode==2){
cout << "You chose subtraction.";
cout << endl;
cout << "Pick a number.";
cout << endl;
cin >> x2;
cout << endl;
cout << "Pick another.";
cout << endl;
cin >> y2;
cout << "The difference of the numbers you chose are: " << x2-y2 <<".";}
return 0;
};
if (Mode==3){
cout << "You chose Multiplacation.";
cout << endl;
cout << "Pick a number.";
cout << endl;
cin >> x3;
cout << endl;
cout << "Pick another.";
cout << endl;
cin >> y3;
cout << "The product of the numbers you chose are: " << x3*y3 <<".";
return 0;
};
if (Mode==4){
cout << "You chose Division.";
cout << endl;
cout << "Pick a number.";
cout << endl;
cin >> x4;
cout << endl;
cout << "Pick another.";
cout << endl;
cin >> y4;
cout << "The quotient of the numbers you chose are: " << x4/y4 <<".";
return 0;
};
答案 0 :(得分:0)
在这一行:
cout << "The difference of the numbers you chose are: " << x2-y2 <<".";}
您还有一个}
答案 1 :(得分:0)
问题:你有两个括号而不是一个:
if (Mode==2){
...
// DELETE THE EXTRANEOUS "}"!
cout << "The difference of the numbers you chose are: " << x2-y2 <<".";}
return 0;
};
建议替代方案:
if (Mode==2){
...
// DELETE THE EXTRANEOUS "}"!
cout << "The difference of the numbers you chose are: " << x2-y2 <<".";
return 0;
}
更好:
switch (Mode) {
case 1 :
...
break;
case 2 :
...
break;
答案 2 :(得分:0)
在这一行,您放错了}
:
cout << "The difference of the numbers you chose are: " << x2-y2 <<".";}
^
当您修复该问题后,您需要在程序结束后额外}
关闭main
。例如,当您关闭 if 语句时,;
之后也不需要}
:
if (Mode==1){
// code...
};
^