尝试使用C ++创建基本的计算器;收到大量编译错误

时间:2018-11-15 21:52:12

标签: c++

我对编程非常陌生,这是我第一次尝试做任何事情。我相信我已经正确地声明了所有内容以及if语句,但是我遇到了没有任何意义的多个编译错误,因为我无法解析它们试图说的是错误的代码。我非常困惑,我想学习自己在做错什么。

以下是列表:

1. main.cpp: In function ‘int main()’:
main.cpp:28:19: error: expected primary-expression before ‘answer’
     cin >> string answer;
                   ^
2. main.cpp:30:19: error: ‘add’ was not declared in this scope
     if (answer == add)
                   ^
3. main.cpp:31:13: error: redeclaration of ‘int add’
     int add(answer1,answer2);
             ^
4. main.cpp:30:19: note: ‘add’ previously declared here
     if (answer == add)
                   ^
5. main.cpp:31:13: error: ‘answer1’ was not declared in this scope
     int add(answer1,answer2);
             ^
6. main.cpp:31:21: error: ‘answer2’ was not declared in this scope
     int add(answer1,answer2);
                     ^
7. main.cpp:31:28: error: expression list treated as compound expression in initializer [-fpermissive]
     int add(answer1,answer2);
                            ^
8. main.cpp:33:5: error: ‘else’ without a previous ‘if’
     else if (answer == subtract)
     ^
9. main.cpp:33:24: error: ‘subtract’ was not declared in this scope
     else if (answer == subtract)
                        ^
10. main.cpp:34:18: error: redeclaration of ‘int subtract’
     int subtract(answer1,answer2);
                  ^
11. main.cpp:33:24: note: ‘subtract’ previously declared here
     else if (answer == subtract)
                        ^
12. main.cpp:34:18: error: ‘answer1’ was not declared in this scope
     int subtract(answer1,answer2);
                  ^
13. main.cpp:34:26: error: ‘answer2’ was not declared in this scope
     int subtract(answer1,answer2);
                          ^
14. main.cpp:34:33: error: expression list treated as compound expression in initializer [-fpermissive]
     int subtract(answer1,answer2);
                                 ^
15. main.cpp:36:5: error: ‘else’ without a previous ‘if’
     else if (answer == multiply)
     ^
16. main.cpp:36:24: error: ‘multiply’ was not declared in this scope
     else if (answer == multiply)
                        ^
17. main.cpp:37:18: error: redeclaration of ‘int multiply’
     int multiply(answer1,answer2);
                  ^
18. main.cpp:36:24: note: ‘multiply’ previously declared here
     else if (answer == multiply)
                        ^
19. main.cpp:37:18: error: ‘answer1’ was not declared in this scope
     int multiply(answer1,answer2);
                  ^
20. main.cpp:37:26: error: ‘answer2’ was not declared in this scope
     int multiply(answer1,answer2);
                          ^
21. main.cpp:37:33: error: expression list treated as compound expression in initializer [-fpermissive]
     int multiply(answer1,answer2);
                                 ^
22. main.cpp:39:5: error: ‘else’ without a previous ‘if’
     else if (answer == divide)
     ^
23. main.cpp:39:24: error: ‘divide’ was not declared in this scope
     else if (answer == divide)
                        ^
24. main.cpp:40:16: error: redeclaration of ‘int divide’
     int divide(answer1,answer2);
                ^
25. main.cpp:39:24: note: ‘divide’ previously declared here
     else if (answer == divide)
                        ^
26. main.cpp:40:16: error: ‘answer1’ was not declared in this scope
     int divide(answer1,answer2);
                ^
27. main.cpp:40:24: error: ‘answer2’ was not declared in this scope
     int divide(answer1,answer2);
                        ^
28. main.cpp:40:31: error: expression list treated as compound expression in initializer [-fpermissive]
     int divide(answer1,answer2);
                               ^
29. main.cpp:42:5: error: ‘else’ without a previous ‘if’
     else if
     ^
