views:

329

answers:

5

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.

+13  A: 

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

Jon Skeet
+2  A: 
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.

AakashM
So down to internals ... isn't "i" a 2char string constant (one for length, one for actual letters) and so it registers as a 2char string constant? (being in the source it's obviously not a dynamic string in this case). Also, what would the compiler emit if the choice instead were "" (I'm too lazy to bring up a C++ compiler... )
drachenstern
@drachenstern yes `"i"` will be an array of two characters, one for the `i` and one for the null terminator. Similarly for `""` it would be an array of just one, the null terminator. In both cases the compiler complains because the *types* are incompatible - it is not valid to assign an *array* (even one with just one member) to a non-array.
AakashM
@AakashM Thanks for clearing that up. I forgot the little word array in my thinking ... ;)
drachenstern
A: 

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]

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

I would remove that block.

drachenstern
This is a good point but it doesn't really answer the current question.
jmucchiello
I know, just the fact that I can't put code in a comment.
drachenstern
+3  A: 

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

Diego Pereyra
+1  A: 

I suggest using C++ strings and streams:

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

using std::stream;
using std::cout;
using std::endl;
using std::cerr;

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

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

    // Remove non-numeric characters
    const std::string  chars_to_remove = " ()-";
    position = 0;
    while ((position = phone_number.find_first_of(chars_to_remove, position))
           != std::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 *).

Thomas Matthews
This is infinitely the most correct answer. The OP's compilation error was by far and away not the worst problem in his code, such as the buffer overrun waiting to happen in phonenum[25].
DeadMG
im clueless to what a buffer overrun is. and that code doesnt compile. Not sure if alot of it is psuedo or not. I wouldnt know cause im not that fluent in c++
numerical25
@numerical25: This is not pseudo-code. There are just a few typos in the first part. Replace "using std::stream" with "using std::string" and add "using std::cin".
Lucas