| | |
Storing data into an Array
![]() |
•
•
Join Date: Oct 2005
Posts: 20
Reputation:
Solved Threads: 0
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
Any help or tips would be greatly greatly apprechiated, thanks everyone
•
•
Join Date: Oct 2005
Posts: 20
Reputation:
Solved Threads: 0
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?
•
•
•
•
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?
C Syntax (Toggle Plain Text)
char c = '1'; if( isdigit(c) ) printf("Its a digit\n"); else 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.
•
•
Join Date: Oct 2005
Posts: 20
Reputation:
Solved Threads: 0
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
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 something like this. However this will not prevent somebody from entering more than 7 digits or from entering other characters.
or you could use fgets() to get the entire string, then validate that it contains the right number of digits and a hyphen
C Syntax (Toggle Plain Text)
int c; int array[7] = {0}; int counter = 0; while( (c = getchar()) != '\n') { if( isdigit(c)) // put the binary number in the array and bump the counter array[counter++] = c - '0'; }
or you could use fgets() to get the entire string, then validate that it contains the right number of digits and a hyphen
C Syntax (Toggle Plain Text)
char input[9]; fgets(input,sizeof(input),stdin); // does it contain a hyphen if( input[3] != '-' ) { // display an error message } // count number of digits for(counter = 0, i = 0; input[i]; i++) { if( isdigit(input[i])) counter++; } if(counter != 7) { // display error message }
![]() |
Similar Threads
- Sending an acknowledgement via Email while storing data in ASP.NET1.0 (ASP.NET)
- Data consolidation across an array a.k.a. Hashed to death... heeelp... (Perl)
- Please help with data file and array (C++)
- array / data comparision help (C)
- input data from a file into an Array (C)
- adding data into an char array (C++)
- How do I create a program using an Array ? (C++)
Other Threads in the C Forum
- Previous Thread: Binary Tree Traversal
- Next Thread: Please help with atoi()
| Thread Tools | Search this Thread |
#include * ansi api array arrays binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() database directory dynamic fflush fgets file floatingpointvalidation forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux highest histogram homework i/o inches include infiniteloop input interest intmain() iso keyboard km linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send sequential shape single socketprogramming stack standard strchr string suggestions test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