30. main.cpp:43:5: error: expected ‘(’ before ‘cout’
     cout << "Invalid Option!";
     ^
31. main.cpp: At global scope:

32. main.cpp:50:14: error: ‘a’ was not declared in this scope
 int subtract(a,b);
              ^
33. main.cpp:50:16: error: ‘b’ was not declared in this scope
 int subtract(a,b);
                ^
34. main.cpp:50:17: error: expression list treated as compound expression in initializer [-fpermissive]
 int subtract(a,b);
                 ^
35. main.cpp:51:1: error: expected unqualified-id before ‘{’ token
 {
 ^
36. main.cpp:56:14: error: ‘a’ was not declared in this scope
 int multiply(a,b);
              ^
37. main.cpp:56:16: error: ‘b’ was not declared in this scope
 int multiply(a,b);
                ^
38. main.cpp:56:17: error: expression list treated as compound expression in initializer [-fpermissive]
 int multiply(a,b);
                 ^
39. main.cpp:57:1: error: expected unqualified-id before ‘{’ token
 {
 ^
40. main.cpp:62:9: error: ‘a’ was not declared in this scope
 int add(a,b);
         ^
41. main.cpp:62:11: error: ‘b’ was not declared in this scope
 int add(a,b);
           ^
42. main.cpp:62:12: error: expression list treated as compound expression in initializer [-fpermissive]
 int add(a,b);
            ^
43. main.cpp:63:1: error: expected unqualified-id before ‘{’ token
 {
 ^
44. main.cpp:68:12: error: ‘a’ was not declared in this scope
 int divide(a,b);
            ^
45. main.cpp:68:14: error: ‘b’ was not declared in this scope
 int divide(a,b);
              ^
46. main.cpp:68:15: error: expression list treated as compound expression in initializer [-fpermissive]
 int divide(a,b);
               ^
47. main.cpp:69:1: error: expected unqualified-id before ‘{’ token
 {
 ^

代码如下:

#include <iostream>
using namespace std;

int main()
{

int number1;
int number2;
string answer;
int result;
int a;
int b;


cout << "Enter first number" << endl;
cin >> number1;
cout << "Enter second number" << endl;
cin >> number2;
cout << "What would you like to do?" << endl << "Options: Add, substract, 
multiply, divide" << endl;
cin >> string answer;

if (answer == add)
int add(answer1,answer2);
 cout << result;
else if (answer == subtract)
int subtract(answer1,answer2);
 cout << result;
else if (answer == multiply)
int multiply(answer1,answer2);
 cout << result;
else if (answer == divide)
int divide(answer1,answer2);
cout << result;
else if
cout << "Invalid Option!";
return 0;

}



int subtract(a,b);
{
 result = a-b;
 return result;   
}

int multiply(a,b);
{
    cout << a*b;
}

int add(a,b);
{
cout << a+b;
}

int divide(a,b);
{
    cout << a/b;
 }

3 个答案:

答案 0 :(得分:1)

使用cin >> string answer;代替cin >> answer;。您已经声明了答案,因此不必在此处声明答案(实际上是不合法的)。除此之外,您声明的加,减,乘和除函数还为时过早。添加声明或将这些函数定义移至main之前。你还有额外的;在函数声明和它的主体之间出现符号,并且您缺少函数参数的类型

int subtract(a,b); // delete ; here and add int (or better float) before a and b
{
 result = a-b; // you need to declare result, add type before this line (int or float, float would be better
 return result;   
}

数字1和数字2也应至少为浮点数(或双精度数),否则您进行除法运算的时间会很糟糕。

如果您还有其他疑问,请帮我。

答案 1 :(得分:1)

不要放弃!尝试从头开始。慢慢移动。

#include <iostream>
#include <string>

/*
This is regarded as bad practice...
From Python's `import this`:
   Namespaces are one honking great idea -- let's do more of those!
*/
using namespace std;

// Define your function
int add(int a, int b);

int main()
{

    int number1, number2, result;
    string answer;

    cout << "Enter first number" << endl;
    cin >> number1;
    cout << "Enter second number" << endl;
    cin >> number2;
    cout << "What would you like to do?" << endl << "Options: Add, substract, multiply, divide" << endl;
    cin >> answer;

    cout << "You chose: " << answer << endl;

    // Can this handle lowercase "add"? If not, how do you fix it?
    if (answer == "Add") {
        result = add(number1,number2);
        cout << number1 << " + " << number2 << " = " << result << endl;
    }
    // What about divide, multiply, etc.?
    // That's an exercise left to the reader ;)

}

int add(int a, int b) {
    return a + b;
}

这将产生:

Enter first number
12

Enter second number
12

What would you like to do?
Options: Add, substract, multiply, divide
Add

You chose: Add
12 + 12 = 24

答案 2 :(得分:0)

cin >> string answer;没有任何意义。 string answer不是表达式,因此不能将其用作>>运算符的右侧操作数。

您的意思可能是cin >> answer;,因为string answer;已在函数的较早位置声明。

此外,您还缺少#include <string>

(此答案仅涵盖第一个错误。我通常只查看第一个错误消息,修复代码,然后再次编译。在您的情况下,代码似乎有更多错误。)