You are on page 1of 33

Chapter 1

INTRODUCTION

Welcome to C++!

A Simple C++ Program


// A first program in C++ #include <iostream> int main() { std::cout << "Welcome to C++!\n"; return 0; }

What is C++?
Has 2 parts: Preprocessor Directives Main function

Preprocessor Directives
All directive begin with # character E.g., #include , #define Why preprocessor directives?
C++ defines only a small number of operation. Most of the functions and symbols are in Libraries.

Main Function
All C++ program must have main() function. All function start with a { and end with a } symbol. It is the body of the function.

// Printing on one line with multiple statements

#include <iostream> int main() { std::cout << "Welcome "; std::cout << "to C++!\n"; return 0; }

//Printing multiple lines with a single statement

#include <iostream> int main() { std::cout << "Welcome\nto\n\nC++!\n"; return 0; }

#include <iostream> using namespace std; int main() { cout << "Welcome\nto\n\nC++!\n"; //cout << "Welcome\n // << to\n\nC++!\n";
return 0;

#include <iostream> using namespace std; int main() { int num; cout << "Please enter a number."; cin >> num; cout << "You entered: " << num << endl; return 0; } // end program

// Program that displays the sum of two numbers. #include <iostream> using namespace std; int main() { int number1, number2,sum; // variable declarations cout << "Enter first integer: "; // prompt user for data cin >> number1; // read first integer into number1 cout << "Enter second integer: "; cin >> number2; // read second integer sum = number1 + number2; // assignment of sum cout << "Sum is " << sum << endl; //display sum return 0;

Memory Concepts
Variable names such as number1, number2, and sum is correspond to locations in the computers memory. Every variable has a name, a type, and a value

Arithmetic in C++
C++ operation Arithmetic Algebraic operator expression C++ expression

Addition Subtraction Multiplication


Modulus

+ *

Division & integer / division

f + 7 p c bm x/y or x y
r mod s

f p b x

+ 7 c * m /y

r % s

Integer division / yields an integer result E.g. 7 / 4 = 1 17 / 5 = 3 Modulus operator % yields the remainder after integer division. The modulus operator is an integer operator Can be used only with integer operands. E.g. 7%4=3 17 % 5 = 2 15%0 is undefined

0/4 = 0

4/0 is undefined

Are not part of the executable program Comments are used for explaining difficult sections of code describe the program, author, date, modification changes etc. documentation of variables and their usage copyrighting All comments must start with // Symbol /* and end with */ for multiple lines

Comments

E.g. /* * Programmer: William Bell * Date completed: May 9,2003 * Instructor: Janet Smith * Class: CIS61 * * Calculates and displays the area and * circumference of a circle */ Use // for single line

Input Statement
Accepting data from the user through keyboard Example cin >> salary; //receive salary cin >> d >>f >>c; //receive 3 variable

Output Statement
Print result to the screen Example
cout << "The student is good; //print a text cout <<code <<fridge <<coat ; //print variable cout << The amount is << total; //print text and variable

insert a line cout << endl; cout << "the man is crazy" << endl; cout << total << endl; All output line can combine with escape sequence to format the output text

Escape sequence

Description

\n
\t

Newline. Position the screen cursor to the beginning of the next line Horizontal tab. Move the screen cursor to the next tab stop
Carriage return. Position the screen cursor to the beginning of the current line

\r

\\
\ \ \a

Blackslash. Used to print a blackslash character Single quote. Used to print a single quote character Double quote. Used to print a double quote character Alert. Sound the system bell

Common Programming Errors


Debugging to find any error in the program Types of Errors: Syntax errors Run-time errors Logic errors

Syntax Errors
Code violation during compilation Example: Missing semicolon Undefined variable Did not close /* with */

Run-time Errors
Illegal operation occur during execution

Example
Divide a number by zero Open an non existing file Write to non existing printer

Logic Errors
Passed Syntax and Run-time Errors but produce false result It is usually causes by the algorithm of the program

I/O Manipulators
Using library <iomanip>
setw(int n) setprecision(int n) fixed scientific left right sets width of next output value only to the argument. set decimal places. Default is 6. output as decimal notation output as scientific notation left align . Use with setw() right align. Use with setw()

setw( )
E.g., int n=1234 cout << setw(10) <<n; //default right align

output:
1 2 3 4

setprecision()
The precision determines the maximum number of digits that shall be output to express floating-point values, counting both the digits before and after the decimal point.

// setprecision example #include <iostream> #include <iomanip> using namespace std;


int main () { double f =3.141596; cout << setprecision (5) << f << endl; cout << setprecision (3) << f << endl; return 0; }

Output: 3.1416 // rounded up 3.14

Fixed or Scientific
float n=12.345678; cout << n << endl; cout << fixed; cout << n << endl; cout << scientific; cout << n << endl; Output: 12.3457//n 12.345678 // fixed

1.234568e+001//scientific

float n=123.2345; cout << fixed; cout<< setprecision(2)<<n; cout<< setprecision(3)<<n; cout<< setprecision(4)<<n;

Output
123.23 123.234 123.2345

Left/Right
float n=123.456; cout << n << endl; cout << fixed; cout << setw(20)<< n << endl; 123.456001 cout << setw(20)<< left << n << endl; 123.456001 cout << setw(20)<< right<< n<< endl; 123.456001
output <---------- 20 ----------> 123.456

You might also like