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'
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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
something like this. However this will not prevent somebody from entering more than 7 digits or from entering other characters.
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
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
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343