I am trying to take a user input phone number and perform calculations on it as an integer of type long long.

I've tried this but it only reads the first 3 numbers then when it reaches the hyphen it stops.

printf("Enter Number: \n");			//get input from user

	count = scanf ( "%[0-9]%n", hold, &len );	//check number of inputs and length of input


	number = atoll(hold);

so if i get a number like 555-123-9876

it will convert it to 5551239876

but i also need to be able to read numbers put in as 5551239876

any help would be appreciated

Recommended Answers

All 9 Replies

Take in the telephone number as a string.

Scan over the chars, and remove any hyphens or /'s.

If any char in the string is unwanted, then

for(each char from that point, to the (end -1) of the string) {
   assign i+1 to i, in the array
}

and you're dancing on the ceiling. ;)

I am trying to take a user input phone number and perform calculations on it as an integer of type long long.

Why? To see if the phone number is prime? This makes no sense because a phone number isn't a number, per se.

it's for my computing course at uni. i need to manipulate the number as an integer. simply just to show that i can. my ideas so far are to read them as seperate integers and them join them together some how though i'm not sure how to do that i thought that maybe there was a way to read it as a string skipping the hyphens then converting it to an integer which is what i tried to do above but can't get it right

i thought that maybe there was a way to read it as a string skipping the hyphens then converting it to an integer which is what i tried to do above but can't get it right

Not really, but you can read as a string and remove the hyphens, as Adak mentioned.

So much time wasted for such a simple task. I wonder if the OP really wants to do it and isn't just looking for a handout.

OK so I have written the loop as recommended and I think its a great idea thank you but...


I seem to be stuck on how to remove the hyphen I am only aware of how to replace it and it wont let me replace it with nothing

Ives tried doing as many different thing as I could think of but to no avail.

I've included the code I have so far tried the things that haven't worked are commented out and I've included what the printout was

# include<stdio.h>
# include<string.h>
# include<stdlib.h>

int

main(int argc, char **argv){

char number[30] = "555-123-9876";
char number2[30];
int count;

for (count = 0; count < sizeof(number); count++)
{
//count = count + 1

if (number[count] == '-')
//if (number[count] != '-' && number[count] != ' ')
{
//number[count] = '\0';								// I end up with 555
//number2[count] = number[count];					// I end up with 555
//strcpy(number2, &number[count]);					// I end up with 555-123-9876
}
}

printf("%s\n	", number);

return 0;
}

So much time wasted for such a simple task. I wonder if the OP really wants to do it and isn't just looking for a handout.

No-one has to spend time they can't spare. I have been stuck on this all night. it's the last part on a much larger project.

I am trying. and not asking for handouts. I like the help I've received so far which is why I'm still here.

Adak suggested one algorithm, but it's somewhat inefficient. Consider two pointers (or indices) into the string. One of them is the end of your result and the other walks along the original string and skips over hyphens. This is a common algorithm for removing whitespace, but it can be specialized to hyphens or even generalized:

char *remove_all(char *s, const char *match)
{
    char *start = s;
    char *p;
    
    for (p = s; *s != '\0'; s++) {
        if (strchr(match, *s) == NULL)
            *p++ = *s;
    }
    
    *p = '\0';
    
    return start;
}

Thanks heaps for the help. I got it with an added while() and moved the rest of the string back one place each time there was a hyphen. i realise this is not the most efficient way but i'm glad it worked in the end.

haven't tried narue's last suggestion but will have a look now

solved :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.