C++ 试图用C++;;获取大量编译错误

C++ 试图用C++;;获取大量编译错误,c++,C++,我对编程非常陌生,这是我第一次尝试做任何事情。我相信我已经正确地声明了所有内容以及if语句,但是我得到了多个没有任何意义的编译错误,因为我无法解析他们试图说的代码有错。我非常困惑,我想知道我做错了什么 以下是一份清单: 1。main.cpp:在函数“int main()”中: main.cpp:28:19:错误:“答案”之前应为主表达式 cin>>字符串应答; ^ 2.main.cpp:30:19:错误:未在此作用域中声明“add” 如果(答案==添加) ^ 3.main.cpp:31:13:错

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

以下是一份清单:

1。main.cpp:在函数“int main()”中:
main.cpp:28:19:错误:“答案”之前应为主表达式
cin>>字符串应答;
^
2.main.cpp:30:19:错误:未在此作用域中声明“add”
如果(答案==添加)
^
3.main.cpp:31:13:错误:“int add”的重新声明
int add(回答1、回答2);
^
4.main.cpp:30:19:注:此处先前声明了“添加”
如果(答案==添加)
^
5.main.cpp:31:13:错误:“answer1”未在此范围内声明
int add(回答1、回答2);
^
6.main.cpp:31:21:错误:“answer2”未在此范围内声明
int add(回答1、回答2);
^
7.main.cpp:31:28:错误:表达式列表在初始值设定项[-fppermissive]中被视为复合表达式
int add(回答1、回答2);
^
8.main.cpp:33:5:错误:“else”没有前面的“if”
else if(答案==减法)
^
9main.cpp:33:24:错误:“subtract”未在此范围内声明
else if(答案==减法)
^
10main.cpp:34:18:错误:“int subtract”的重新声明
int减法(answer1,answer2);
^
11main.cpp:33:24:注:此处之前声明了“减法”
else if(答案==减法)
^
12main.cpp:34:18:错误:“answer1”未在此范围内声明
int减法(answer1,answer2);
^
13main.cpp:34:26:错误:“answer2”未在此范围内声明
int减法(answer1,answer2);
^
14main.cpp:34:33:错误:表达式列表在初始值设定项[-fppermissive]中被视为复合表达式
int减法(answer1,answer2);
^
15main.cpp:36:5:错误:“else”没有前面的“if”
否则如果(答案==乘法)
^
16main.cpp:36:24:错误:未在此作用域中声明“multiply”
否则如果(答案==乘法)
^
17main.cpp:37:18:错误:重新声明“整数乘法”
整数乘法(应答1,应答2);
^
18main.cpp:36:24:注意:前面在这里声明了“乘法”
否则如果(答案==乘法)
^
19main.cpp:37:18:错误:“answer1”未在此范围内声明
整数乘法(应答1,应答2);
^
20main.cpp:37:26:错误:“answer2”未在此范围内声明
整数乘法(应答1,应答2);
^
21main.cpp:37:33:错误:表达式列表在初始值设定项[-fppermissive]中被视为复合表达式
整数乘法(应答1,应答2);
^
22main.cpp:39:5:错误:“else”没有前面的“if”
else if(答案==除法)
^
23main.cpp:39:24:错误:未在此作用域中声明“divide”
else if(答案==除法)
^
24main.cpp:40:16:错误:重新声明“整除”
整数除法(回答1,回答2);
^
25main.cpp:39:24:注:此处先前声明了“除法”
else if(答案==除法)
^
26main.cpp:40:16:错误:“answer1”未在此范围内声明
整数除法(回答1,回答2);
^
27main.cpp:40:24:错误:“answer2”未在此范围内声明
整数除法(回答1,回答2);
^
28main.cpp:40:31:错误:表达式列表在初始值设定项[-fppermissive]中被视为复合表达式
整数除法(回答1,回答2);
^
29main.cpp:42:5:错误:“else”没有前面的“if”
否则如果
^
30main.cpp:43:5:错误:应为“(”在“cout”之前)

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

您的意思可能是
cin>>answer;
,因为
string answer;
在函数前面已经声明过

此外,您缺少
#include


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

使用
cin>>answer;
代替
cin>>string 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;   
}
另外,number1和number2至少应该是浮点数(或double),否则您将在除法运算中遇到麻烦


如果你有更多的问题,请打电话给我。

不要放弃!试着从头开始。慢慢来

#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;
}
这里有很多错误。老实说,我建议放弃这个,从头开始重写。每2-5行之后(当你完成一个if或一些输出或函数时),编译并运行,确保事情按您预期的方式进行。当您一次编写了50多行代码,并希望所有内容都能在这个编程级别上编译并工作时,您将花费大量的时间
#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