Chapter 1 c++ structure C++ Input / Output

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

 C++ programming facilitates a disciplined approach to program design. ◦ If you learn the correct way, you will be spared a lot of work and frustration.
Introduction to C Programming
Introduction Kingdom of Saudi Arabia Shaqra University
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Introduction to C Programming
Introduction to C++ Programming
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Introduction to C++ Programming
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - HelloWorld Application: Introduction to.
History of C and C++ C++ evolved from C ANSI C C++ “spruces up” C
CHAPTER 2 PART #3 INPUT - OUTPUT 1 st semester King Saud University College of Applied studies and Community Service Csc
Week 1 Algorithmization and Programming Languages.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
C++ How to Program, Late Objects Version, 7/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 2 part #1 C++ Program Structure
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester 1436 King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah Alakeel.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
CHAPTER 1: INTRODUCTION C++ Programming. CS 241 Course URL: Text Book: C++ How to Program, DETITEL & DEITEL, eighth Edition.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Introduction to C++.  Computers: CPU, Memory & Input / Output (IO)  Program: Sequence of instructions for the computer.  Operating system: Program.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Chapter 2 of C++ How to Program, 10/e © by Pearson Education, Inc. All Rights Reserved.
Bill Tucker Austin Community College COSC 1315
C++ First Steps.
© by Pearson Education, Inc. All Rights Reserved.
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Topic Pre-processor cout To output a message.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE
Chapter 1: Introduction to computers and C++ Programming
Introduction to C++ Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact.
CSC201: Computer Programming
Completing the Problem-Solving Process
CS-103 COMPUTER PROGRAMMING
Chapter 2 part #1 C++ Program Structure
Basic Elements of C++.
Chapter 2 Assignment and Interactive Input
Beginning C++ Programming
Chapter 2 part #3 C++ Input / Output
Basic Elements of C++ Chapter 2.
Chapter 2 – Getting Started
2.1 Parts of a C++ Program.
Introduction to C++ Programming
1.13 The Key Software Trend: Object Technology
Chapter 3: Input/Output
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Introduction to C++ Programming
Chapter 2: Introduction to C++.
Programs written in C and C++ can run on many different computers
Capitolo 1 – Introduction C++ Programming
Chapter 2 part #3 C++ Input / Output
Introduction to Programming - 1
Introduction to C Programming
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

Chapter 1 c++ structure C++ Input / Output King Saud University College of Applied studies and Community Service By: Fatimah Alakeel 2013 Edit: Ghadah Al hadba 2019 Edit: Safaa Albassam Chapter 1 c++ structure C++ Input / Output

C++ Program Structure #include <iostream> // Preprocessor Commands using namespace std; int main( ) // main function { // Declaration section – Declare needed variables …... // Input section – Enter required data ….. // Processing section – Processing Statements // Output section – Display expected results ….... return 0; }// end main The part of a program that tells the compiler the names of memory cells in a program

D – Exit is included at the end of every main function. return 0; is included at the end of every main function. The keyword return is one of several means we’ll use to exit a function. When the return statement is used at the end of main as shown here, the value 0 indicates that the program has terminated successfully.

First Program in C++: Printing a Line of Text HELLO WORLD ! First Program in C++: Printing a Line of Text // hello world programe #include <iostream> int main( ) { // main function //No Declaration, Input ,Processing sections // Output section std::cout << “Hello World \n"; // Display a message return 0; }// end main Hello World The output

