1,105,633 Community Members

reading a number separated by hyphens

Member Avatar
Prankmore
Newbie Poster
14 posts since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Adak
Posting Virtuoso
1,711 posts since Jun 2008
Reputation Points: 419 [?]
Q&As Helped to Solve: 207 [?]
Skill Endorsements: 10 [?]
 
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. ;)

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

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.

Member Avatar
Prankmore
Newbie Poster
14 posts since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
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.

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
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.

Member Avatar
Prankmore
Newbie Poster
14 posts since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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;
}
Member Avatar
Prankmore
Newbie Poster
14 posts since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
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;
}
Member Avatar
Prankmore
Newbie Poster
14 posts since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Question Answered as of 2 Years Ago by Narue, WaltP and Adak
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: