1 Objectives Understand streamed input and output.

Slides:



Advertisements
Similar presentations
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Advertisements

Exercise 2.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
CS1 Lesson 3 Expressions and Interactivity CS1 -- John Cole1.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
Arithmetic Operators MeaningOperator Addition Subtraction Multiplication Division Modulus + - * / % Type Binary Binary Binary Binary Binary.
Starting Out with C++, 3 rd Edition 1 Chapter 3. Expressions and Interactivity.
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
Character I/O. COMP104 Character I/O Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable.
CS150 Introduction to Computer Science 1
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
CS150 Introduction to Computer Science 1
1 Chapter 3 Expressions and Interactivity. 2 Topics 3.1 The cin Object 3.2 Mathematical Expressions 3.3 When You Mix Apples and Oranges: Type Conversion.
1 10/25/06CS150 Introduction to Computer Science 1 Reading from and Writing to Files.
Chapter 3: Input/Output
Basic Elements of C++ Chapter 2.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
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.
1 CS102 Introduction to Computer Programming Week 3 Chapter 3 Expressions and Interactivity.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
Chapter 3 Expressions and Interactivity Department of Computer Science Missouri State Univeristy.
Chapter 3: Expressions & Interactivity
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ Programming: Basic Elements of C++.
Input/Output Sujana Jyothi C++ Workshop Day 2. C++ I/O Basics 2 I/O - Input/Output is one of the first aspects of programming that needs to be mastered:
1 Original Source : and Problem and Problem Solving.ppt.
VARIABLES AND DATA TYPES Chapter2:part1 1. Objectives: By the end of this section you should: Understand what the variables are and why they are used.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
CS Class 03 Topics  Sequence statements Input Output Assignment  Expressions Read pages Read pages 40 – 49 for next time.
Introduction to Computing Concepts Note Set 12. Writing a Program from Scratch public class SampleProgram1 { public static void main (String [] args)
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
LESSON 2 Basic of C++.
A Sample Program #include using namespace std; int main(void) { cout
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
TK1913 C++ Programming Basic Elements of C++.
Chapter Topics The Basics of a C++ Program Data Types
CS102 Introduction to Computer Programming
What Actions Do We Have Part 1
Chapter 1.2 Introduction to C++ Programming
Chapter 3. Expressions and Interactivity
Basic Elements of C++.
Command Line Arguments
Introduction to C++ October 2, 2017.
Chapter 2 part #3 C++ Input / Output
Basic Elements of C++ Chapter 2.
Chapter 2 Elementary Programming
Starting Out with C++: From Control Structures through Objects
Introduction to C++ Programming
הרצאה 03 אבני היסוד של תוכנית ב- C
Counting Loops.
Chapter 3: Input/Output
Introduction to C++ Programming
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
What Actions Do We Have Part 1
Chapter 2 part #3 C++ Input / Output
Programming Strings.
Programming Fundamental-1
Presentation transcript:

1 Objectives Understand streamed input and output

2 Calculating the area of a rectangle. _ Length ? Width ? Area ? #include #include using namespace std; int main () { int Length, Width, Area; cout << “Calculates the area of” cout << “Calculates the area of” cout << “ a rectangle.\n”; cout << “ a rectangle.\n”; cout & cin

3 What is the width? _ Calculating the area of a rectangle. _ Length ? 10 What is the length? _ Width ? Area ? cout & cin (con’t) cout << “What is the length? ”; cout << “What is the length? ”; cin >> Length; cin >> Length; cout << “What is the width? ”; cout << “What is the width? ”; cin >> Width; cin >> Width; Area = Length * Width; Area = Length * Width;

4 rectangle is 200.The area of the What is the width? _ Calculating the area of a rectangle. _ Length ? 10 What is the length? _ _ Width ? Area ? cout & cin (con’t) cout << “The area of the ”; cout << “The area of the ”; cout << “rectangle is ” << Area << “.\n”; cout << “rectangle is ” << Area << “.\n”;}

5 Calculating the area of a rectangle. _ Length ? Width ? Area ? # include using namespace std; int main () { int Length, Width, Area; cout << “Calculating the area of” cout << “Calculating the area of” cout << “ a rectangle.\n”; cout << “ a rectangle.\n”; cin & Multiple Values

6 Separate entries with a space: _ Calculating the area of a rectangle. _ Length ? 10 Enter length and width of Width ? Area ? 20 Multiple Values (con’t) rectangle. cout << “Enter length and width of ”; cout << “Enter length and width of ”; << “rectangle.\n”; << “rectangle.\n”; cout << “Separate entries with”; cout << “Separate entries with”; << “ a space: ”; << “ a space: ”; cin >> Length >>Width; cin >> Length >>Width;

7 The area of the rectangle is _ Separate entries with a space: _ Calculating the area of a rectangle. _ Length ? 10 Enter length and width of Width ? Area ? Multiple Values (con’t) rectangle. 200 _ Area = Length * Width; Area = Length * Width; cout << “The area of the rectangle is ”; cout << “The area of the rectangle is ”; << Area << endl; << Area << endl; return 0; }

8 Multiple values & data types Whole ? Fractional ?.? Letter ? # include using namespace std; int main () { int Whole; double Fractional; double Fractional; char Letter; char Letter;

