Assignment 14
Due Tuesday, November 15 at 3:20pm on Catalyst


Assignment 14.1: The Right Angle, Part 2 (10pts)
  • Let's improve upon Right Angle, Part 1!
  • For this version we will be adding:
    • 1 do-while loop
    • 1 while loop
  • Open up triangle.cpp.
  • Now, add a while loop to test cin.fail() after you prompt the user for the base variable. While the user enters an inappropriate value for the base of the triangle, the program will continue to prompt him or her for a correct value. This part of the program should work like this:
  • Don't forget to include cin.clear() and cin.ignore(1000,\n); as part of your while loop. For more information, see the relevant section of today's lesson.
  • Next, let's alter our while loop that is controlling whether the program repeats.
  • Change this loop to a do-while loop to your program to ask the user whether he or she wishes to print more triangles or terminate the program. 
Would you like me to print more triangles for you (y/n)? _
  • In addition to "y", the program should now also accept "Y", "yes", or "Yes" as correct input to continue printing more triangles.
  • Hint: You will need to use boolean operators (&&, ||) in the condition of your do-while loop to allow for multiple options.
  • The final version of your program should work identically to the sample output below (except user input will vary).
  • I have provided an example of all of the functionality in my example so please observe it carefully.
  • When finished, upload your assignment to Catalyst.

The output of your program should look identical to the sample output below:


Assignment 14.2: Lower and Upper Case (10 pts)

  • Let's write a program to do the following:
    • convert a string to all lower-case letters
    • convert a string to all upper-case letters
  • To approach this problem, we need to remember the ASCII table and how it works.
  • Recall that upper case letters have ASCII values between 65 and 90.
  • Lower case letters have ASCII values between 97 and 122.
  • Verify that these facts are correct by looking at the ASCII Table.
  • Finally, note that lower case 'a' is the integer number 97 and upper case 'A' is the integer number 65.
  • Therefore, there is a difference of 32 between the ASCII value of any lower case character and its upper case version.
  • We will use these facts to our advantage in this program.
  • Open up CodeBlocks and create a new file called upperLower.cpp.
  • Copy and paste the starter code below into your file:

/*
*name
*section
*/

#include <iostream>
using namespace std;


int main () { 

    string sentence;   

    return 0;

}


  • Add some code to welcome the user, prompt the user to enter a sentence and then store the sentence as a string variable.
  • Verify that your program compiles and runs.
  • Now, let's add some statements.
  • We will need to use a for loop, string indexing and our knowledge of ASCII to complete this task.
  • Write a for loop to cycle through our string variable that was passed in as a parameter to our function.
for (int i = 0; i < sentence.length(); i++) {
    //statements for string processing go in here
}
  • Inside the loop, we need to find out if this is a capital letter or not. We will use an if statement to test whether the ASCII value lies between 65 and 90.
if (sentence[i] >= 65 && sentence[i] <= 90) {//if this character is between 65 and 90, it is a capital

}

  • What should go inside the if statement?
  • Consider that each upper case character has an ASCII value that is 32 less than each lower case character.
  • So, if it is an uppercase letter, we will need to add 32 to its ASCII value and then convert it back to a char.
  • Add the below statements inside the curly braces of the if.
sentence[i] += 32;
//adding 32 to sentence[i]'s ASCII value
  • Now, run your program and verify that it works correctly by looking at the output of the test in main.
  • For the rest of the program, your job is to write the remaining for loop to convert the sentence to all upper case letters.
  • When you are finished, submit your work to Catalyst

Your output should look identical to the output below: