CMPT 201
Include Directives #include <iostream> using namespace std; Preprocessor directive iostream is a library containing definitions of the input and output function Linker Appears at the start of the program, begin with # using namespace std; Tells the compiler to use names in iostream in a “standard” way
int main() { //beginning of the main function .... //statements return 0; } //end of the program
The output function cout c(C++ language)out(output) c(C++ language)in(input) cout<<whatever data you want to output; cout<<“Welcome to CMPT201!”; << represents the flow of the data.
Okay, let’s write a hello world program in C++ C++ is fun!
Note No extra space between < and iostream, or between the end of the file name and > #include<iostream > #include< iostream> #include<iostream> Old C++ compilers: iostream.h Statements end with a semi-colon.
Program Layout Compiler accepts almost any pattern of line breaks and indentation Programmers format programs so they are easy to read Place opening brace ‘{‘ and closing brace ‘}’ on a line by themselves Indent statements Use only one statement per line
.cpp file .exe file
More about cout <<: insertion operator cout<<“Hello World”; is equivalent to cout<<“Hello”<<“ World”; cout<<“Hello ”; cout<<“World”;
About cout Start a new line Escape sequence \n (need to go inside of the quotes) endl Escape sequence \n new line \t tab \” ” \\ \
Comments // This is a comment. /* This is a comment. This is another comment. */
Exercise Write a simple program to print to the screen the following. Be sure to add your name in the comment. Hello! I say “C++ is fun!” Good-bye!
Variables a place to hold a number or data of other types Variable names Start with a letter or the underscore Remaining can be letters, numbers or underscore x x1 3X _abc %change data-1 PROG.CPP Big_Bonus C++ is case-sensitive rate RATE Rate
Variable Names Variables are often spelled with all lowercase letters. Avoid keywords/reserved words int, double cin, cout Choose meaningful names! x, y, z distance, speed, time
Declare a Variable Syntax Example Where? type name; type name1, name2, ... ; Example int number; double weight, height; Where? before the variable is used at the start of the main function (preferred)
Assign Value to a Variable Syntax variable = value(or expression); Example a = 2; b = a; c = a + b; count = count + 1 is totally wrong in math, but in C++?
Combine Declaration and Assignment int number = 3; double rate = 0.07, balance = 0;
Example here cout<<number; cout<<“number”;
Input Function cin cout: output to screen cin: input from keyboard cout<<whatever you want to output; cin>>variable_name; cin>>variable1>>variable2; Or cin>>variable1; cin>>variable2;
Example here Give the user a prompt before the input.
Exercise Write a program that reads in two integers and then outputs both their sum and their product.
Debug Bug: a mistake in a program Debugging: Eliminating the mistake
Errors Syntax errors Run-time errors Logic errors The compiler may be wrong about the location/error! Start from the first error Warning message Run-time errors Logic errors hardest to diagnose
Modify the previous program so that it also outputs their quotient Modify the previous program so that it also outputs their quotient. Recompile the program.
Test It is important to test your program on several data sets!
Homework 1 Due next Friday before class Hard copy Source code Screen shot of test runs