Storing data into an Array

Reply

Join Date: Oct 2005
Posts: 20
Reputation: Savage221 is an unknown quantity at this point 
Solved Threads: 0
Savage221 Savage221 is offline Offline
Newbie Poster

Storing data into an Array

 
0
  #1
Nov 13th, 2005
Sorry for the rather newbish question. I'm stuck on part of this program, it seems simple enough :-| The user is prompted to enter a phone number in the format of: xxx-xxxx (the hyphen must be included). From there the phone number needs to be stored into an array of 7 integers, lets just say phoneNumber. The hyphen however needs to be removed before the number is stored. Just storing data is easy, but removing the hyphen then storing is really confusing me, the book doesnt explain anything about how this could be done (at least nothing in the array chapter and any previous chapters).

Any help or tips would be greatly greatly apprechiated, thanks everyone
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Storing data into an Array

 
0
  #2
Nov 13th, 2005
store it in a character array and then compare each character with '-'....if it is equal to hyphen then skip it(don't store it) in the phone number array...use continue statement
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 20
Reputation: Savage221 is an unknown quantity at this point 
Solved Threads: 0
Savage221 Savage221 is offline Offline
Newbie Poster

Re: Storing data into an Array

 
0
  #3
Nov 13th, 2005
ok, thanks. The only part that worries me is the teacher is pretty strict on the code format, if we use different methods than what he wants he will make us change it. The teacher has made errors before in the instructions but it says the array needs to be 7 integers. isdigit() was mentioned to be used, I know nothing about it, and I assume isdigit needs a char type?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,340
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Storing data into an Array

 
0
  #4
Nov 13th, 2005
Originally Posted by Savage221
ok, thanks. The only part that worries me is the teacher is pretty strict on the code format, if we use different methods than what he wants he will make us change it. The teacher has made errors before in the instructions but it says the array needs to be 7 integers. isdigit() was mentioned to be used, I know nothing about it, and I assume isdigit needs a char type?
isdigit() returns true if the character is '0' through '9'
  1. char c = '1';
  2. if( isdigit(c) )
  3. printf("Its a digit\n");
  4. else
  5. printf("Its not a digit\n");

now, all you have to do is loop through the input string and place each digit into the next available array element. If isdigit() returns false (0) then do nothing. That will mean you will need to keep a counter variable to keep track of where in the array to place the digit.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 20
Reputation: Savage221 is an unknown quantity at this point 
Solved Threads: 0
Savage221 Savage221 is offline Offline
Newbie Poster

Re: Storing data into an Array

 
0
  #5
Nov 13th, 2005
maybe I should re read the chapter I have no problem making the loop run 8 times, prompting for the next number, checking if its a digit and then storing it into an array of 7 integers, however, the person needs to enter the phone number all at once with the hyphen. It's sad, but I'm drawing a blank, the rest of the program seems so simple, but I can't do anything untill I get past the initial input
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,340
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Storing data into an Array

 
0
  #6
Nov 13th, 2005
something like this. However this will not prevent somebody from entering more than 7 digits or from entering other characters.
  1. int c;
  2. int array[7] = {0};
  3. int counter = 0;
  4.  
  5. while( (c = getchar()) != '\n')
  6. {
  7. if( isdigit(c))
  8. // put the binary number in the array and bump the counter
  9. array[counter++] = c - '0';
  10. }

or you could use fgets() to get the entire string, then validate that it contains the right number of digits and a hyphen
  1. char input[9];
  2. fgets(input,sizeof(input),stdin);
  3. // does it contain a hyphen
  4. if( input[3] != '-' )
  5. {
  6. // display an error message
  7. }
  8. // count number of digits
  9. for(counter = 0, i = 0; input[i]; i++)
  10. {
  11. if( isdigit(input[i]))
  12. counter++;
  13. }
  14. if(counter != 7)
  15. {
  16. // display error message
  17. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 20
Reputation: Savage221 is an unknown quantity at this point 
Solved Threads: 0
Savage221 Savage221 is offline Offline
Newbie Poster

Re: Storing data into an Array

 
0
  #7
Nov 13th, 2005
ok, thanks alot Ancient Dragon, very helpful, and very much apprechiated
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC