We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,473 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

reading a number separated by hyphens

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

4
Contributors
9
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
11
Views
Question
Answered
Prankmore
Newbie Poster
14 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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. ;)

Adak
Posting Virtuoso
1,640 posts since Jun 2008
Reputation Points: 456
Solved Threads: 196
Skill Endorsements: 7

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.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

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

Prankmore
Newbie Poster
14 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

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.

Narue
Bad Cop
Team Colleague
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 54

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;
}
Prankmore
Newbie Poster
14 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

Prankmore
Newbie Poster
14 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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;
}
Narue
Bad Cop
Team Colleague
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 54

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

Prankmore
Newbie Poster
14 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by Narue, WaltP and Adak

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0896 seconds using 2.7MB