1 / 27

Objectives

Objectives. Understand streamed input and output. ?. ?. ?. Length. Width. Area. cout &amp; cin. #include &lt;iostream&gt; using namespace std; int main () { int Length, Width, Area; cout &lt;&lt; “Calculates the area of” cout &lt;&lt; “ a rectangle.<br>”;. Calculating the area of a rectangle. _. ?.

marcel
Télécharger la présentation

Objectives

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Objectives • Understand streamed input and output

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

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

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

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

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

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

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

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

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

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

  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. cin reads a string into a character array # include <iostream>using namespace std; int main () { char Name[15]; Name

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

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

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

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

  19. The Typecast Operator #include <iostream.> 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 <double> (Books) / Months; cout << “That is” << PerMonth << “books per month.\n”; }

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

  21. # define Directive #include <iostream> #include <cmath> //needed for pow functionusing 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; }

  22. # define Directive #Include <iostream> #include <cmath> //needed for pow functionusing 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; }

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

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

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

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

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

More Related