Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

#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.

like image 207
numerical25 Avatar asked May 13 '10 14:05

numerical25


4 Answers

The important thing here is to understand the difference between "i" and '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).

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'.

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

like image 25
Diego Pereyra Avatar answered Nov 01 '22 05:11

Diego Pereyra


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

like image 154
Jon Skeet Avatar answered Nov 01 '22 07:11

Jon Skeet


I suggest using C++ strings and streams:

#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::string has many useful methods for manipulating methods.

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

like image 4
Thomas Matthews Avatar answered Nov 01 '22 06:11

Thomas Matthews


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.

like image 2
AakashM Avatar answered Nov 01 '22 07:11

AakashM