c++错误c2015:太多字符在常量。

时间:2022-01-22 19:51:21

Sorry if it's something simple, but I'm new to C++ and haven't really gotten a good hold on it, yet. I need to build a calculator whose only named variables are pointers, and this is what I have so far, but I keep getting errors and I can't figure out why. Every error that always related to my if construct, though.

不好意思,如果是简单的东西,但是我是c++的新手,还没有真正掌握它。我需要建立一个计算器,它的唯一命名变量是指针,这是目前为止我所拥有的,但我不断地出错,我不知道为什么。不过,每个错误都与我的if构造有关。

int main()
{
    //Creating variables

    //Values to perform operations on
    float *aptr = new(nothrow)float;
    float *bptr = new(nothrow)float;
    float *ansptr = new(nothrow)float;
    int *endptr = new(nothrow)int;
    char *operationptr = new(nothrow)char;

    cout << "Simple Operation Calculator" << endl; //Displays program name
    cout  << "Performs +, -, *, or / on any two real operands." << endl; //Describes nature of program to user

    *endptr = 1;
    while(*endptr = 1) //Creates a loop so that the user may perform as many operations as desired
    {
        //Prompts the user for the first operand
        cout << "First operand: " << endl;
        cin >> *aptr;

        //Prompts user for operator
        cout << "Operator(+,-,*,/): " << endl;
        cin >> *operationptr;

        //Prompts user for second operand
        cout << "Second operand: " << endl;
        cin >> *bptr;

        //Performs requested operation
        if(*operationptr == '+' || *operationptr == 'plus')
        {
            *ansptr = *aptr + *bptr;
        }
        else if(*operationptr == '-' || *operationptr == 'minus')
        {
            *ansptr = *aptr - *bptr;
        }
        else if(*operationptr == '*' || *operationptr ==  'times')
        {
            *ansptr = *aptr * *bptr;
        }
        else if(*operationptr == '/' || *operationptr == 'divided by')
        {
            if(*bptr = 0)
            {
                cout << "Cannot divide by zero. Terminating program." << endl;
                *endptr = 2;
                break;
            }
            *ansptr = *aptr / *bptr;
        }
        else
        {
            cout << "Invalid operand input. Terminating program." << endl;
            *endptr = 2;
            break;
        }

        //Displays results
        cout << *aptr << *operationptr << *bptr << " = " << *ansptr << endl;

        //Asks user if they wish to perform another operation. If so, they stay in loop. If not, then break from loop.
        cout << "Do you wish to perform another operation? (1 = yes, 2 = no)" << endl;
        cin >> *endptr;

        //If 1, loop repeats. If 2, program ends.
        if (*endptr == 2)
        {
            cout << "Thank you for using my program. Goodbye!" << endl;
        }
    } //end while loop

    return 0;

 }//end main function

2 个解决方案

#1


6  

There are character literals (with ') and string literals (with "). Character literals have one character. String literals are arrays of characters. You can't write something like 'plus' because it has more than one character (well technically you can, but it's a multi-character literal, but lets not go there).

有字符文字(有)和字符串文字(有“)”。字符文字有一个字符。字符串字面值是字符数组。你不能写“+”这样的东西,因为它有一个以上的字符(从技术上讲你可以,但是它是一个多字符的文字,但是我们不去那里)。

Nonetheless, this wouldn't make any sense because operationptr points at a single char object. A single char can't contain the entire word plus.

尽管如此,这不会有任何意义,因为operationptr指向一个char对象。单个字符不能包含整个单词加上。

If you want to be able to accept plus as input, then I suggest you start using strings. In fact, use std::string.

如果您希望能够接受+作为输入,那么我建议您开始使用字符串。事实上,使用std::string。

As a side note, you are using pointers and dynamic allocation far too often. You are also forgetting to delete the objects that you create with new - this is a memory leak. I imagine you have come from a language that uses new for all object creation. In C++, this is not necessary (and is not a good practice). Instead, you can declare objects like so:

顺便说一下,您经常使用指针和动态分配。你也忘记删除你用新创建的对象——这是内存泄漏。我想您来自一种对所有对象创建都使用新语言的语言。在c++中,这是不必要的(并且不是一个好的实践)。相反,您可以声明如下对象:

float aptr;

There is no need to dereference this object. You can just use aptr directly as a float.

没有必要取消这个对象。您可以直接使用aptr作为浮动。

#2


5  

'plus'

“+”

is a character constant, and can't contain more than one character.

是一个字符常量,不能包含多个字符。

'+' is fine, since it's a single character in a constant.

“+”很好,因为它是一个常量中的一个字符。

As per the comment on this answer,

根据这个答案的评论,

'plus' could be ok, if the compiler is not expecting a char.

如果编译器不期望有char,那么“plus”就可以了。

#1


6  

There are character literals (with ') and string literals (with "). Character literals have one character. String literals are arrays of characters. You can't write something like 'plus' because it has more than one character (well technically you can, but it's a multi-character literal, but lets not go there).

有字符文字(有)和字符串文字(有“)”。字符文字有一个字符。字符串字面值是字符数组。你不能写“+”这样的东西,因为它有一个以上的字符(从技术上讲你可以,但是它是一个多字符的文字,但是我们不去那里)。

Nonetheless, this wouldn't make any sense because operationptr points at a single char object. A single char can't contain the entire word plus.

尽管如此,这不会有任何意义,因为operationptr指向一个char对象。单个字符不能包含整个单词加上。

If you want to be able to accept plus as input, then I suggest you start using strings. In fact, use std::string.

如果您希望能够接受+作为输入,那么我建议您开始使用字符串。事实上,使用std::string。

As a side note, you are using pointers and dynamic allocation far too often. You are also forgetting to delete the objects that you create with new - this is a memory leak. I imagine you have come from a language that uses new for all object creation. In C++, this is not necessary (and is not a good practice). Instead, you can declare objects like so:

顺便说一下,您经常使用指针和动态分配。你也忘记删除你用新创建的对象——这是内存泄漏。我想您来自一种对所有对象创建都使用新语言的语言。在c++中,这是不必要的(并且不是一个好的实践)。相反,您可以声明如下对象:

float aptr;

There is no need to dereference this object. You can just use aptr directly as a float.

没有必要取消这个对象。您可以直接使用aptr作为浮动。

#2


5  

'plus'

“+”

is a character constant, and can't contain more than one character.

是一个字符常量,不能包含多个字符。

'+' is fine, since it's a single character in a constant.

“+”很好,因为它是一个常量中的一个字符。

As per the comment on this answer,

根据这个答案的评论,

'plus' could be ok, if the compiler is not expecting a char.

如果编译器不期望有char,那么“plus”就可以了。