Objectives
- Write some simple C++ programs
- Learn how to use the g++ compiler
- Get comfortable with Emacs
Note: this lab does not require you to submit anything, and there is no starter code in the labs repository. However, you are encouraged to add a new subdirectory to your lab repo and use it to store your work.
Exercises
1. First program
Use emacs to create a file called “first.cpp”. For its contents, write the simplest C++ program which is complete:
int main(){
return 0;
}
Save the file and exit or suspend the editor. At the shell prompt (denoted by $ hereafter), compile it using:
$ g++ first.cpp
If any error messages are output by the compiler then start emacs again, attempt to fix the problem, and then reattempt the compilation.
g++ will create an executable program named “a.out” (verify this using ls
). Run your program by typing the following at the shell prompt:
$ ./a.out
Does the actual result match your expectations? If not, consult with your instructor.
2. Hello, world!
Modify the body of main so that, when recompiled and run, your program behaves as shown below:
$ ./a.out
hello, world!
$
Note: after running your program, the new prompt should be on a new line.
3. Naming the output
Now, try changing the name of the executable program from “a.out” to hello
using the compiler’s “-o” option.
$ g++ first.cpp -o hello
This time, run the program using ./hello
instead of ./a.out
.
4. Important! Compiler flags
The g++ compiler can be told to perform extra error checking by including the following flags when invoked:
$ g++ -ansi -pedantic-errors -Wall -Wconversion first.cpp -o hello
Note: copy-pasting the above command may not work, because the
-
characters might be rendered differently in your browser. If you get an error, try erasing and re-typing the-
characters.
Run the Linux alias
command and examine its output. Try to determine if these flags have been enabled by default, each time you run g++. If you’re not sure about the result, ask your instructor.
In this course, your C++ source code must always be compiled using these four flags.
5. Break things and fix them again
The quicker you are able to recognize the potential cause of a compiler error, the quicker you will be able to get a program running and be ready to test its logic. To help you learn the kinds of messages produced, here are some experiments with syntax errors.
Make each change below, one at a time. After each change, save the source file and then attempt to compile. Try to understand the error message(s) produced. Then, fix (undo) the error, and move on to the next change.
delete the line: #include <iostream>
omit the # from the line: #include <iostream>
misspell
iostream
by omitting them
.“comment out” the line:
using namespace std;
insert a semicolon at the end of
int main()
misspell int by capitalizing the
i
misspell main by capitalizing the
m
omit the semicolon at the end of
return 0
change
return 0
to return a string, i.e. return "zero"Reverse the << operator in the output statement, so that it is >>
Replace the double quotes around "hello, world!" with single quotes
Omit the closing quotation mark for "hello, world!"
Note that the error messages you have just encountered are not an exhaustive list of those which are possible! If you encounter a message you don’t understand, ask one of the IAs or your instructor to explain it.
6. Try some arithmetic
You and some friends order pizza and decide to split the cost evenly. Write a program that defines the total cost and the number of friends, then calculates and displays the amount each person owes.
Examples:
Total cost | Number of friends | Amount each person owes |
---|---|---|
40 | 4 | 10 |
40 | 3 | 13.3333 |
60 | 7 | ?? |
Note: this seems pretty simple, but be careful! You’ll need to use the correct data types to get the right answer.