错误C2440:'=':无法从'const char [2]'转换为'char' [英] error C2440: '=' : cannot convert from 'const char [2]' to 'char'
问题描述
我正在学习c ++,我在做一些新手事情时遇到问题。我试图创建一个非常小的应用程序,它接受用户输入并将其存储到一个字符数组。然后我解析通过该数组,删除所有括号和dases并显示它。例如以下
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
(325)858-7455
to
3258587455
(325)858-7455
to
3258587455
但我遇到错误
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.
推荐答案
问题在这里,我相信:
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 :)
这篇关于错误C2440:'=':无法从'const char [2]'转换为'char'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!