9 Multiple values & data types Enter an integer, a double, _ Whole ? 4 and a character: _ b Fractional ?.? Letter ? b 5.7 cout << “Enter an integer, a double,\n” cout << “Enter an integer, a double,\n” << “and a character: ”; << “and a character: ”; cin >> Whole >> Fractional cin >> Whole >> Fractional >> Letter; >> Letter;

10 _ Fractional: _ Whole: 4 Multiple values & data types Whole ? Fractional ?.? Letter ? _ Enter an integer, a double, 4 b 5.7 and a character: _ b Letter: b 5.7 cout << “Whole: ” << Whole<< endl; cout << “Fractional: ” << Fractional << endl; << Fractional << endl; cout << “Letter: ” << Letter; return 0;}

11 Incorrect data entry Enter an integer, a double, _ Whole ? and a character: _ b Fractional ?.? Letter ? cout << “Enter an integer, a double,\n” cout << “Enter an integer, a double,\n” << “and a character: ”; << “and a character: ”; cin >> Whole >> Fractional cin >> Whole >> Fractional >> Letter; >> Letter;.7 5 4

12 cin >> Rules Integer read –Ignores/skips leading white space characters (space, tab, new line) –Begins collecting digits, stops at the first non-digit character (leaving that character in the buffer) Character read –Ignores/skips leading white space characters (space, tab, new line) –Reads next character (any valid ASCII character)

13 cin >> Rules String read –Ignores/skips leading white space characters (space, tab, new line) –Collects characters and stops at the first white space character

14 Name cin reads a string into a character array # include using namespace std; int main () { char Name[15];

15 Name cin reads a string into a character array What is your name? _ Good morning Charlie Charlie C h a r l i e \0 _ cout << “What is your name? ”; cout << “What is your name? ”; cin >> Name; cin >> Name; cout << “Good morning ” << Name cout << “Good morning ” << Name << endl; << endl; return 0; }

16 Last Read 2 strings into 2 character arrays First # include using namespace std; int main () { char First[10], Last[10];

17 Enter your first and last name and I will Last 2 strings into 2 character arrays (con’t) First reverse them. _ cout << “Enter your first ”; << “and last name\n and I will”; << “ reverse them.\n”; << “ reverse them.\n”;

18 Enter your first and last name and I will Last 2 strings into 2 character arrays (con’t) First reverse them. _ Jones, Johnny J o h n n y \0 J o n e s \0 cin >> First >> Last; cout << Last << “, ” << First << endl; return 0;} Johnny Jones

19 The Typecast Operator #include int main () {int Months, Books; double PerMonth; cout << “How many books do” << “you plan to read?”; cin >> Books; cout << “ How many months will” << “it take you to read them?”; cin >> Months; PerMonth = static_cast (Books) / Months; cout << “That is” << PerMonth << “books per month.\n”; }

20 The Typecast Operator #include using namespace std; int main () { int Number = 65; cout << Number << endl; // C method of type casting cout << char(Number) << endl; }

21 # define Directive #include #include //needed for pow function using namespace std; #define PI int main() {double Area, Radius; cout << “This program calculates the” <<“area of a circle.\n”; cout << “What is the radius of the circle?”; cin >> Radius; Area = PI * pow(Radius, 2); cout << “The area is “ << Area; }

22 # define Directive #Include #include //needed for pow function using namespace std; #define PI void main(void) {double Area, Radius; cout << “This program calculates the” <<“area of a circle.\n”; cout << “What is the radius of the circle?”; cin >> Radius; Area = PI * pow(Radius, 2); cout << “The area is “ << Area; }

23 cout << (a + 5); Expressions with a value

24 Statement Screen Output cout << (a + b);11 cout << (b * 2);14 cout << (b + (a*2));15 cout << (a = 6);6 Expressions with a value Assume a is 4 and b is 7

25 Duo ? 5 10 Unus ? 59 Tres ? _ 11 _ 1 _ # include using namespace std; int main () { int Unus, Duo, Tres; Unus = Duo = Tres = 5; Unus = Duo = Tres = 5; Unus += 4; Unus += 4; Duo *= 2; Duo *= 2; Tres -= 4; Tres -= 4; Unus /= 3; Unus /= 3; Duo += Tres; Duo += Tres; cout << Unus << endl; cout << Unus << endl; cout << Duo << endl; cout << Duo << endl; cout << Tres << endl; cout << Tres << endl; return 0; }

26 Unusual ? Strange ? Mystery ? _ 5 _ 10 _ # include # include int main () using namespace std; { int Unusual, Mystery, Strange; cout << (Unusual = 2) << endl; cout << (Unusual = 2) << endl; cout << (Strange = Unusual + 3) << endl; cout << (Strange = Unusual + 3) << endl; cout << (Mystery = Strange * 2) cout << (Mystery = Strange * 2) << endl; << endl;

27 Unusual ? Strange ? Mystery ? _ 5 _ 10 _ 2 _ 5 _ 10 _ cout << Unusual << endl; cout << Unusual << endl; cout << Strange << endl; cout << Strange << endl; cout << Mystery << endl; cout << Mystery << endl; return 0; }