Question: For this assignment, you are asked to implement the following 2 functions & use them to implement a program to get IDs from a user
For this assignment, you are asked to implement the following 2 functions & use them to implement a program to get IDs from a user & save them to a file.
1. The get word function is used to get a word from the user.
/* if a word of length (strictly) less than n is successfully read from stdin, it is stored in the array word & the function returns 1; if the user presses the end-of-file key, the function returns 0 (see details below)
pre-condition: n < 1024 */
int get_word(const char prompt[], char word[], size_t n);
This function repeatedly prompts & reads input from the user until either
the user enters a line whose first word has length (strictly) less than n; or
the user presses the end-of-file key (at the start of a line).
In the first case, the entered word is stored into word & the function returns 1; in the second case, the functions returns 0 (but be sure to call clearerr in this case). In all other cases (i.e., the user enters only whitespaces or the first word has length n or more), the function simply prompts for input again (with no error messages printed).
Input is read a line at a time & only the first word (if there is one) of the line is used. If there is no first word (i.e., there are no words) or if it has length n or more, the user is re-prompted for input.
prompt is the string printed (to standard output) by get word to ask the user to enter input.
Note that get word needs to use internal buffers to store the line the user enters & its first word. Use buffers of LINESIZE characters where LINESIZE is a macro with a value of 1024. Be sure not to overflow buffers.
2. The is valid id function is used to test whether a string is a valid ID.
/* returns 1 if s is a valid ID; otherwise returns 0 (see details below) */
int is_valid_id(const char s[]);
For this exercise, a string is a valid ID if it starts with either the character a or the character A followed by 8 digits (with no additional characters). For example, the string "a12345678" is a valid ID whereas "a32768", "abc" & "a12345678a" are not valid IDs.
Note that this function does not need to get input it simply validates a string.
Use the above 2 functions to implement a program that gets IDs from a user & save them to a file:
The program takes a filename as a command-line argument. This file is created if it doesnt exist; otherwise, its content is deleted.
The program then repeatedly prompts the user for an ID. If the user enters a valid ID, it is saved to the file with lowercase a. (Each ID is saved on a separate line.) If the entered ID is not valid, it is simply ignored. The program terminates when the user hits the end-of-file key when prompted for input.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
