Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Objectives Understand streamed input and output.

Similar presentations


Presentation on theme: "1 Objectives Understand streamed input and output."— Presentation transcript:

1

2 1 Objectives Understand streamed input and output

3 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

4 3 What is the width? _ Calculating the area of a rectangle. _ Length ? 10 What is the length? _ 10 20 Width ? Area ? 200 20 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;

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

6 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

7 6 Separate entries with a space: _ Calculating the area of a rectangle. _ Length ? 10 Enter length and width of 10 20 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;

8 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 10 20 Width ? Area ? 200 20 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; }

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

10 9 Multiple values & data types Enter an integer, a double, _ Whole ? 4 and a character: _ 4 5.7 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;

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

12 11 Incorrect data entry Enter an integer, a double, _ Whole ? and a character: _ 5.7 4 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

13 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)

14 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

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

16 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; }

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

18 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”;

19 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

20 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”; }

21 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; }

22 21 # define Directive #include #include //needed for pow function using namespace std; #define PI 3.14159 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; }

23 22 # define Directive #Include #include //needed for pow function using namespace std; #define PI 3.14159 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; }

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

25 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

26 25 Duo ? 5 10 Unus ? 59 Tres ? 5 11 3 1 3 _ 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; }

27 26 Unusual ? Strange ? Mystery ? 102 5 2 _ 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;

28 27 Unusual ? Strange ? Mystery ? 102 5 2 _ 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; }


Download ppt "1 Objectives Understand streamed input and output."

Similar presentations


Ads by Google