SlideShare a Scribd company logo
1 of 35
C++ Programming: From Problem Analysis to
        Program Design, Fifth Edition


                Review
            Chapter 1, 2, and 3
Processing a C++ Program
#include <iostream>
using namespace std;
int main()
{
    cout << "My first C++ program." << endl;

      system("pause");
      return 0;
}
============================================


Sample Run:
My first C++ program.



C++ Programming: From Problem Analysis to Program Design, Fifth Edition   2
The Problem Analysis–Coding–Execution
                       Cycle




C++ Programming: From Problem Analysis to Program Design, Fifth Edition   3
Processing a C++ Program
• To execute a C++ program:
      – Step 1: Analyze the problem
      – Step 2: Design an algorithm
      – Step 3: Create a Program (Coding)
            • Use an editor to create a source code in C++, Save the
              program on a secondary storage device (filename.cpp)
      – Step 4: Directives begin with # are processed by the
        preprocessor
      – Step 5: Use the compiler to:
            • Check that the program obeys the rules
            • If yes, translate C++ programs into machine language (object
              code)


C++ Programming: From Problem Analysis to Program Design, Fifth Edition   4
Processing a C++ Program
                  (cont'd.)
      – Step 6: Linking:
            • Combines object program with other programs
              provided by the SDK to create an executable code
      – Step 7: Loading:
            • Loads executable program into main memory
      – Step 8: Execution
            • CPU executes the program one instruction at a
              time.



C++ Programming: From Problem Analysis to Program Design, Fifth Edition   5
A C++ Program
#include <iostream>
using namespace std;
int main()
{
  int num;
  num = 6;
  cout << "My first C++ program." << endl;
  cout << "The sum of 2 and 3 = " << 5 << endl;
  cout << "7 + 8 = " << 7 + 8 << endl;
  cout << "Num = " << num << endl;
  return 0;
}

C++ Programming: From Problem Analysis to Program Design, Fifth Edition   6
Comments
      • Comments used for the reader, not for the
        compiler
      • Two types:
            – Single line
                   // This is a C++ program. It prints the sentence:
                   // Welcome to C++ Programming.

            – Multiple line
                   /*
                        You can include comments that can
                        occupy several lines.
                   */



C++ Programming: From Problem Analysis to Program Design, Fifth Edition   7
Reserved Words (Keywords)
• Reserved words, keywords, or word
  symbols
      – Example:
            •   int
            •   float
            •   double
            •   char
            •   const
            •   void
            •   return
C++ Programming: From Problem Analysis to Program Design, Fifth Edition   8
Identifiers
• Consist of letters, digits, and the underscore
  character (_)
• Must begin with a letter or underscore
• C++ is case sensitive
      – NUMBER is not the same as number




C++ Programming: From Problem Analysis to Program Design, Fifth Edition   9
Identifiers (cont'd.)
• Legal identifiers in C++:
      first
      conversion
      payRate




C++ Programming: From Problem Analysis to Program Design, Fifth Edition   10
Simple Data Types
 int : integers (numbers without a decimal), four
  bytes
      long, short, signed, unsigned
 char : Used for characters: letters, digits, and
  special symbols. Enclosed in single quotes, one
  byte
     'A', 'a', '0', '*', '+', '$', '&‘, ' ‘
 float: decimal numbers , represents any real
  number, four bytes
 double: decimal numbers , represents any real
  number, eight bytes
   bool : true or false (0), one byte.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition   11
Arithmetic Operators and Operator
            Precedence
• C++ arithmetic operators:
                 + addition
                 - subtraction
                 * multiplication
                 / division
                 % modulus operator
• +, -, *, and / can be used with integral and
  floating-point data types
• Operators can be unary or binary
C++ Programming: From Problem Analysis to Program Design, Fifth Edition   12
Order of Precedence
• All operations inside of () are evaluated
  first
• *, /, and % are at the same level of
  precedence and are evaluated next
• + and – have the same level of
  precedence and are evaluated last
•   When operators are on the same level, Performed from left to right

• 3 * 7 - 6 + 2 * 5 / 4 + 6 means
      (((3 * 7) – 6) + ((2 * 5) / 4 )) + 6


C++ Programming: From Problem Analysis to Program Design, Fifth Edition   13
Expressions
• If the two operands are integers
            • Yields an integral result
            • Example: 5/2  2
            • If the two operands are floating-point
            • Yields a floating-point result
            • Example: 5.0 / 2.0  2.5
• If mixed operands
            • Integer is changed to floating-point
            • Result is floating-point
            • Example: 5 / 2.0  2.5
C++ Programming: From Problem Analysis to Program Design, Fifth Edition   14
Type Conversion (Casting)

• Cast operator: provides explicit type
  conversion (value of one type is changed
  to another type)
      static_cast<dataTypeName>(expression)




C++ Programming: From Problem Analysis to Program Design, Fifth Edition   15
Type Conversion (cont'd.)




C++ Programming: From Problem Analysis to Program Design, Fifth Edition   16
string Type
      •    Sequence of zero or more characters
      •    Enclosed in double quotation marks
      •    Null: a string with no characters
      •    Each character has relative position in a
           string
            – Position of the first character is 0
      • Length of a string : is the number of
        characters in the string.
            – Example: length of "William Jacob" is 13
C++ Programming: From Problem Analysis to Program Design, Fifth Edition   17
Constants
• const: is a memory location whose content
  can’t change during execution
• The syntax to declare a named constant
  is:
• In C++, const is a reserved word




C++ Programming: From Problem Analysis to Program Design, Fifth Edition   18
Increment and Decrement
                      Operators
• Increment operator: increment variable by 1
      – Pre-increment: ++variable
      – Post-increment: variable++
• Decrement operator: decrement variable by 1
      – Pre-decrement: --variable
      – Post-decrement: variable--
• What is the difference between the following?
                            x = 5;                          x = 5;
                            y = ++x;                        y = x++;
C++ Programming: From Problem Analysis to Program Design, Fifth Edition   19
Compound Assignments
      +=, -=, *=, /=, and %=
• Example:
      x *= y;  x = x * y ;
     x *= y+2  x= x * (y+2) ;




C++ Programming: From Problem Analysis to Program Design, Fifth Edition   20
I/O Streams
• Use iostream header file to extract
  (receive) data from keyboard and send
  output to the screen
      – Contains definitions of two data types:
            • istream: input stream
            • ostream: output stream
      – Has two variables:
            • cin: stands for common input
            • cout: stands for common output

C++ Programming: From Problem Analysis to Program Design, Fifth Edition   21
Output

      • The syntax of cout and << is:


            – Called an output statement
      • The stream insertion operator is <<
      • Expression evaluated and its value is
        printed at the current cursor position on
        the screen

C++ Programming: From Problem Analysis to Program Design, Fifth Edition   22
Output (cont'd.)
• A manipulator is used to format the output
      – Example: endl causes insertion point to
        move to beginning of next line




C++ Programming: From Problem Analysis to Program Design, Fifth Edition   23
Output (cont'd.)

      • The new line character is 'n'
            – May appear anywhere in the string
                   cout << "Hello there.";
                   cout << "My name is James.";
                   • Output:
                      Hello there.My name is James.
                   cout << "Hello there.n";
                   cout << "My name is James.";
                   • Output :
                      Hello there.
                      My name is James.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition   24
Output (cont'd.)




C++ Programming: From Problem Analysis to Program Design, Fifth Edition   25
setw
• Outputs the value of an expression in
  specific columns
      – cout << setw(5) << x << endl;
• If number of columns exceeds the number
  of columns required by the expression
      – Output of the expression is right-justified
      – Unused columns to the left are filled with
        spaces
• Must include the header file iomanip
C++ Programming: From Problem Analysis to Program Design, Fifth Edition   26
Input (Read) Statement
• cin is used with >> to gather input


• The stream extraction operator is >>
• For example, if miles is a double variable
     cin >> miles;
            • Causes computer to get a value of type double
            • Places it in the variable miles


C++ Programming: From Problem Analysis to Program Design, Fifth Edition   27
Input (Read) Statement (cont'd.)
• Using more than one variable in cin
  allows more than one value to be read at a
  time
• For example, if feet and inches are
  variables of type int, a statement such
  as:
         cin >> feet >> inches;
      – Inputs two integers from the keyboard
      – Places them in variables feet and inches
        respectively
C++ Programming: From Problem Analysis to Program Design, Fifth Edition   28
cin and the Extraction Operator >>
             (cont'd.)




C++ Programming: From Problem Analysis to Program Design, Fifth Edition   29
cin and the Extraction Operator >>
             (cont'd.)




C++ Programming: From Problem Analysis to Program Design, Fifth Edition   30
cin and the Extraction Operator >>
             (cont'd.)




C++ Programming: From Problem Analysis to Program Design, Fifth Edition   31
cin and the Extraction Operator >>
             (cont'd.)




C++ Programming: From Problem Analysis to Program Design, Fifth Edition   32
Input/Output and the string Type
         – Entering a char value into an int or
           double variable causes serious errors,
           called input failure
         – cin can read a string into a variable of the
           data type string (without spaces)
         – The function getline() reads until
             end of the current line (with spaces)




C++ Programming: From Problem Analysis to Program Design, Fifth Edition   33
Syntax Errors & Logical Errors
• Syntax errors
      – Reported by the compiler
            int x;                    //Line        1
            int y                     //Line        2: error
            double z;                 //Line        3
            y = w + x;                //Line        4: error

• Logic errors
      –   Typically not caught by the compiler
      –   Spot and correct using cout statements
      –   Temporarily insert an output statement
      –   Correct problem
      –   Remove output statement
C++ Programming: From Problem Analysis to Program Design, Fifth Edition   34
HW1: Submission next meeting

More Related Content

What's hot

Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in JavaNiloy Saha
 
Array in c programming
Array in c programmingArray in c programming
Array in c programmingMazharul Islam
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsVineeta Garg
 
Searching linear &amp; binary search
Searching linear &amp; binary searchSearching linear &amp; binary search
Searching linear &amp; binary searchnikunjandy
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure Janki Shah
 
String function in my sql
String function in my sqlString function in my sql
String function in my sqlknowledgemart
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsJulie Iskander
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentationVedaGayathri1
 
Recursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresRecursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresPriyanka Rana
 

What's hot (20)

Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
DBMS: Types of keys
DBMS:  Types of keysDBMS:  Types of keys
DBMS: Types of keys
 
Joins in SQL
Joins in SQLJoins in SQL
Joins in SQL
 
Data Structures
Data StructuresData Structures
Data Structures
 
Unit 4 plsql
Unit 4  plsqlUnit 4  plsql
Unit 4 plsql
 
Lecture09 recursion
Lecture09 recursionLecture09 recursion
Lecture09 recursion
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Class method
Class methodClass method
Class method
 
Searching linear &amp; binary search
Searching linear &amp; binary searchSearching linear &amp; binary search
Searching linear &amp; binary search
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Control statements
Control statementsControl statements
Control statements
 
SQL select clause
SQL select clauseSQL select clause
SQL select clause
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
String function in my sql
String function in my sqlString function in my sql
String function in my sql
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentation
 
Recursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresRecursion - Algorithms and Data Structures
Recursion - Algorithms and Data Structures
 

Similar to Review chapter 1 2-3

Computer Programming
Computer ProgrammingComputer Programming
Computer ProgrammingBurhan Fakhar
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02Terry Yoast
 
PPT slide_chapter 02 Basic element of C++.ppt
PPT slide_chapter 02 Basic element of C++.pptPPT slide_chapter 02 Basic element of C++.ppt
PPT slide_chapter 02 Basic element of C++.pptmasadjie
 
ch01_an overview of computers and programming languages
ch01_an overview of computers and programming languagesch01_an overview of computers and programming languages
ch01_an overview of computers and programming languagesLiemLe21
 
9781285852744 ppt ch01
9781285852744 ppt ch019781285852744 ppt ch01
9781285852744 ppt ch01Terry Yoast
 
9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12Terry Yoast
 
UoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdfUoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdfmadihamaqbool6
 
Programming For Engineers Functions - Part #1.pptx
Programming For Engineers Functions - Part #1.pptxProgramming For Engineers Functions - Part #1.pptx
Programming For Engineers Functions - Part #1.pptxNoorAntakia
 
Chapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaChapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaAdan Hubahib
 
9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.ppt9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.pptLokeshK66
 
9781285852751 ppt c++
9781285852751 ppt c++9781285852751 ppt c++
9781285852751 ppt c++umair khan
 
C++ Programming Chapter 01
C++ Programming Chapter 01C++ Programming Chapter 01
C++ Programming Chapter 01Sourng Seng
 
Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Mohamed El Desouki
 
OOPS using C++
OOPS using C++OOPS using C++
OOPS using C++cpjcollege
 
Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14IIUM
 
Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14IIUM
 

Similar to Review chapter 1 2-3 (20)

Computer Programming
Computer ProgrammingComputer Programming
Computer Programming
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02
 
PPT slide_chapter 02 Basic element of C++.ppt
PPT slide_chapter 02 Basic element of C++.pptPPT slide_chapter 02 Basic element of C++.ppt
PPT slide_chapter 02 Basic element of C++.ppt
 
C++.ppt
C++.pptC++.ppt
C++.ppt
 
ch01_an overview of computers and programming languages
ch01_an overview of computers and programming languagesch01_an overview of computers and programming languages
ch01_an overview of computers and programming languages
 
9781285852744 ppt ch01
9781285852744 ppt ch019781285852744 ppt ch01
9781285852744 ppt ch01
 
Chapter 4 5
Chapter 4 5Chapter 4 5
Chapter 4 5
 
9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12
 
C++ ch1
C++ ch1C++ ch1
C++ ch1
 
UoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdfUoN-Lec_12_Control_Structure.pdf
UoN-Lec_12_Control_Structure.pdf
 
Programming For Engineers Functions - Part #1.pptx
Programming For Engineers Functions - Part #1.pptxProgramming For Engineers Functions - Part #1.pptx
Programming For Engineers Functions - Part #1.pptx
 
Lesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving processLesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving process
 
Chapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaChapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of Java
 
9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.ppt9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.ppt
 
9781285852751 ppt c++
9781285852751 ppt c++9781285852751 ppt c++
9781285852751 ppt c++
 
C++ Programming Chapter 01
C++ Programming Chapter 01C++ Programming Chapter 01
C++ Programming Chapter 01
 
Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++
 
OOPS using C++
OOPS using C++OOPS using C++
OOPS using C++
 
Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14
 
Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14Csc1100 lecture03 ch03-pt2-s14
Csc1100 lecture03 ch03-pt2-s14
 

Review chapter 1 2-3

  • 1. C++ Programming: From Problem Analysis to Program Design, Fifth Edition Review Chapter 1, 2, and 3
  • 2. Processing a C++ Program #include <iostream> using namespace std; int main() { cout << "My first C++ program." << endl; system("pause"); return 0; } ============================================ Sample Run: My first C++ program. C++ Programming: From Problem Analysis to Program Design, Fifth Edition 2
  • 3. The Problem Analysis–Coding–Execution Cycle C++ Programming: From Problem Analysis to Program Design, Fifth Edition 3
  • 4. Processing a C++ Program • To execute a C++ program: – Step 1: Analyze the problem – Step 2: Design an algorithm – Step 3: Create a Program (Coding) • Use an editor to create a source code in C++, Save the program on a secondary storage device (filename.cpp) – Step 4: Directives begin with # are processed by the preprocessor – Step 5: Use the compiler to: • Check that the program obeys the rules • If yes, translate C++ programs into machine language (object code) C++ Programming: From Problem Analysis to Program Design, Fifth Edition 4
  • 5. Processing a C++ Program (cont'd.) – Step 6: Linking: • Combines object program with other programs provided by the SDK to create an executable code – Step 7: Loading: • Loads executable program into main memory – Step 8: Execution • CPU executes the program one instruction at a time. C++ Programming: From Problem Analysis to Program Design, Fifth Edition 5
  • 6. A C++ Program #include <iostream> using namespace std; int main() { int num; num = 6; cout << "My first C++ program." << endl; cout << "The sum of 2 and 3 = " << 5 << endl; cout << "7 + 8 = " << 7 + 8 << endl; cout << "Num = " << num << endl; return 0; } C++ Programming: From Problem Analysis to Program Design, Fifth Edition 6
  • 7. Comments • Comments used for the reader, not for the compiler • Two types: – Single line // This is a C++ program. It prints the sentence: // Welcome to C++ Programming. – Multiple line /* You can include comments that can occupy several lines. */ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 7
  • 8. Reserved Words (Keywords) • Reserved words, keywords, or word symbols – Example: • int • float • double • char • const • void • return C++ Programming: From Problem Analysis to Program Design, Fifth Edition 8
  • 9. Identifiers • Consist of letters, digits, and the underscore character (_) • Must begin with a letter or underscore • C++ is case sensitive – NUMBER is not the same as number C++ Programming: From Problem Analysis to Program Design, Fifth Edition 9
  • 10. Identifiers (cont'd.) • Legal identifiers in C++: first conversion payRate C++ Programming: From Problem Analysis to Program Design, Fifth Edition 10
  • 11. Simple Data Types  int : integers (numbers without a decimal), four bytes  long, short, signed, unsigned  char : Used for characters: letters, digits, and special symbols. Enclosed in single quotes, one byte 'A', 'a', '0', '*', '+', '$', '&‘, ' ‘  float: decimal numbers , represents any real number, four bytes  double: decimal numbers , represents any real number, eight bytes bool : true or false (0), one byte. C++ Programming: From Problem Analysis to Program Design, Fifth Edition 11
  • 12. Arithmetic Operators and Operator Precedence • C++ arithmetic operators:  + addition  - subtraction  * multiplication  / division  % modulus operator • +, -, *, and / can be used with integral and floating-point data types • Operators can be unary or binary C++ Programming: From Problem Analysis to Program Design, Fifth Edition 12
  • 13. Order of Precedence • All operations inside of () are evaluated first • *, /, and % are at the same level of precedence and are evaluated next • + and – have the same level of precedence and are evaluated last • When operators are on the same level, Performed from left to right • 3 * 7 - 6 + 2 * 5 / 4 + 6 means (((3 * 7) – 6) + ((2 * 5) / 4 )) + 6 C++ Programming: From Problem Analysis to Program Design, Fifth Edition 13
  • 14. Expressions • If the two operands are integers • Yields an integral result • Example: 5/2  2 • If the two operands are floating-point • Yields a floating-point result • Example: 5.0 / 2.0  2.5 • If mixed operands • Integer is changed to floating-point • Result is floating-point • Example: 5 / 2.0  2.5 C++ Programming: From Problem Analysis to Program Design, Fifth Edition 14
  • 15. Type Conversion (Casting) • Cast operator: provides explicit type conversion (value of one type is changed to another type) static_cast<dataTypeName>(expression) C++ Programming: From Problem Analysis to Program Design, Fifth Edition 15
  • 16. Type Conversion (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition 16
  • 17. string Type • Sequence of zero or more characters • Enclosed in double quotation marks • Null: a string with no characters • Each character has relative position in a string – Position of the first character is 0 • Length of a string : is the number of characters in the string. – Example: length of "William Jacob" is 13 C++ Programming: From Problem Analysis to Program Design, Fifth Edition 17
  • 18. Constants • const: is a memory location whose content can’t change during execution • The syntax to declare a named constant is: • In C++, const is a reserved word C++ Programming: From Problem Analysis to Program Design, Fifth Edition 18
  • 19. Increment and Decrement Operators • Increment operator: increment variable by 1 – Pre-increment: ++variable – Post-increment: variable++ • Decrement operator: decrement variable by 1 – Pre-decrement: --variable – Post-decrement: variable-- • What is the difference between the following? x = 5; x = 5; y = ++x; y = x++; C++ Programming: From Problem Analysis to Program Design, Fifth Edition 19
  • 20. Compound Assignments +=, -=, *=, /=, and %= • Example: x *= y;  x = x * y ; x *= y+2  x= x * (y+2) ; C++ Programming: From Problem Analysis to Program Design, Fifth Edition 20
  • 21. I/O Streams • Use iostream header file to extract (receive) data from keyboard and send output to the screen – Contains definitions of two data types: • istream: input stream • ostream: output stream – Has two variables: • cin: stands for common input • cout: stands for common output C++ Programming: From Problem Analysis to Program Design, Fifth Edition 21
  • 22. Output • The syntax of cout and << is: – Called an output statement • The stream insertion operator is << • Expression evaluated and its value is printed at the current cursor position on the screen C++ Programming: From Problem Analysis to Program Design, Fifth Edition 22
  • 23. Output (cont'd.) • A manipulator is used to format the output – Example: endl causes insertion point to move to beginning of next line C++ Programming: From Problem Analysis to Program Design, Fifth Edition 23
  • 24. Output (cont'd.) • The new line character is 'n' – May appear anywhere in the string cout << "Hello there."; cout << "My name is James."; • Output: Hello there.My name is James. cout << "Hello there.n"; cout << "My name is James."; • Output : Hello there. My name is James. C++ Programming: From Problem Analysis to Program Design, Fifth Edition 24
  • 25. Output (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition 25
  • 26. setw • Outputs the value of an expression in specific columns – cout << setw(5) << x << endl; • If number of columns exceeds the number of columns required by the expression – Output of the expression is right-justified – Unused columns to the left are filled with spaces • Must include the header file iomanip C++ Programming: From Problem Analysis to Program Design, Fifth Edition 26
  • 27. Input (Read) Statement • cin is used with >> to gather input • The stream extraction operator is >> • For example, if miles is a double variable cin >> miles; • Causes computer to get a value of type double • Places it in the variable miles C++ Programming: From Problem Analysis to Program Design, Fifth Edition 27
  • 28. Input (Read) Statement (cont'd.) • Using more than one variable in cin allows more than one value to be read at a time • For example, if feet and inches are variables of type int, a statement such as: cin >> feet >> inches; – Inputs two integers from the keyboard – Places them in variables feet and inches respectively C++ Programming: From Problem Analysis to Program Design, Fifth Edition 28
  • 29. cin and the Extraction Operator >> (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition 29
  • 30. cin and the Extraction Operator >> (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition 30
  • 31. cin and the Extraction Operator >> (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition 31
  • 32. cin and the Extraction Operator >> (cont'd.) C++ Programming: From Problem Analysis to Program Design, Fifth Edition 32
  • 33. Input/Output and the string Type – Entering a char value into an int or double variable causes serious errors, called input failure – cin can read a string into a variable of the data type string (without spaces) – The function getline() reads until end of the current line (with spaces) C++ Programming: From Problem Analysis to Program Design, Fifth Edition 33
  • 34. Syntax Errors & Logical Errors • Syntax errors – Reported by the compiler int x; //Line 1 int y //Line 2: error double z; //Line 3 y = w + x; //Line 4: error • Logic errors – Typically not caught by the compiler – Spot and correct using cout statements – Temporarily insert an output statement – Correct problem – Remove output statement C++ Programming: From Problem Analysis to Program Design, Fifth Edition 34