Professional Documents
Culture Documents
الفصل الثالث للطباعة PDF
الفصل الثالث للطباعة PDF
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-2
١
١٠/٠١/٤٠
٢
١٠/٠١/٤٠
• User input goes from keyboard to the input • Can be used to input multiple values
buffer, where it is stored as characters cin >> height >> width;
• cin converts the data to the type that • Multiple values from keyboard must be
separated by spaces or [Enter]
matches the variable
• Must press [Enter] after typing last value
int height;
cout << "How tall is the room? "; • Multiple values need not all be of the same type
cin >> height; • Order is important; first value entered is stored
in first variable, etc.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-5 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-6
٣
١٠/٠١/٤٠
٤
١٠/٠١/٤٠
٥
١٠/٠١/٤٠
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-11 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-12
٦
١٠/٠١/٤٠
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-13 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-14
٧
١٠/٠١/٤٠
٨
١٠/٠١/٤٠
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-17 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-18
٩
١٠/٠١/٤٠
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-19 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-20
١٠
١٠/٠١/٤٠
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-21 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-22
١١
١٠/٠١/٤٠
١٢
١٠/٠١/٤٠
١٣
١٠/٠١/٤٠
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-27 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-28
١٤
١٠/٠١/٤٠
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-29 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-30
١٥
١٠/٠١/٤٠
١٦
١٠/٠١/٤٠
• Both can be displayed with cout and << getline(cin, str); // Reads in a string
// that may contain
// blanks
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-33 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-34
١٧
١٠/٠١/٤٠
String Operators
Character Input
= Assigns a value to a string
Reading in a character: string words;
char ch; words = "Tasty ";
cin >> ch; // Reads in any non-blank char + Joins two strings together
cin.get(ch); // Reads in any char string s1 = "hot", s2 = "dog";
string food = s1 + s2; // food = "hotdog"
ch = cin.get;// Reads in any char
+= Concatenates a string onto the end of another one
cin.ignore();// Skips over next char in
words += food; // words now = "Tasty hotdog"
// the input buffer
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-35 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-36
١٨
١٠/٠١/٤٠
١٩
١٠/٠١/٤٠
C-String Input
C-String Initialization vs. Assignment
• Reading in a C-string
const int SIZE = 10; • A C-string can be initialized at the time of its
char Cstr[SIZE]; creation, just like a string object
cin >> Cstr; // Reads in a C-string with no const int SIZE = 10;
// blanks. Will write past the
// end of the array if input string char month[SIZE] = "April";
// is too long.
• However, a C-string cannot later be assigned a
cin.getline(Cstr, 10);
value using the = operator; you must use the
// Reads in a C-string that may strcpy() function
// contain blanks. Ensures that <= 9
// chars are read in. char month[SIZE];
• Can also use setw() and width() to control input field month = "August" // wrong!
widths strcpy(month, "August"); //correct
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-39 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-40
٢٠
١٠/٠١/٤٠
٢١
١٠/٠١/٤٠
These require cstdlib header file • Use time() to generate different seed values
• rand each time that a program runs:
– Returns a random number between 0 and the #include <ctime> //needed for time()
largest int the computer holds …
– Will yield the same sequence of numbers each unsigned seed = time(0);
time the program is run srand(seed);
• srand(x) • Random numbers can be scaled to a range:
– Initializes random number generator with int max=6;
unsigned int x. x is the “seed value”. int num;
– Should be called at most once in a program
num = rand() % max + 1;
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-43 Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-44
٢٢
١٠/٠١/٤٠
٢٣