Code:
int main()
{
char id_num[1][5];
printf ("Enter <quit> under ID number when finished\n");
fgets(id_num[0],5,stdin);
printf("\nOutput: %s\n",id_num[0]);
return 0;
}
Run this code. fgets is better than gets because it protects the stream input by allowing you to control the size of the stream that it accepts. stdin means standard input.
On another note. scanf is not good unless you take special precautions. The reason is because it can leave characters in the standard input buffer, and most critically the newline character. Infact even fgets can leave characters in the standard input buffer but not the newline. A newline left in the buffer can kill subsequent input attempts. One way to combat this is to clear the buffer. You can put this into your code after you scan:
while(getchar() != '\n') continue;
Also regarding validation. This should be performed on all input. It can be accomplished with scanf and a loop. If the input fails than the loop executes and the user is asked to enter input again. The loop breaks if the input is good. For example run this program:
Code:
int main()
{
int number;
//Say I want a number between 0 and 100 range
//All other input is rejected
printf("Enter a number: ");
while( scanf("%d",&number) != 1 || number > 100 || number < 0)
{
//clear standard input buffer
while(getchar() != '\n') continue;
printf("\nOut %d is out of range. \nEnter number between 0 and 100: ",number);
}
printf("\nYou entered %d, this is acceptable, thank you.\n",number);
return 0;
}
Lastly, now I see more of your program. Regarding your sort function. I can give you a simple example if you like.