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!
Hey, there are a lot of helpful hints at the bottom!
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.
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()andtolower()won’t change them at all. - Example: The string
h3llo world!would end up ash3lLo 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!
Write Comments as You Go
Writing comments as you 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: Build and Check Your Code on the CS 70 Server
Everything so far has been on your own computer. Now do the same work on the CS 70 server. There are two reasons, and the second one is graded:
- it checks that the environment you built on your own machine behaves the same as ours, which is the whole point of having built it; and
- the server is where you check your style, because it has the version of
cpplintwe grade with.
You set the server up in Homework 0, so these are the same moves as then. Log
in, go to your cs70 directory, and clone your repository there:
cd ~/cs70
git clone https://github.com/hmc-cs70-fall2026/repo .git
(
If git clone says the repository does not exist, that almost always means
you are not in the CS 70 GitHub organization yet rather than that the
repository is missing. Run cs70-fix-access on the server and it will check.
Then compile and run it with exactly the command you have been using:
clang++ -std=c++17 -Wall -pedantic -o fontfun fontfun.cpp
./fontfun
It should behave exactly as it did on your own computer. If it does not, that is worth telling us about — a difference here is interesting rather than embarrassing.
Checking your style
The cpplint program checks your code against Google's C++ style guide, which
CS 70 uses so that code is readable and consistently formatted from one
submission to the next. It is picky, deliberately: keeping on top of formatting
as you go saves you from fixing dozens of small annoying things at the end, and
along the way you stop having to think about it.
With your code compiling and running on the server, check its style:
cpplint fontfun.cpp
More generally it is cpplint followed by the name of the file you want to
check, so on later assignments with several files you will run it on each.
Run cpplint on the server, not on your own machine.
CS 70 does not use cpplint's default rules. The CPPLINT.cfg file in your
repository switches several of its checks off, and cpplint picks that file
up by itself as long as you run it from inside the repository — so you do not
have to configure anything.
You are not asked to install cpplint on your own computer, and we would
rather you did not: you would get whichever version is current the day you
install it, and it could report problems we are not asking you to fix, or
miss ones we are.
Meh. Why do I have to do the same thing twice?
Because "it works on my machine" is the oldest sentence in software, and this is the week you find out whether it is true. Doing it in two places is how you learn that a program is not the same thing as the computer it happened to run on.
What if it works on mine and not on the server?
Then you have found something genuinely worth knowing, and we would like to hear about it. That is a good outcome, not a failure.
(When logged in, completion status appears here.)