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++.
Write a C++ program that:
For example, when we run our exercise and the user inputs the number 100, the interaction with the terminal should look like:
$ ./ex5 Which positive integer would you like me to factorize? 100 1 2 4 5 10 20 25 50 100 $
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
.
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.
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!
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;
.
You will submit:
ex5.cc
.
Your code must:
attu
, or CSE home VM).g++
and valgrind
).$ g++ -Wall -g -std=c++17 -o ex5 ex5.cc
.cc
file with
your name(s) and CSE or UW email address(es).cpplint.py
).Submit your code on . Don't forget to add your partner if you have one.