Lab 03: C++ Basics

Jan 11, 2024  β”‚  m. Jan 10, 2024 by Charlotte Curtis

Objectives

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.

  1. delete the line: #include <iostream>

  2. omit the # from the line: #include <iostream>

  3. misspell iostream by omitting the m.

  4. “comment out” the line: using namespace std;

  5. insert a semicolon at the end of int main()

  6. misspell int by capitalizing the i

  7. misspell main by capitalizing the m

  8. omit the semicolon at the end of return 0

  9. change return 0 to return a string, i.e. return "zero"

  10. Reverse the << operator in the output statement, so that it is >>

  11. Replace the double quotes around "hello, world!" with single quotes

  12. 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 costNumber of friendsAmount each person owes
40410
40313.3333
607??

Note: this seems pretty simple, but be careful! You’ll need to use the correct data types to get the right answer.



Previous: Lab 02: More Linux and Emacs
Next: Lab 04: Input, output, arithmetic, and debugging with GDB