A3-Strings and Conditions

Table of Contents


Objectives

  • Work with sequences of variables
  • Work with different types of data
  • Start declaring and using string variables
  • Start calling (invoking) string member functions
  • Start using if-statements.
  • Explore pair programming.

Academic Honesty

Read the Scholastic Honesty Policy and Assignment Integrity policies of the syllabus. Here are some clarifications for this particular assignment:

  • You are encouraged to work with one other student of this class following the rules of Pair Programming for Homework Assignments. If you choose to pair program, there is a bonus applied.
  • You may not give a copy of your code to your designated pair-programming partner if you did not develop the code together.
  • You may not show your completed code to another person or look at another person's code until you complete and submit this assignment, except for code you develop together with your pair-programming partner.
  • You may get help from people other than your pair-programming partner if you get stuck, but only if they do not show or tell you the code to type.
  • Remember that the instructor performs similarity tests on programming project submissions, and copied or plagiarized code is usually very easy to detect.

Preparation

  1. Make sure you have completed the exercises from lesson 3.
  2. Complete the Review Exercises in CodeLab 3. These exercises will help prepare you for the problem-solving programs and should be completed first.

Project Specifications

Your solutions to these project must only use techniques we have covered so far.

Programming Style

For all programs, remember to follow all the style rules we covered including:

  1. File block comments.
  2. Placement of curly braces and indentation within curly braces, including if-else-if-else Formatting.
  3. Limiting line length to about 80 characters.

    TextPad, and most other text editors, tell you both the line and column location, which lets you check the line length.

  4. No tab characters in your code.

    You can remove tab characters by either setting up TextPad correctly (see here) or by running a program named astyle (see here).

  5. Spaces before and after operators.
  6. Meaningful variable names and consistent naming style (caps vs. underbars).

What Part of
antidisestablishmentarianism
Don't You Understand? ☺

Project 1: Working with a Long Word

According to the Oxford English Dictionary, one of the longest words used in the English language is antidisestablishmentarianism [1].

Most of the time, "longest" words are merely made up and not really used in the English language (except as examples of the longest word) or are technical words. However, antidisestablishmentarianism, at 28 letters, is a non-technical word that is found in many dictionaries and is occasionally found in genuine use. [1, 2]

The formal names of DNA and chemical compounds are almost unlimited in length. Dictionary writers tend to regard such names as "verbal formulae", rather than as English words. [3]

Project Specifications
  1. Start by downloading the worksheet: longword.cpp.

    Add to the existing code to complete the project. Leave the existing code unchanged, except where indicated in the comments.

  2. Name the source code file longword.cpp and include all your code in this single file.

    Be careful of the spelling, including capitalization, as you will lose points for a misspelled name. Naming is important in programming.

  3. Add your name and the date to the file comment block at the top of the file where shown in the comments.
  4. Do NOT add any user input (cin) to this project. If you have a cin statement in your code, you will get a low score.
  5. Using the length() function, print the length of the word where indicated in the comments. See the Example Run to verify correctness.
  6. Print the first and last indices of the word where indicated in the comments. See the Example Run to verify correctness.
  7. Using the substr(index, numChars) function, print the first and last letters (characters) of the word where indicated in the comments. See the Example Run to verify correctness.
  8. Using the substr(index, numChars) function, print at least 4 different but valid shorter words you find as a single substring (same order as in the word and without skipping letters) of the word antidisestablishmentarianism, each on their own line without extra ornamentation. For example, write a cout statement like this:
    cout << longWord.substr(0, 1) << endl; // a
    

    Include a comment, as shown above, that states what word you are trying to print. Verify your words are valid by looking them up in the Oxford English Dictionary. Valid words must be complete words and may NOT be abbreviations, acronyms, initialisms, informal (slang), proper nouns (names of things), or non-English words. Do NOT print invalid words.

  9. Example Run: The outputs of the program must look like the following for full credit, including the same order of words in the output.
    ***Long Words Worksheet***
    The length of antidisestablishmentarianism is 28
    The first letter index is 0 and the last letter index is 27
    The first character is a
    The last character is m
    Four or more words contained inside antidisestablishmentarianism are:
    a
    (4 or more words listed here, each on their own line like the one above)
    

    You may print the example word a but must add at least 3 more.

  10. After displaying the output, exit the program.
  11. Submit this project with the rest of the assignment as described in Deliverables.
References and More Information
  1. antidisestablishmentarianism: Oxford English Dictionary
  2. Antidisestablishmentarianism: Wikipedia article
  3. What is the longest English word?: Oxford English Dictionary

Image
Image source

Project 2: Mad Libs

Mad Libs is a word game where one player prompts others for a list of words to substitute for blanks in a story, before reading the story aloud [1]. When prompted, you insert a specific type of word. Once all words are added, the story is read.

In this project, we will write a computer program to play a game of madlibs with a human player. The computer will prompt for the words (and numbers) and display the story in the terminal window.

