Discussion 4-6

Homework Tips

cout and escape sequences

So far we've learned how to print to the console using "cout". We're going to look through two programs here.

  cout_simple.cpp
  cout_with_escape_chars.cpp

Practice Problems: Find the error

Below are some programs that have simple errors. Try doing some combination of:

  1. Find the mistakes just by looking at the code
  2. Copy-paste into Visual Studio, and use their help to find mistakes
  3. Build the program in Visual Studio and try to understand the error message it gives us.
    • Sometimes this is pretty hard. Google is often helpful.

Problem 1

using namespace std;

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

Problem 2

#include<iostream>
using namespace std;

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

Problem 3

#include<iostream>
using namespace std;

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

Problem 4

#include<iostream>

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

Problem 5

#include<iostream>
using namespace std;

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

Problem 6

#include<iostream>
using namespace std;int main()

{
     cout << "Luke, I am your father." << "\n"
     return 0;
}

Problem 7

#include<iostream>
using namespace std;

int main() {
     cout >> "Toto, I've got a feeling we're not in Kansas anymore." >> "\n";
     return 0;
}

Problem 8

#include<iostream>
using namespace std;

int main() {
     cout << "You talking to me?"\n;
     return 0;
}

Problem 9

#include<iostream>
using namespace std;

int main() {
     cout << "What we've got here is failure to communicate." << \n;
     return 0;
}

Problem 10

#include<iostream>
using namespace std;

int main() {
     cout << I love the smell of napalm in the morning. << "\n";
     return 0;
}

Problem 11

#include<iostream>
using namespace std;

int main() {
     Cout << "Love means never having to say you're sorry.\n";
     return 0;
}

Problem 12

#include<iostream>
using namespace std;

int main() {
    cout << "You don't understand!\n";
	  << "I coulda had class.\n"
	  << "I coulda been a contender.\n"
	  << "I could've been somebody, instead of a bum, which is what I am.\n";
	return 0;
}

Variables and User Input

So far we've learned how to use cout to write statements to the console. But as of right now we can only print things that we've manually typed out ourselves in the code. Today we're going to start talking about taking user input in our programs. This will let us do many more interesting things.

Example Program: Declaring Variables

In order to take input from a user, we need to talk about variables. We use variables to store pieces of data that are important to our program. Here's an example:

#include<iostream>
using namespace std;

int main() {
    int x;  // Declares x to be a variable that can store an integer.  See note below.
    x = 4;  /* "Assignment operator". Take the value on the
            right-hand side, store it in the variable on the left.
            After this x will be 4.  */
    
    cout << x << "\n";   // Will display 4 on the console
    
    x = x + 2;   // Changes the value of x to 6  (x = 4 + 2)
    cout << x << "\n";   // Will display 6 on the console

    return 0;
}

The line "int x;" is called declaring the variable x. This tells our program to reserve enough memory on your computer to store an integer. For the rest of the program we refer to this integer as x. You must declare a variable before you can use it, and each variable should only be declared once (if we write "int x;" again somewhere else in our code we will get a compiler error).

When we first write "int x;", the variable x is uninitialized. This means that we haven't stored any data inside x yet. In practice, this usually means that some crazy nonsense value is stored inside x. In the next line "x = 4;" the crazy nonsense is erased and replaced with 4.

Example Program: Declaring Variables and cin

In this program we use the cin command to accept input from a user.

#include<iostream>
using namespace std;

int main() {
    int num;  // Declares the variable num. See notes.

    cout << "Please input a number: ";

    cin >> num;  // Asks the user to input a number via the console.
                 // Stores that number inside the variable num.

    cout << "Your number was " << num << ". After multiplying by 3, your number becomes " << (num * 3) << ".\n";
    return 0;
}


Things to try (for fun):
(1) Give bad input. When we declared the variable "num", we said that it stores 
    integers. Try giving the program letters, words, special characters, weird 
    mixes of numbers and letters, etc. It may be fun to see what it does.
(2) Initialize num (set it equal to some actual number), then repeat (2)

Example Program 2:

I wrote this program because I want to point out a few things. Let's take a look:

#include<iostream>
using namespace std;

int main() {
    cout << "Enter two numbers:\n";
    int width;
    int height;
    width = 0;
    height = 0;
    cin >> width >> height; // This line will ask for two numbers. You enter the first, press ENTER, then enter the second.
    cout << "If a rectangle has width " << width << " and height " << height << " then it has area " << (width*height) << ".\n";
    return 0;
}

Think about:
This program uses two variables, width and height. What are the scopes of these 
variables? In other words, on which lines of the program can I use these variables?
Also, what values are stored in these variables at different positions in the code?

Practice code-writing problems (if we have time)

Solutions will be posted below at the end of the discussion.

Problem 1

Write a program that asks for the user's birth year, and tells the user how old he/she will be turning this year.

Problem 2

Write a program that asks for a worker's hourly wage and hours worked, and prints how much money the worker has earned.

Problem 3

Write a program that asks for a worker's hourly wage, regular hours work, and overtime hours worked, and prints how much money he/she has made (assume the worker's overtime wage is 1 1/2 times the normal wage).

Note: I'm actually cheating with this problem. We have only talked about integers so far, but the 1 1/2 above might introduce some decimals into your problem. Try to do it anyways!