1: C++ and Basic Input-Output

Housekeeping

Announcements

See Announcements link in Canvas to keep up with what is going on. Here are a few to get started:

1.1: Orientation

In this section we discuss basics of the course for those who have not completed the Orientation module in Canvas.

1.1.1: Course Overview

Information + Practice

Information + Practice

Learning to Program

1.1.2: Required Materials

Computer and Internet

If occasional Internet problems then we will work around the issue. Make sure to take screenshots to document situations.

Textbook

Title(s): ZyBook for CS-11 Introduction to Programming Concepts and Methodology, C++.
We will be doing extensive work in the book. To get your zyBook:

  1. Create an account at zyBooks.com
  2. Enter zyBook code: CABRILLOCS11ParrishFall2020
  3. Click "Subscribe" for School term

For technical issues email support@zybooks.com. If purchase is a financial hardship please email instructor.

Canvas

Make sure to setup Canvas including:

1.1.3: Assignments

Pair Programming

Supplemental Instruction

1.1.4: Class Meetings

Cooperative Quizzes

Problem Solving

1.1.5: Using Zoom

Setup

Answering Questions

Toolbar

Yes and No Buttons

Drawing on the Screen

Survey

Operating system list

1.2: Developing Questions

Part of the reason to meet twice a week is to build community. Today we are going to start with a short exercise with a breakout meeting.

Prework (3m)

  1. Take out a pen and paper or open a text editor on your computer.
  2. Pause and think about what you are starting with this course.
  3. Make a quick list of things you are uncertain about the course.
  4. Underline or mark (*) 1 or 2 of the uncertainties that stand out to you the most.

Preparing a question (3m)

Take your pre-work, select a stand-out moment, and write your question.

For example:

I want to know [something about the course].

Question Sharing

We will share our questions with small groups of people, which I will call teams.

Breakout Rooms (10m)

  1. Read your question
  2. Team members listen, discuss the question and see if someone knows the answer
  3. If no one knows the answer then mark it for sharing with the class
  4. Repeat for all team members
  5. Return to large group for voluntary shares when time is up

No critiques and no ritual apologizing. You will be asked to go to a room so click on the button to go to the room. In your rooms, please turn your camera on and unmute yourself. If you are not following along in Canvas, take a picture of these instructions.

Voluntary Shares

For sharing, put a questions in the chat. Once you've written something go ahead and scroll back in the chat and see what people are saying. We will go through the questions and the instructor will answer them aloud as time permits.

1.3: Getting Started with C++

In this section we start programming in C++.

1.3.1: C++ Overview

1.3.2: Example C++ Source Code

Code for Hello World
1
2
3
4
5
6
7
#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!\n";
    return 0;
}

Brief Explanation by Line Number

  1. Adds a library (pre-written code) to our program
  2. All the standard libraries use the std namespace
  3. A blank line we add to separate blocks of text
  4. the main() function where all C++ programs start
  5. The output statement (our algorithm)
  6. A statement to end the main() function
  7. The closing brace of the main() function

1.3.3: Entering and Saving Source Code

To save programs, we use a text editor. Do NOT use word processing programs like Word, LibreOffice or OpenOffice for code. Some of the text editors we may use are:

If you use TextEdit on the Mac, make sure to set it to save in plain text mode. Google textedit plain text.

1.3.4: Compiling and Running Code

To compile, run and save programs without the textbook, I recommend one of the following options:

Let us compile and run the following program using the Repl.it option above. Here is an example program we will compile and run:

#include <iostream>
using namespace std;

int main() {
    cout << "Enter a number: ";
    int num;
    cin >> num;
    cout << "Hello " << num << endl;
    return 0;
}

To compile our code we will use the following command:

g++ -Wall -Wextra -Wpedantic -o main main.cpp

Where the parts of the command mean:

1.4: Problem Solving

In this section we practice solving problems.

Exercise 1.4.1: Elementary C++

In this exercise we examine the basic elements of a C++ program starting with the following code.

Code for Hello World!
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
    CS-11 Prints a message to the screen.
    @file helloworld.cpp
    @author Ed Parrish
    @version 1.0 8/30/05
 */
#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!\n";
    return 0;
} // end of main function
  1. Start Repl.it and enter the above code:
  2. Compile the code using:
    g++ -Wall -Wextra -Wpedantic -o main main.cpp
    

    If you have problems, ask for help as needed.

  3. Run the code and verify you get the message, "Hello, World!"
    $ ./main
    Hello, World!
    
  4. Try changing the message to personally greet you with your own name, like the following:
    Hello, Ed Parrish!
    
  5. The code that starts with /** and ends with */ is known as a block comment. Comments are parts of code that are ignored by the compiler. Delete the entire block comment and then recompile and rerun your code.

    You should see no change in how your code compiles or runs. If you see a difference, ask for help as needed.

  6. Look at the following line of the code:
    } // end of main function
    
    The last part of the line is another type of comment that starts with // and lasts until the end of the line. Delete the comment and then recompile and rerun your code.

    You should see no change in how your code compiles or runs. If you see a difference, ask a classmate or the instructor for help as needed.

  7. Remove the directive using namespace std; and then try to recompile the code.

    Your program should not compile and you should get an error message. If you have a different experience, ask for help as needed.

  8. Restore the directive using namespace std; back into your program and verify that it compiles.

    If you have problems, ask for help as needed.

  9. Copy the file into a text editor and save the file as "hellome.cpp".
  10. Submit your hellome.cpp file to Canvas.

Exercise 1.4.2: Read multiple user inputs

In this exercise we get two user inputs for a program.

  1. Start Repl.it
  2. Add the following after line 1:
    using namespace std;
    
  3. Declare two int variables: birthMonth and birthYear.
  4. Write two cin statements to get input values into birthMonth and birthYear.
  5. Write a statement to output the month, a dash, and the year; end with newline.
  6. Compile the code using:
    g++ -Wall -Wextra -Wpedantic -o main main.cpp
    

    If you have problems, ask for help as needed.

  7. Run the program to verify correct output. For example, if the user enters 1 2000 the output is:
    1-2000
    
  8. Copy the file into a text editor and save the file as "input2.cpp".
  9. Submit your input2.cpp file to Canvas.

Remember that when submitting multiple files to Canvas, you must submit both in the same session.