$30
PA 7: Intro to C++
Programs that do not compile will receive a grade of zero.
There's no makefile for this lab. ☺
C++ style input and output is required throughout this lab.
The following C language features are prohibited:
printf
scanf
C library names
\t in output statements
\n in output statements
Guidelines:
Use appropriate, meaningful names for all variables and functions. Use camel case for names of
variables and functions. Use all caps for names of constants.
SSH Remote Login to Linux Host:
For the C++ assignments you are required to do the following:
1) Use Bitvise SSH or other SSH app to connect to a CS Linux host computer.
2) Use Bitvise FTP or other FTP app to transfer files to and from a Linux host computer.
3) Use Kate or other editor to create and edit your C++ programs and makefiles.
4) Use the terminal (command line) for Linux commands.
5) Use the g++ compiler to compile and link the programs.
C++ Compiler:
C++ source code should be stored in a file with the extension .cpp. The C++ compiler command is
g++. For example, to compile a C++ program named myProgram.cpp and create an executable
named myProgram, you would use the command
g++ -std=c++11 myProgram.cpp -o myProgram
(Note: Those are ones, for C++ version 11, not els.)
using namespace std;
I know that chapter 15 starts by showing you how to write C++ programs without this statement, but
for all assignments in this course, you must have the statement:
using namespace std;
in your program and the prefix "std::" should not appear before words such as cout, cin, string, etc.
C++ string class:
C++ has a string class (note the lowercase s on string). The class has numerous methods and
operators for working with string objects.
To declare a string variable, use the type string.
string word;
Caution: C++ doesn't use the null character in strings.
For more information on the string class, visit this page:
http://www.cplusplus.com/reference/string/string/
Getting string input:
To input a string, use the statement:
getline( cin, word );
where word is the name of the string variable.
Outputting strings:
You can print a string just like you would any variable.
cout << word;
Input and Output
C++ uses the term "stream" for objects that get input or display output.
cin is the standard input stream (by default, this is the keyboard)
>> is called the "stream extraction operator". It's used to get (extract) data from an input source.
>> works for most data types but not for strings because it stops reading (extracting) when it reaches
any whitespace character.
To check for the end of input (EOF as we often call it), use the eof function with an input stream name.
For example,
if (cin.eof( ))
cout << "No more input available from the keyboard." << endl;
cout is the standard output stream (by default, this is the screen)
<< is called the "stream insertion operator". It's used to send (insert) data to an output device.
Formatted Output:
C++ uses functions called "I/O manipulators" to control the formatting of output. (Remember...you
can't use printf in your C++ programs for this class.)
The most commonly-used I/O manipulators are:
endl Prints a newline
setw( int w ) Sets the field width for the next data item. Not sticky... meaning not permanent.
It only affects the first next thing that you print.
setfill( char c ) Sets the fill character. The default fill character is a space. Sticky.
left Set the alignment to left. Sticky.
right Set the alignment to right. Sticky. Right alignment is the default.
fixed Sets floating point output to fixed decimal.
setprecision( int n ) Sets the precision to n digits after the decimal point.
For more on I/O manipulators, check C++ resource links in Canvas or visit these sites:
http://www.fredosaurus.com/notes-cpp/io/omanipulators.html
http://faculty.cs.niu.edu/~mcmahon/CS241/c241man/node83.html
Your first program: wordFun.cpp
Create a new file and save it as wordFun.cpp.
Add header comments as you did in previous assignments. Also, add inline comments as you write
the code.
In the main function:
Create an array of 25 strings. (If you skipped over the explanation of the string class on page
2, perhaps you should go back and read it.)
Use a sentinel loop that reads strings from cin until the array is full or the end of input is
reached (when the user presses Ctrl-D), whichever comes first. Note: Each string is only one
word and will not contain whitespace.
After the sentinel loop is over, use a for loop to move through the array. Write the loop
condition so that you don't go past the last array element that was input.
Print one array element (one string) followed by a newline
Use a for loop to move through the characters of the string you just printed
o print one character followed by a newline
Example run of the program: (blue indicates what the user types)
Type a word (press Ctrl-D to quit): hello
Type a word (press Ctrl-D to quit): data
Type a word (press Ctrl-D to quit): Ctrl-D is pressed
hello
h
e
l
l
o
data
d
a
t
a
The second program: logTable.cpp
You may copy wordFun.cpp and start with that. Change the header comments and remove the old
statements from the main.
The C++ math functions are located in the <cmath> library. They're the same function names we
used in C.
http://www.cplusplus.com/reference/cmath/
In this program you will calculate and display a table of logarithms for the numbers from 1 to 100.
Output must have:
column headings exactly as shown
right-aligned column data, located under each heading exactly as shown
3 digits of precision on float or double values
I/O manipulators are located in the <iomanip> library. See "Formatted Output" on page 3.
See below for the first portion of the output. (Your output will continue until number 100.)
Number Log base 2 Log base 10 Log base e
1 0.000 0.000 0.000
2 1.000 0.301 0.693
3 1.585 0.477 1.099
4 2.000 0.602 1.386
5 2.322 0.699 1.609
6 2.585 0.778 1.792
7 2.807 0.845 1.946
8 3.000 0.903 2.079
9 3.170 0.954 2.197
10 3.322 1.000 2.303
...
100 6.644 2.000 4.605
Submit 2 files:
wordFun.cpp
logTable.cpp
Make sure that you select the correct files and the programs are uploaded before you
submit.