270 likes | 409 Views
Objectives. Understand streamed input and output. ?. ?. ?. Length. Width. Area. cout & cin. #include <iostream> using namespace std; int main () { int Length, Width, Area; cout << “Calculates the area of” cout << “ a rectangle.<br>”;. Calculating the area of a rectangle. _. ?.
E N D
Objectives • Understand streamed input and output
? ? ? 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. _
? ? ? 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
? ? ? 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 _
? ? ? 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. _
? ? ? 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
? ? ? 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 _
? ? Whole Letter ?.? Fractional Multiple values & data types # include <iostream>using namespace std; int main () { int Whole; double Fractional; char Letter;
? ? 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
? ? 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 _
? ? 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
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)
cin >> Rules • String read • Ignores/skips leading white space characters (space, tab, new line) • Collects characters and stops at the first white space character
cin reads a string into a character array # include <iostream>using namespace std; int main () { char Name[15]; Name
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
Last First Read 2 strings into 2 character arrays # include <iostream>using namespace std; int main () { char First[10], Last[10];
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. _
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
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”; }
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; }
# 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; }
# 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; }
Expressions with a value cout << (a + 5);
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
? ? ? 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 _
? ? ? 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
cout << Unusual << endl; cout << Strange << endl; cout << Mystery << endl; return 0; } ? ? ? Mystery Strange Unusual 2 _ 5 _ 10 _ 2 _ 5 _ 2 10 5 10 _