";"标记之前的预期主表达式 - c++ 的新增功能

Expected primary expression before ';' token - new to c++

本文关键字:表达式 c++ 功能 新增      更新时间:2023-10-16

所以,我的任务是创建一个简单的计算器,第一个需要 1 个数字,而不是你想要对其执行的操作,而不是第二个数字,问题是,我无法让它工作,因为(至少我认为正因为如此)编译器不能将 +、-、*/作为字符。 我能做些什么来解决它?提前感谢,我已经尝试寻找解决方案一段时间了,但无法......

#include <iostream>
using namespace std;
int main()
{
    double first;
    double second;
    char x;
    char add  = "+" ;
    char take = "-" ;
    char add2 = "*" ;
    char take2 = "/";
    cout << "Please enter the first numbern";
    cin >> first;
    cout << "Please enter the math actionn";
    cin >> x;
    cout << "Please enter the second numbern";
    cin >> second;
;    if (x == add)
    {
        cout << first << x << second << "=" << first+second;
    }
    if (x == take)
    {
        cout << first << x << second << "=" << first-second;
    }
    if (x == add2)
    {
        cout << first << x << second << "=" << first*second;
    }
    if (x == take2)
    {
        cout << first << x << second << "=" << first/second;
    }
    else
    {
        cout << "Couldn't reconize the character, please try again";
    }
}

这里有两件事你应该纠正。

  1. 使用 char c = 'x'; 而不是 char c = "x"; 来设置字符:双引号给你一个字符串,单引号给你一个字符。
  2. 其中
  3. 一行的开头有一个杂散的分号。

你需要删除它;首先从

;    if (x == add)

并在分配字符时使用单个 qoute ' 而不是"