LAB 13


Software tools needed: web browser and the C++ compiler, gcc and a graphical editor, such as gEdit.

In-class Quiz

During lab, there is a quiz. The password to access the quiz will be given during lab. To complete the quiz, log on to Blackboard (see Lab 1 for details on using Blackboard).

Variables in C++

In C++ (and C, Java and many other languages), you must first state (or ‘declare’) a variable’s name and its type before using. For example,

int num;

declares a variable, called num of type int (integer or whole number). If you do not request the space in advance in C++, you will get an error. This requirement (as well as the “typing” of variables) is to avoid errors where misspellings access the wrong variable and are never caught. It is also makes the underlying design of the compiler (the program that converts your program to an executable file) simpler.

There are many types of variables availabe in C++. Here are a few that we will use:

Note: we won’t discuss strings in C++, but see the reading if you’re interested (will be covered in detail in CSCI 135/156). Here is a chart with a few more common types.

Decisions in C++

As with Python, we can make decisions that take different branches through the code. The basic structure is:

if (condition)
{
  ...
}
else
{
  ...
}

The syntax of Python and C++ are very similar, but there’s a few differences to keep in mind when translating from Python to C++.

Here’s a sample program. Read through it first, and guess what it does:

//Name:  CSci 127 Teaching Staff
//Date:  November 2017
//A program demonstrating if-else statements in C++
#include <iostream>
using namespace std;

int main() 
{
  int num;
  cout << "Hello!";
  cout << "Enter a number: ";
  cin >> num;
  if (num % 2 == 0)
  {
    cout << "Even number!\n";
  }
  else
  {
    cout << "Odd number\n";
  }
  return 0;
}

Let’s look in more depth at the code:

We start by greeting the user and reading in a number:

int num;
cout << "Hello!";
cout << "Enter a number: ";
cin >> num;

Since we declared the type to be int, when we read in the number, we do not have to cast it to an integer. Instead, it is converted automatically to be a whole number.

Much of the arithmetic carries over from Python. The code above uses the modulo, or remainder, operator to test if the number entered is even or odd and prints a message accordingly.

if (num % 2 == 0)
{
    cout << "Even number!\n";
}
else
{
    cout << "Odd number\n";
}

Note that C++ uses the curly brackets for blocks of code, but that it is ‘good programming style’ to indent your code to make it readable. As a rule of thumb, if you would be required to indent in Python, you should indent in C++ (not required for the code to run, but ‘good style’).

How would you modify this program to print out a different message based on the time of the day? For example, if the user entered the hour (in 24-hour time), you should print out “Good Morning” if it before 12, “Good Afternoon” for 12 until 17, and “Good Evening “ otherwise. See Programming Problem List.

Indefinite Loops in C++

Now that we have discussed conditionals and definite loops, we can introduce indefinite loops. They follow a similar format to those in Python:

while (...condition...)
{
  command1;
  command2;
  ...
}

Again, we do not have the colon (:) at the end of the line with while, but we do end individual commands with a (;).

Take the Python code from Lab 10 that asks the user to enter their age (and continues checking until they enter a non-negative number):

# errors.py is based on dateconvert2.py from Chapter 5 of the Zelle textbook
# Converts day month and year numbers into two date formats

def main():
    # get the day month and year
    day, month, year = eval(input("Please enter day, month, and year numbers: "))

    date1 = str(month)+"/"+str(day)+"/"+str(year)

    months = ["January", "February", "March", "April", 
	      "May", "June", "July", "August", 
	      "September", "October", "November", "December"]
    monthStr = months[-1]
    date2 = monthStr+" " + str(day) + ", " + str(year)

    print("The date is", date1, "or", date2+".")

main()

and translate it into C++. An easy way to approach this is to start with the C++ program above that demonstrates if statements and modify it to handle tbe indefinite loop for checking ages. See Programming Problem List.

What’s Next?

If you finish the lab early, now is a great time to get a head start on the programming problems due early next week. There’s instructors to help you, and you already have Python up and running. The Programming Problem List has problem descriptions, suggested reading, and due dates next to each problem.