Search code examples
c++visual-studio-2008

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


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

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.

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.


Solution

  • 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 :)