C++ 错误 C2440:“=”:无法从“const char [2]”转换为“char”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2827688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:11:27  来源:igfitidea点击:

error C2440: '=' : cannot convert from 'const char [2]' to 'char'

c++visual-studio-2008

提问by numerical25

I am learning c++, and I am having issues doing some newbie things. I am trying to create a very small application that takes the users input and stores it into a char array. I then parse through that array and remove all parenthesis and dases and display it. like the following

我正在学习 C++,我在做一些新手事情时遇到了问题。我正在尝试创建一个非常小的应用程序,它接受用户输入并将其存储到一个字符数组中。然后我解析该数组并删除所有括号和 dases 并显示它。像下面这样

(325)858-7455 to
3258587455

(325)858-7455 转
3258587455

But I am getting errors

但我收到错误

 error C2440: '=' : cannot convert from 'const char [2]' to 'char'

Below is my simple code that can easily be thrown in a compiler and ran.

下面是我的简单代码,可以轻松地将其放入编译器并运行。

#include "stdafx.h"
#include<iostream>
#include<conio.h>

using namespace std;

/*
This is a template Project
*/
int main()
{
    char phoneNum[25];

    for(int i = 0; i < (sizeof(phoneNum) / sizeof(char)); i++)
    {
        phoneNum[i] = "i";
    }


    cout<< "Enter a phone Number" <<endl;
    cin>>phoneNum;

    if(phoneNum[0] != '(' || phoneNum[4] != ')' || phoneNum[8] != '-')
    {
        cout<<"error";
    }
    else
    {

        for(int i = 0; i < (sizeof(phoneNum) / sizeof(char));i++)
        {
            if(phoneNum[i] != '(' || phoneNum[i] != ')' || phoneNum[i] != '-')
            {
                cout<<phoneNum[i];
            }
        }
    }

    cin>>phoneNum;
    getchar();


    return 0;
}

It is not completely finished so if anyone has any pointers on the best way to remove strings characters from a string. that would be great.

它没有完全完成,所以如果有人有任何关于从字符串中删除字符串字符的最佳方法的指针。那很好啊。

回答by Jon Skeet

The problem is here, I believe:

问题就在这里,我相信:

phoneNum[i] = "i";

You want to assign a single character, so you need to use single quotes for your literal:

您想分配一个字符,因此您需要对文字使用单引号:

phoneNum[i] = 'i';

There may well be other problems - I've only tried to fix the one mentioned in the title :)

很可能还有其他问题 - 我只是试图解决标题中提到的问题:)

回答by Diego Pereyra

The important thing here is to understand the difference between "i"and 'i'.

这里最重要的是要了解之间的差异"i"'i'

"i"is a string, and strings are stored in memory as a sequence of char values, appending at the end of the string a null character (let's say zero). So when you write "hello"you are storing 'h' 'e' 'l' 'l' 'o' '(null)'. In the same way, when you write "i", you are storing 'i' '(null)', and thats the 'const char [2]'(an array of 2 char elements).

"i"是一个字符串,字符串作为字符值序列存储在内存中,在字符串的末尾附加一个空字符(假设为零)。因此,当您编写时,"hello"您正在存储'h' 'e' 'l' 'l' 'o' '(null)'. 以同样的方式,当您编写 时"i",您正在存储'i' '(null)',这就是'const char [2]'(2 个字符元素的数组)。

When you take a 'char array' and use the [] operator, you are referring to a 'char'element in that array. So when you write phoneNum[i]you are getting a 'char'.

当您使用“字符数组”并使用 [] 运算符时,您指的'char'是该数组中的一个元素。所以当你写的时候,phoneNum[i]你会得到一个'char'.

That's why you need to write phoneNum[i] = 'i';

这就是为什么你需要写 phoneNum[i] = 'i';

回答by Thomas Matthews

I suggest using C++ strings and streams:

我建议使用 C++ 字符串和流:

#include <string>
#include <iostream>
#include <cstdlib>

using std::string;
using std::cout;
using std::endl;
using std::cerr;
using std::cin;
using std::flush;

int main(void)
{
    string phone_number;
    cout << "Enter phone number: " << flush;
    getline(cin, phone_number);

    // Check first for valid characters
    const string valid_characters = "0123456789()- ";
    string::size_type position = phone_number.find_first_not_of(valid_characters);
    if (position != string::npos)
    {
        cerr << "Invalid phone number.\n";
        return EXIT_FAILURE;
    }

    // Remove non-numeric characters
    const string chars_to_remove = " ()-";
    position = 0;
    while ((position = phone_number.find_first_of(chars_to_remove, position))
           != string::npos)
    {
        phone_number.erase(position, 1);
    }

    cout << "\nPhone number only digits: " << phone_number << endl;
    return EXIT_SUCCESS;
}

The std::stringhas many useful methods for manipulating methods.

std::string有几个操作方法许多有用的方法。

The advice from many experienced developers on Stack Overflowis for newbies to learn using C++ strings (std::string) before using C-style strings (char *).

Stack Overflow上许多有经验的开发人员的建议是让新手std::string在使用 C 样式字符串 ( char *)之前先学习使用 C++字符串 ( )。

回答by AakashM

phoneNum[i] = "i";

The thing on the left is a char; the thing on the right is a string, an array of char. You want 'i'on the right.

左边的东西是一个char; 右边的东西是一个字符串,一个char. 你想'i'在右边。

回答by jcolebrand

Why do you need this check? What if they wanted to enter a non-US phone number formatted like you offered [meaning with parens and dashes, but not limited to (3)3-4]

为什么需要这张支票?如果他们想输入您提供的格式的非美国电话号码怎么办 [意思是括号和破折号,但不限于 (3)3-4]

if(phoneNum[0] != '(' || phoneNum[4] != ')' || phoneNum[8] != '-')
{
    cout<<"error";
}
else

I would remove that block.

我会删除那个块。