2

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;

    ...
}
Joe
  • 4,553
  • 9
  • 51
  • 57

8 Answers8

4

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
  • 7,179
  • 1
  • 29
  • 26
2

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.
    }
}
luke
  • 36,103
  • 8
  • 58
  • 81
Martin York
  • 257,169
  • 86
  • 333
  • 562
1

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

user224003
  • 217
  • 1
  • 3
1

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

Then use atoi to convert string to int.

Community
  • 1
  • 1
kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
0

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
  • 12,245
  • 9
  • 42
  • 49
0

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.

thkala
  • 84,049
  • 23
  • 157
  • 201
Liz Albin
  • 1,479
  • 8
  • 8
0

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

char a[10],b[10],c[10];
scanf("%d-%d-%d", a, b, c);
rmn
  • 2,386
  • 1
  • 14
  • 21
0

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
  • 75
  • 1
  • 5