CS 70

Homework 1: Coding Part: More Fun With Fonts

Now that you've had a chance to read through, and hopefully understand, fontfun.cpp, in this part you'll practice some C++ coding by extending fontfun with new features!

  • Duck speaking

    Hey, there are a lot of helpful hints at the bottom!

  • LHS Cow speaking

    Yes. They cover common issues students have when doing this assignment—be sure to check them out!

Task 1: Input From a Text File

In the given version of the code, fontfun prompts the user for custom text to display, which the user inputs directly in the terminal. You will edit the code to also accept input from a text file.

To do this, you should add a new "special case" in the input handling logic. Right now, there is one special case: the string "quit", which triggers the program to exit. You should add an additional check for the string "file". If the program sees this special case, it should then prompt the user to enter a file name. The program will then open this file and read a line of text from it (you can assume that the specified file is a plain text file which contains one line of valid text to display).

We have provided a sample file sample.txt for you to test with. If you have correctly finished this part, the following interaction should work:

Enter text to display (or 'quit' to exit): file
Enter the name of the file to load: sample.txt

Your text in ZX Spectrum font:

  ####                                                                    #  #                            #  #  
 #    #   ###    #   #            ###     ###    #   #    ###             #  #   ## #     ###     ###     #  #  
 #       #   #   # # #           #           #   #   #   #                       # # #   #   #   #   #          
 #       #   #   # # #            ###     ####   #   #    ###                    # # #   #   #   #   #          
 #    #  #   #   # # #               #   #   #    ####       #                   # # #   #   #   #   #          
  ####    ###     # #            ####     ####       #   ####                    # # #    ###     ###           
                                                  ###                                                   

Helpful Hints

Adding the file name prompt

To implement the additional prompt asking the user to enter a file name, you can mostly just pattern it after the existing text prompt we've already provided. Just remember to save the file name to a separate variable, as you will need to use it!

Reading text from files

A neat thing about the getline function (which the given code already uses to read text from user input) is that it works on all kinds of input, including files! The only trick is that you first need to open the file. We'll help you out for that part: you can use the following code to open a file for reading, assuming that the file name is stored in the variable filename:

std::ifstream file(filename);

At a technical level, what this actually does is create a variable called file whose type is ifstream. You can think of an ifstream as something that "feeds" text from a file into the C++ program—the same way that cin "feeds" text from the terminal into the program!

Based on these hints, we'll leave it to you to figure out exactly how to call getline to grab the actual text from the file.

  • RHS Cow speaking

    If you get stuck, don't forget that you can ask a grutor or instructor, and that you're allowed to use online references (with attribution)!

Task 2: Modifying Text

The text that you give to the program to display is actually first processed by a function aptly named processText. Right now, this function does nothing, it just returns the original text exactly as-is. Change the code to transform the text into the alternating caps (aka "mocking Spongebob") format by implementing the following:

  • Every other character should be capitalized with toupper().
  • The other characters should be lowercased with tolower().
  • The first character should always be lowercase.
  • Don’t worry about nonalphabetic characters—calls to toupper() and tolower() won’t change them at all.
  • Example: The string h3llo world! would end up as h3lLo wOrLd!

Helpful Hints

Don't Forget to Recompile

Remember that every time you change fontfun.cpp, you need to recompile and relink it before you can run the program to test the results. (If you “fix” a bug in the code and the program doesn’t behave differently, you probably forgot one or both of these steps.)

Be Attentive to Warnings About Your Code

We recommend that you fix compiler warnings immediately, rather than waiting until just before you submit your code. Even if many warnings are about issues that don’t cause trouble in practice, warnings often mean there are serious errors in your code!

Keep an Eye on Your Code's Syntax

The cpplint program checks your code to ensure that it adheres to Google's C++ style guide, which we're using in CS 70 so your code is readable and consistently formatted from submission to submission.

You'll find that cpplint is pretty picky about how you write your code, so you should run it periodically and correct any formatting errors it calls out. Keeping on top of formatting issues will help you internalize the expected code style, and will also save you from the hassle of trying to fix dozens (or for larger programs, hundreds) of small annoying errors.

Run cpplint from the command line as

cpplint myfile.cpp

(where myfile.cpp is, obviously, replaced with the name of the file you want to style-check).

Write Comments as You Go

Writing comments as your write your code has two major benefits. First, the comments you write will help you be sure that your code is doing what you think it should be doing. Second, comments help anyone else who needs to read your code (like grutors or professors) understand what you think your code should be doing. Comments are especially useful when what you're trying to do is complicated and requires some abstract thinking to understand.

Just like dealing with compiler warnings and code style, writing comments while you're working will be much easier than trying to write them afterwards, when you might not remember exactly why you did something in a particular way.

Part 3: Run the Code on the CS 70 Server

Finally, to validate that the local development environment you've set up on your computer behaves the same as the one on the server, you should try running the code on the server!

Open a remote window in VS Code connected to the CS 70 server, then in that window, clone your HW1 repository the "normal" way (i.e., through the VS Code git interface). In the VS Code integrated terminal, try compiling your code (using the same Clang command you've been using) and running it, and confirm that it behaves exactly the same as it did on your computer.

To Complete This Part of the Assignment…

(When logged in, completion status appears here.)