Project Specifications
  1. Write a program that plays the game of Mad Libs.
  2. Name the source code file madlibs.cpp and include all your code in this single file.

    Be careful of the spelling, including capitalization, as you will lose points for a misspelled name. Naming is important in programming.

  3. Ask the user for the following inputs (and no other input) in this order, as shown in the Example Run below:
    1. Your first name
    2. Your instructor's last name
    3. A food
    4. A number between 100 and 120 (double)
    5. An adjective (describes something or somebody like lumpy, soft, ugly, messy, or short)
    6. A color
    7. An animal
    8. A second adjective
    9. A whole number (int)
  4. After the strings are entered, substitute the strings into the following story and output the story to the terminal window as shown in the Example Run below:

    Dear Instructor [Instructor's Last Name]

    I am sorry that I cannot turn in my homework today. First, I ate a rotten [food], which made me turn [color] and very sick. I came down with a fever of [number]. Next, my [adjective] pet [animal] must have smelled the remains of the [food] on my homework, because my pet ate my homework.

    I knew I should not have ordered that [adjective] [animal] on eBay!

    I am beginning to feel [second adjective] and was hoping you would accept my homework only [whole number] days late.

    Sincerely,
    [Your name]

  5. Insert line breaks into the output to make the text readable and presentable within an 80 character screen for reasonably sized input words.
  6. Example Run: The input prompts and outputs of the program must look like the following for full credit, including the same order of input and exact wording of the output. For the input shown you must get the same output. However, the output must change properly if the inputs are different.
    Welcome to Mad Libs!(R)
    
    Enter your first name: Ed
    Enter your instructor's last name: Parrish
    Enter a food: hamburger
    Enter a number between 100 and 120: 115
    Enter an adjective: greasy
    Enter a color: green
    Enter an animal: frog
    Enter another adjective: clean
    Enter a whole number: 42
    
    Dear Instructor Parrish,
    
    I am sorry that I cannot turn in my homework today.
    First, I ate a rotten hamburger which made me turn green and
    very sick. I came down with a fever of 115. Next, my
    greasy pet frog must have smelled the remains of the hamburger
    on my homework, because my pet ate my homework.
    
    I knew I should not have ordered that greasy frog on eBay!
    
    I am beginning to feel clean and am hoping you
    will accept my homework only 42 days late.
    
    Sincerely,
    Ed
    

    In the above example run, the user entered the values shown in aqua italics (for emphasis) to produce the output. Your program does NOT print the characters in aqua italics, nor does the user input appear in aqua italics.

  7. After displaying the output, exit the program.
  8. Submit this project with the rest of the assignment as described in Deliverables.
References and More Information
  1. Mad Libs: Wikipedia retrieved 1/14/2017
  2. The History of Mad Libs

Image
Image source

Project 3: Coffee Chatbot

For this programming assignment, we write a basic chatbot program. For fun, try having a conversation with an online chatbot here or here. Some sources estimate that 25% of customer services will be handled by Chatbots in 2020, vs. 2% in 2017 (1).

In this project we write a program for a cafe to order coffee. The chatbot must ask the user for the following information:

  1. What is your name?
  2. What type of coffee (1-5)?

The type of coffee is selected from a menu as described below.

Project Specifications
  1. Write a chatbot program that interacts with the user as described in the Interaction section below to process a customer's coffee order.
  2. Name the source code file coffeebot.cpp and include all your code in this single file.

    Be careful of the spelling, including capitalization, as you will lose points for a misspelled name. Naming is important in programming.

  3. Interaction: Interact with the user with the following inputs in the order given (and no other input) and provide responses as follows:
    1. Name: open the conversation with a welcome and find out the user's name (string). Reply with a greeting that displays the user's name, like "Thanks for using CoffeeBot <user's name>!".
    2. Coffee type: Display the following menu and ask the user what type they want (int).
      1. Espresso     $1.80
      2. Americano    $1.90
      3. Cafe' Latte  $2.90
      4. Cappuccino   $3.20
      5. Cafe' Mocha  $3.40
      What type of coffee (1-5)?
      

      The numbers, names and prices are all part of the menu display.

    3. Coffee name: After the user enters the type, respond by spelling out the coffee name on its own line like, "One <coffee name> coming right up!".
    4. Comment: After the coffee type, add an extra comment about the type on its own line. Each coffee type must provide a unique message and messages for different coffee types must differ.
    5. Total price: After the unique message, state the total price with a tax of 10% added on its own line.

    The parenthesis above show the required data types for the input. The angle brackets show where the program prints various information like the user's name.

  4. Assume the user enters only valid data.
  5. Example Runs: The following shows example runs. Your program prompts and output does not have to exactly match mine. Your program does need to reply to all input as stated in Interactions above, but you are free to give your chatbot a personality. Make sure the inputs are in the correct order and that your program provides the required output. Otherwise, have fun with it!
    Welcome to CoffeeBot!
    What is your name? Ed
    Thanks for using CoffeeBot Ed.
    Here are our coffees:
    1. Espresso     $1.80
    2. Americano    $1.90
    3. Cafe' Latte  $2.90
    4. Cappuccino   $3.20
    5. Cafe' Mocha  $3.40
    What type of coffee (1-5)? 1
    One Espresso coming right up!
    That's a real eye opener!.
    With tax, that will cost you $1.98.
    Enjoy your coffee!
    
    Welcome to CoffeeBot!
    What is your name? Emma
    Thanks for using CoffeeBot Emma.
    Here are our coffees:
    1. Espresso     $1.80
    2. Americano    $1.90
    3. Cafe' Latte  $2.90
    4. Cappuccino   $3.20
    5. Cafe' Mocha  $3.40
    What type of coffee (1-5)? 3
    One Cafe' Latte coming right up!
    Steamed milk with espresso to liven it up!.
    With tax, that will cost you $3.19.
    Enjoy your coffee!
    

    In the above example runs, the user entered the values shown in aqua italics (for emphasis) to produce the output. Your program does NOT print the characters in aqua italics, nor does the user input appear in aqua italics.

  6. After displaying the output, exit the program.
  7. Submit the source code file coffeebot.cpp with the rest of the assignment as described in Deliverables.
References and More Information
  1. PYMNTS.com
  2. Chatbots Life
  3. 12 Different Types Of Coffee Explained
  4. Explore coffee bot GIFs: for inspiration on comments
  5. Robot baristas serve coffee at cafe

Extra Credit

The following are worth extra credit points:

  1. Complete the assignment using pair programming with the same person for all three projects. (2 points)
  2. Complete the longword.cpp program and list seven (7) or more valid words contained within antidisestablishmentarianism. (1 point)
  3. Attend an SI session for this assignment, sign the roll sheet and type in your README.txt, "attended SI" followed by the date(s) attended. (2 points)

Make certain that your README.txt file describes any extra credit completed.

Tutorial Lab

In preparation for next weeks lessons, complete the following:

  1. Read the assigned reading in the textbook
  2. Complete the Tutorial Exercises in CodeLab 3 before the specified due date.

    Refer to the assigned reading for the next lesson to help you understand the problems. Also, you can use the online lecture notes for more information as the notes become available. You can look at solutions if you miss your first few attempts and are stuck by clicking the "Solution" tab.

Grading Criteria

The instructor will evaluate your assignment using the following criteria. Thus you should check your assignment against these criteria to maximize your score.

Each criteria represents a specific achievement of your assignment and has a scoring guide. The scoring guide explains the possible scores you can receive. Some scoring guides have a list of indicators. These indicators are a sign of meeting, or a symptom of not meeting, the specific criterion. Note that a single indicator may not always be reliable or appropriate in a given context. However, as a group, they show the condition of meeting the criterion.

For information on grading policies, including interpretation of scores, see the syllabus.

Lesson Exercises

  • 2: All lesson exercises attempted and turned in
  • 1: Some lesson exercises completed and turned in
  • 0: No lesson exercises completed or turned in

Projects (x3)

  • 5: Demonstrates mastery of the program
    • Applies concepts from the lessons appropriately
    • Meets all specifications (see above)
    • Runs to completion with no abnormal error conditions
    • Generates correct output given correct input
    • Correct file name
  • 4: Has most of the functionality expected of the program
    • Demonstrates some techniques from the lesson
    • Attempts to meet all but one of the specifications (see above)
    • Implementation seems more complicated than necessary.
    • May have one minor error
  • 3: Has some of the functionality expected of the program
    • Demonstrates some techniques from the lesson
    • Attempts to meet at least 1/2 of the specifications (see above)
    • Implementation seems excessively complicated.
    • May have 2-3 minor errors
  • 2: Serious functional problems but shows some effort and understanding
    • Attempts to meet less than 1/2 of the of the specifications (see above)
    • Has a major error or many minor errors
    • Implementation seems very convoluted
    • Demonstrates few techniques from the lesson
  • 1: Does not compile or wrong file turned in
  • 0: Not turned in or uses techniques not covered

Project's Style

  • 3: Code is well-documented including:
  • 2: Code has a minor documentation error
  • 1: Code has some documentation errors
  • 0: No apparent attempt to follow documentation standards or write documentation comments

CodeLab Exercises

  • Number CodeLab completed correctly / number exercises * 8 and rounded up to the nearest integer.

README.txt File

  • 2: README.txt file submitted following the instructions
  • 1: README.txt file submitted but some information was missing
  • 0: No README.txt file submitted

Total possible: 30, plus extra credit

Deliverables

Students submit some homework as they work on it like CodeLab. However, students must submit other homework in Canvas following the link to A3-Strings and Conditions. Include the following items when submitting to Canvas:

  1. README.txt file prepared by following the instructions for submitting homework.
  2. All the exercise files from Lesson 3 including:
    • nameapp.cpp
    • selection.cpp
    • students.txt
  3. longword.cpp
  4. madlibs.cpp
  5. coffeebot.cpp

Note: Make certain your programs compile before you turn them in. When a program does not compile then it does not function either. For all programming projects, you should expect little or no credit if your program does not compile and run. For more information see the Grading Criteria.

You must submit all the files needed to complete your assignment. Your assignment must work as submitted. Remember to test and double check your files before submitting them. If you make a mistake, you can resubmit up to the deadline. If you resubmit, you must include all your assignment files in the last submission as Canvas hides prior submissions.

Last Updated: September 22 2019 @20:36:34