Activity 7 - Input line
Congratulations!
You are very close to completely saving the nation. All that remains is to be able to enter a complete sentence! For this we only have to make a few small changes. First, you need to use the statement to enter the string:getline()
.
string phrase;
getline(cin, phrase);
By doing it this way, the console will be able to accept a complete message, not just a single word.
Follow these steps, and complete the program to save the kingdom:
- In the
main
function, change the statementcin >> name;
togetline(cin, name);
. - In both functions, add an
if
condition in thefor
loop, and execute the contents of thefor
loop only if the current character is greater than or equal ‘>=’ to the character ‘a’ and less than or equal ‘<=’ to the character ‘z’. By doing so, we can ignore encrypting and decrypting exclamation points, commas, spaces, and other special characters.
Don’t forget how this program works:
- First, the program asks for the user’s name.
- Next, we must decide if the message must be encrypted or decrypted by typing
1
(for encryption) or2
(for decryption). - Finally, we type in the message to encrypt or decrypt.
Create your own secret messages, or decrypt those of your peers!
Warning: in this program we should always write the strings and characters in lowercase letters.
Remember that you can always go back to the previous activities to review anything!