CSE 333 Exercise 8
Due: Wednesday, January 28 by 11 AM
Rating: 1 (note)
Each exercise this quarter is rated on a integer scale of 1 – 5, inclusive, with 1 being the "least time-consuming" and 5 being the "most time-consuming".
This difficulty scale is meant as a rough guide for you in predicting the amount of time to set aside for each exercise as you balance the work required for 333 with your other obligations. However, it is necessarily imperfect as everyone's set of circumstances and experiences with the exercises differ. If your experience with an exercise does not align with its rating, that is not a reflection of you or your abilities.
Goals
- Write a C++ program from scratch.
- Write code that reads in user input from stdin, and parses the input.
- Utilize streams in C++.
- Use the linter on C++ source code.
Background
As discussed in lecture, C++ and C have a lot of similarities, yet can be different in a lot of ways. In this exercise, you will write your first C++ program from scratch that, like Exercise 1, parses user input to perform some arithmetic computation. We want you to compare your experience with this exercise and Exercise 1 to see some of the similarities and differences between C and C++.
Problem Description
Write a C++ program that:
- Prompts the user for a positive integer (> 0, strictly greater than 0).
- Prints out all integers that are factors of that integer; feel free to use the simplest, brute-force factorization algorithm you can think of. You should only print each factor once, in ascending order.
For example, when we run our exercise and the user inputs the number 100, the interaction with the terminal should look like:
$ ./ex8 Which positive integer would you like me to factorize? 100 1 2 4 5 10 20 25 50 100 $
- On the second line, there is a space between the prompt and the user input ('100').
- On the third line (i.e., the factors), there is no space before the '1' or after the '100'.
Implementation Notes
Input/Output Streams
You should use the iostream library to read user input
from the terminal and to write to the terminal.
Specifically, you should investigate using std::cin,
std::cout, and std::cerr.
You can find more information by looking at
iostream in the C++ reference.
User Input
Since you will be reading in user input from stdin, you
should make sure your code handles various inputs from the user,
which may be in an unexpected format.
For this exercise, you should investigate to see how
std::cin parses input and checks for errors, and then
make sure your code utilizes this error checking for bad input.
Once you detect a bad input, please print out useful error message
and exit from the program –
do NOT try to loop and prompt the user again.
Style Focus
C++ Style
In general, write C++-style code when possible.
For example, although both printf and
std::cout can achieve the task of writing to the
terminal, you should use std::cout since this is a
C++ program.
cpplint
Since we are now dealing with C++ instead of C, you should no
longer use the --clint flag with
cpplint.py so that it does the proper checks for C++.
You may see some new warnings, so be sure to fix these as well!
Using-Directives
Make sure that if you use any using directives, that
you list out each individual object/function you use, and not an
entire namespace.
In other words, do NOT use using namespace std;,
instead list out everything you are using from that namespace with
lines like: using std::cout;.
Submission
Submit the following file(s) by creating an ex8-submit tag in your exercise repo before the assignment deadline. The file(s) should be located in the exact directory listed below, including capitalization:
ex8/ex8.cc
Your code must:
- Compile without errors or warnings on CSE Linux machines
(lab workstations,
attu, or CSE home VM). - Have no runtime errors, memory leaks, or memory errors
(
g++andvalgrind). - Be contained in the file listed above that compiles with the
command:
$ g++ -Wall -g -std=c++17 -o ex8 ex8.cc
- Note that this is a different compiler command compared to previous exercises because we are now using C++!
- You are encouraged to create your own short Makefile to improve your build experience with this exercise, but you won't be submitting one.
- Have a comment at the top of your
.ccfile with your name(s) and CSE or UW email address(es). - Be pretty: the formatting, modularization, variable and
function names, commenting, and so on should be consistent with
class style guidelines.
Additionally, the linter shouldn't have any complaints about your
code (
cpplint.py). - Be robust: your code should deal with hard-to-handle/edge cases and bogus user input (if there are any) gracefully.