-1

我正在编写一个简单的计算器,根据您输入的是 1 2 3 还是 4,该计算器可以减法或乘法。我不断收到此错误。请记住,我是 C++ 新手。它发生在模式 == 3 和模式 == 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;
};
4

3 回答 3

0

在这一行:

cout << "The difference of the numbers you chose are: " << x2-y2 <<".";}

你有一个额外的}

于 2013-10-25T01:55:29.990 回答
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;
于 2013-10-25T01:55:40.943 回答
0

在这条线上你有一个错位}

cout << "The difference of the numbers you chose are: " << x2-y2 <<".";}
                                                                       ^

}当您修复该问题时,您也需要在程序结束后额外关闭main。此外,当您关闭if语句时,您也不需要 a ;after the ,例如:}

if (Mode==1){
// code...
};
 ^
于 2013-10-25T01:57:05.903 回答