tags:

views:

193

answers:

8

I'm trying input a phone number in the format: 555-555-5555 into a struct with three int's. I've tried using getline with a delimiter of "-", but I keep getting the error: "cannot convert parameter 1 from 'int' to 'char *'".

I tried creating a temp char* variable to store the number in and then type casting it to int, but that didn't work.

How should I go about doing this?

Thanks

edit:

here's some of the code:

void User::Input(istream& infile) {

    char* phone_temp;

    ...

    infile.getline(phone_temp, sizeof(phoneNum.areaCode), "-");
    phoneNum.areaCode = (int)phone_temp;

    ...
}
+1  A: 

if I understand correctly, try atoi() or stringstream to convert from char* to int

+1  A: 

See this example on how you can tokenize the line. This question will also help.

Then use atoi to convert string to int.

kgiannakakis
Tokenizing it would be the best way to go imo.
ChadNC
A: 

You can't cast a char* to an int and expect a correct value. A char* is an address in memory, so when you cast it to int, you'll get a memory address in your int. You need to call a function, such as atoi() to algorithmically convert the data char* is pointing to into an integer.

Marcin
+4  A: 

Since you are posting this as a c++ question, and not a c question, Use istringstream http://www.cplusplus.com/reference/iostream/istringstream/

From my head it your code would become something like:

std::string sPhoneNum("555-555-5555");
struct
{
   int p1;
   int p2;
   int p3;
} phone;
char dummy;

std::istringstream iss(sPhoneNum);
iss >> phone.p1; // first part
iss >> dummy;    // '-' character
iss >> phone.p2; // second part
iss >> dummy;    // '-' character
iss >> phone.p2; // last part

EDIT: now that you have posted example code, I see you already start with an istream, you can just use the >> operator directly, no need to create another istringstream operator. See examples: http://www.cplusplus.com/reference/iostream/istream/operator%3E%3E/

Also, stay away from c-style conversion methods with char * and atoi stuff if you don't have to, working with std::string and istreams is the "right" C++ way. It avoids memory leaks and other nasty problems.

catchmeifyoutry
if you downvote, please comment on why too.
catchmeifyoutry
A: 

rather than using infile.getline() use the free standing version with a std::string: getfile(infile, buffer);

After that, if you'd like you can do an addition getline():

istringstream phonenumber(buiffer); string areacode = getline(phonenumber, part1. '-');

or you can use the extractor >> (that's what it's for!) int areacode; phonenumber >> areacode;

Just a side note: if you're using char* do make sure you allocate space for it, or at least point to allocated space.

Liz Albin
+2  A: 

Reading a phone number from a stream:

Assuming the number is well formatted:

void User::Input(istream& infile)
{    

    int part1;
    int part2;
    int part3;
    char dash1; 
    char dash2; 

    infile >> part1 >> dash1 >> part2 >> dash2 >> part3;

    /*
     * !infile will return false if the file is in a bad state.
     *         This will happen if it fails to read a number from
     *         the input stream or the stream ran out of data.
     *
     * Both these conditions constitute an error as not all the values will
     * be set correctly. Also check that the dash[12] hold the dash character.
     * Otherwise there may be some other formatting problem.
     */ 
    if ((!infile) || (dash1 != '-') || (dash2 != '-'))
    {
         throw int(5); // convert this to your own exception object.
    }
}
Martin York
A: 

Another viable option, although not quite C++, is:

char a[10],b[10],c[10];
scanf("%d-%d-%d", a, b, c);
rmn
A: 

It appears you're trying to convert a char to an integer, in which case you'd want to use the atoi function or a string stream.

Daniel