For this assignment, write a program that reads lines of characters from the key
ID: 3552795 • Letter: F
Question
For this assignment, write a program that reads lines of characters from the keyboard. Each line will contain a password that must meet the following criteria:
The program should determine if the password that is entered is valid or not. If the password is invalid, all of the things that are wrong should be displayed.
This process will continue until the user enters a line beginning with an exclamation point ('!'). Once the user is done entering password information, the following should be displayed:
This program is not long or complex. But there are several new ideas and features to use. There are a lot of details, hints, and requirements in this assignment. So read it carefully - there is some new information in the assignment itself which is not in the lecture notes. Read it several times before you begin work on the program; make sure you clearly understand what is explained and required here.
When you think you are done, read the assignment again carefully and compare it to what is required to be sure you have followed instructions correctly. (Hint: always do this, for all assignments.)
The program is going to process lines of text, one line one character at a time. To do this, the program will use a new input library function:
cin.get(ch); - this function (technically, a "method") reads a single character from standard input and stores it into the char variable passed as an argument. Up to now, we have said that a function cannot directly alter the value of any of its arguments. That was actually a lie. :-) We will soon learn how it can be done. But for now, we will just use this feature.
cin >> ch; does not work well in this program, because it skips over newline chars and spaces.
So the program will read characters - one at a time - from the keyboard. Since the program will quit when the first char in a line is a '!' - but will output a line whenever a newline character is encountered, the following logic can be used (NOTE: ch is a char variable):
The above logic is a loop nested inside of a loop. The outer loop will start the process of examining a password by getting the first character on the line. The inner loop will process each individual character from the password, one at a time.
There is a behavior of cin.get() that needs to be understood: it will not "wake up" until a newline <enter> is typed. In other words, as the characters are typed, they will appear on the screen, but the actual processing of the character will not begin until the line is terminated by pressing <enter>.
The logic that is given above includes a portion in the inner while loop where the character that is being processed is displayed. This means that the program will "echo" - that is, repeat - the inputted line, character by character, on the screen. Understand that you will type a line, and see the characters you type, one at a time as you type. After you press <enter> you should see the same password displayed all at once on the next line.
Hint 1: write the program first without any counters: just make sure you can input and "echo" lines as described above, and also be able to terminate the program with a '!' Then add the calls to the functions and the counters.
Hint 2: think about creating several variables (maybe type bool or int) that can help to determine the validity of a password. For example, a variable called "hasDigit" could be created and if the password contains a digit, it's value could be set to 'true'. If these variables are created, it's important to remember that they will be re-used for every password that is entered by the user and therefore, they will need to be reset to an appropriate starting value *BEFORE* processing of a password begins. Using the earlier example, this means that the hasDigit variable should be set to 'false' before a password is processed.
Write and use the following 3 functions in the program.
This function will determine if the character ch is a digit '0' through '9'. If the character is a digit, return 1. Otherwise, return 0.
DO NOT use the isdigit function from the <cctype> library. You are writing your own version of the function.
This function will determine if the character ch is an uppercase 'A' through 'Z' character. If the character is uppercase, return 1. Otherwise, return 0.
DO NOT use the isupper function from the <cctype> library. You are writing your own version of the function.
This function will determine if the character ch is a lowercase 'a' through 'z' character. If the character is lowercase, return 1. Otherwise, return 0.
DO NOT use the islower function from the <cctype> library. You are writing your own version of the function.
As always, complete program documentation is required for this program, that includes documentation boxes for *EACH* function. This will be the last reminder in the program write-ups.
Hand in a copy of the source code (CPP file) using Blackboard.
Explanation / Answer
The program is going to process lines of text, one line one character at a time. To do this, the program will use a new input library function:
cin.get(ch); - this function (technically, a "method") reads a single character from standard input and stores it into the char variable passed as an argument. Up to now, we have said that a function cannot directly alter the value of any of its arguments. That was actually a lie. :-) We will soon learn how it can be done. But for now, we will just use this feature.
cin >> ch; does not work well in this program, because it skips over newline chars and spaces.
So the program will read characters - one at a time - from the keyboard. Since the program will quit when the first char in a line is a '!' - but will output a line whenever a newline character is encountered, the following logic can be used (NOTE: ch is a char variable):
The above logic is a loop nested inside of a loop. The outer loop will start the process of examining a password by getting the first character on the line. The inner loop will process each individual character from the password, one at a time.
There is a behavior of cin.get() that needs to be understood: it will not "wake up" until a newline <enter> is typed. In other words, as the characters are typed, they will appear on the screen, but the actual processing of the character will not begin until the line is terminated by pressing <enter>.
The logic that is given above includes a portion in the inner while loop where the character that is being processed is displayed. This means that the program will "echo" - that is, repeat - the inputted line, character by character, on the screen. Understand that you will type a line, and see the characters you type, one at a time as you type. After you press <enter> you should see the same password displayed all at once on the next line.
Hint 1: write the program first without any counters: just make sure you can input and "echo" lines as described above, and also be able to terminate the program with a '!' Then add the calls to the functions and the counters.
Hint 2: think about creating several variables (maybe type bool or int) that can help to determine the validity of a password. For example, a variable called "hasDigit" could be created and if the password contains a digit, it's value could be set to 'true'. If these variables are created, it's important to remember that they will be re-used for every password that is entered by the user and therefore, they will need to be reset to an appropriate starting value *BEFORE* processing of a password begins. Using the earlier example, this means that the hasDigit variable should be set to 'false' before a password is processed.
Write and use the following 3 functions in the program.
This function will determine if the character ch is a digit '0' through '9'. If the character is a digit, return 1. Otherwise, return 0.
DO NOT use the isdigit function from the <cctype> library. You are writing your own version of the function.
This function will determine if the character ch is an uppercase 'A' through 'Z' character. If the character is uppercase, return 1. Otherwise, return 0.
DO NOT use the isupper function from the <cctype> library. You are writing your own version of the function.
This function will determine if the character ch is a lowercase 'a' through 'z' character. If the character is lowercase, return 1. Otherwise, return 0.
DO NOT use the islower function from the <cctype> library. You are writing your own version of the function.
As always, complete program documentation is required for this program, that includes documentation boxes for *EACH* function. This will be the last reminder in the program write-ups.
Hand in a copy of the source code (CPP file) using Blackboard.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.