Output Statement std::cout << “Hello World \n”; The entire line is called a statement. Each statement in C++ needs to be terminated with semicolon (;) This instructs the computer to perform an action, namely to print on the screen the string of characters marked by the quotation marks. Preprocessor directives (like #include) do not end with a semicolon.

Output Statement The std:: before cout is required when we use names that we’ve brought into the program by the preprocessor directive #include<iostream>. The notation std::cout specifies that we are using a name, in this case cout, that belongs to “namespace” std. We can avoid writing the std:: by writing using namespace std; as shown in following.

First Program in C++: Printing a Line of Text HELLO WORLD ! First Program in C++: Printing a Line of Text // hello world programe #include <iostream> using namespace std; int main( ) { // main function //No Declaration, Input ,Processing sections // Output section cout << “Hello World \n"; return 0; }// end main Hello World The output

Printing a Single Line of Text with Multiple Statements // Preprocessor Commands #include <iostream> using namespace std; int main( ) // main function { // Output section cout << “Hello“ cout << “World\n"; return 0; }// end main Hello World The output

Printing Multiple Lines of Text with a Single Statement // Preprocessor Commands #include <iostream> using namespace std; int main( ) // main function { // Output section cout << “Hello \n World \n"; return 0; }// end main Hello World The output

Steps to Write, Compile, and Run a Program Editor Source code or file (.cpp) Compile file Object code or file (.obj) Linker Executable code or file (.exe) Loader Executable code is loaded into memory ready for execution

Using iostream The C++ iostream library provides hundreds of I/O capabilities. Standard iostream objects: cout - object providing a connection to the monitor cin - object providing a connection to the keyboard To perform input and output we send messages to one of these objects

Output Stream

The Insertion Operator (<<) To send output to the screen we use the insertion operator << on the object cout Format: cout << something; This something can be: Literal e.g cout<<5; Expression e.g cout<<(5+4); Variable or constant e.g cout<<num;

The Insertion Operator (<<): Literal << is overloaded to output built-in types ( ex. int, float,…etc) as well as user-defined types (i.e. user defined classes). The compiler figures out the type of the object and prints it out appropriately e.g(1): cout << 5; // Outputs 5 e.g(2): cout << 4.1; // Outputs 4.1 e.g(3): cout << “Hello”; // Outputs Hello e.g(4): cout << ‘\n’; // move the cursor to a newline String literals should be surrounded with double quotation marks “ “ character literals should be surrounded with single quotation marks ‘ ‘ Important: escape sequence characters ( such as : \n, \t)can be either embedded into a string or printed alone as e.g (4)

The Insertion Operator (<<): Expression Printing an expression is similar to printing a values since the compiler will calculate then print the resulted value. Thus, the expression operands should be previously declared and had a value e.g.(1): cout << (5 + 1); // Outputs 6 e.g.(2): int x = 5 , y; y = 3; cout << ((x + y)/ 2); // Outputs 4 Important: With expressions make sure to use parenthesis () , to get the expected result 

The Insertion Operator (<<): Variables and constants To print the content of a variable of a constant identifier we simply place the identifier name after the insertion operator cout knows the type of data to output, will be printed probably e.g.(1): int x = 5; const double RATE = 3.14; cout << x; // Outputs 5 cout << RATE; // Outputs 3.14 Important: Must not confuse printing text with printing variables , i.e. don’t surround the variable name with a “ “: int m =12; cout << m; // Outputs 12 cout << “m”; // Outputs m

Cascading Stream-Insertion Operators Allows creating a single output statement that prints 1 or more type of data e.g. (1) int age=25; cout<<“Sarah is “<<age<<“Years Old”; Output for both e.g. int age=25; cout<<“Sarah is “; cout<<age; cout<<“Years Old”; Compilers start printing from left to right Compilers start printing from top to down Sarah is 25 Years Old

Cascading Stream-Insertion Operators Allows creating a single output statement that prints 1 or more type of data e.g. (2) cout << "How" << " are" << " you?"; Output How are you?

Output Statement When a backslash is encountered in a string of characters, the next character is combined with the backslash to form an escape sequence. The escape sequence \n means new line. It causes the cursor (i.e., the current screen position indicator) to move to the beginning of the next line on the screen.

Input Stream

The Extraction Operator (>>) To get input from the keyboard we use the extraction operator >>and the object cin Format: cin >> Variable; The compiler figures out the type of the variable and reads in the appropriate type e.g. int X; float Y; cin >> X; // Reads in an integer cin >> Y; // Reads in a float

Syntax cin >> someVariable; cin knows what type of data is to be assigned to someVariable (based on the type of someVariable).

>> (stream-extraction) Stream Input >> (stream-extraction) Used to perform stream input Normally ignores whitespaces (spaces, tabs, newlines) in the input stream. Returns zero (false) when EOF is encountered, otherwise returns reference to the object from which it was invoked (i.e. cin) This enables cascaded input cin >> x >> y;

Chaining Calls Multiple uses of the insertion and extraction operator can be chained together: cout << E1 << E2 << E3 << … ; cin >> V1 >> V2 >> V3 >> …; Equivalent to performing the set of insertion or extraction operators one at a time Example cout << “Total sales are $” << sales << ‘\n’; cin >> Sales1 >> Sales2 >> Sales3;

Extraction/Insertion Example cout << “Hello world! ”; int i=5; cout << “The value of i is “ << i << endl; //endl puts a new line OUTPUT: Hello World! The value of i is 5 Char letter; cout << “Please enter the first letter of your name: “; cin >> letter; cout<< “Your name starts with “ << letter; Please enter the first letter of your name: F Your name starts with F

Common Programming Errors

Common Programming Errors Debugging  Process removing errors from a program Three (3) kinds of errors : Syntax Error a violation of the C++ grammar rules, detected during program translation (compilation). statement cannot be translated and program cannot be executed

Common Programming Errors cont… Run-time errors An attempt to perform an invalid operation, detected during program execution. Occurs when the program directs the computer to perform an illegal operation, such as dividing a number by zero. The computer will stop executing the program, and displays a diagnostic message indicates the line where the error was detected

Common Programming Errors cont… Logic Error/Design Error An error caused by following an incorrect algorithm Very difficult to detect - it does not cause run-time error and does not display message errors. The only sign of logic error – incorrect program output Can be detected by testing the program thoroughly, comparing its output to calculated results To prevent – carefully desk checking the algorithm and written program before you actually type it