<ul><li>AU/MITM/1.6 </li></ul><ul><li>By Mohammed A. Saleh </li></ul>
<ul><li>An array can hold several values, all of one type. For example: </li></ul><ul><li>60  type  int values that repres...
<ul><li>To create an array, you use a declaration statement. An array declaration should indicate three things: </li></ul>...
<ul><li>The general form for declaring an array: </li></ul><ul><li>typeName arrayName[arraySize]; </li></ul><ul><li>*Note ...
*Note:  the index of the last element is one less than the size of the array. Array declaration: int c [12]  c[6] -45 6 0 ...
<ul><li>An example showing how to declare, initialize and assign values to array elements: </li></ul><ul><li>// arrayone.c...
<ul><li>cout << “Total yams = “;  </li></ul><ul><li>cout << yams[0] + yams[1] + yams[2] << endl; </li></ul><ul><li>cout <<...
<ul><li>Output from the program: </li></ul><ul><li>Total yams = 21  </li></ul><ul><li>The package with 8 yams costs 30 cen...
<ul><li>Initialization rules </li></ul><ul><li>Use initialization form only when defining an array </li></ul><ul><li>int c...
<ul><li>If too many initializers, a syntax error is generated </li></ul><ul><li>int i [2] = {10, 20, 30} </li></ul><ul><li...
1.  Initialize array using a declaration 2.  Define loop 3.  Print out each array element Program Output Element  Value 0 ...
<ul><li>A  string  is a series of characters stored in consecutive bytes of memory. </li></ul><ul><li>Two ways of dealing ...
<ul><li>All strings end with a  null  character (‘�’) </li></ul><ul><li>Examples: </li></ul><ul><li>char dog [5] = { ‘b’, ...
<ul><li>A better way to initialize a character array to a string. Just use a quoted string, called a  string constant  or ...
<ul><li>char boss[8] = “Bozo”; </li></ul><ul><li>Note that a  string constant  (with double quotes) is not interchangeable...
<ul><li>Thus, the statement: </li></ul><ul><li>char shirt_size = ‘S’;  // this is fine </li></ul><ul><li>But “S” represent...
<ul><li>Concatenating String Constants </li></ul><ul><li>Long strings may be concatenated – by combining two quoted string...
<ul><li>Concatenating String Constants </li></ul><ul><li>Long strings may be concatenated – by combining two quoted string...
<ul><li>Using Strings in an Array </li></ul><ul><li>The two most common ways of getting a string into an array are to init...
<ul><li>// strings.cpp -- storing strings in an array  </li></ul><ul><li>#include <iostream>  </li></ul><ul><li>#include <...
<ul><li>Sample output of the program: </li></ul><ul><li>Howdy! I’m C++owboy!  </li></ul><ul><li>What’s your name? Basicman...
<ul><li>Program Notes: </li></ul><ul><ul><li>sizeof  operator gives the size of the entire array </li></ul></ul><ul><ul><l...
<ul><ul><li>The program uses a symbolic constant for the array size </li></ul></ul>
<ul><li>Adventures in String Input </li></ul><ul><li>Tricky string input: </li></ul><ul><li>// instr1.cpp -- reading more ...
<ul><li>Note that the cin offers no protection against placing a 30-character string in a 20-character array </li></ul>
<ul><li>Reading String Input a Line at a Time </li></ul><ul><li>Reading string input a word at a time is often not the mos...
<ul><li>However, getline() then discards the newline character, whereas get() leaves it in the input queue. </li></ul><ul>...
<ul><li>The function takes two arguments: </li></ul><ul><ul><li>name of the array destined to hold the line of input </li>...
<ul><li>Suppose you want to read a name into a 20-element name array: </li></ul><ul><li>cin.getline(name,20); </li></ul><u...
<ul><li>The getline() conveniently gets the whole line a time. </li></ul>
<ul><li>Line-Oriented Input with get() </li></ul><ul><li>The istream class has another member function, get(), which comes...
<ul><li>Because the first call leaves the newline character in the input queue, that newline character is the first charac...
<ul><li>// getvariation1.cpp -- reading more than one word with get() </li></ul><ul><li>#include <iostream> </li></ul><ul>...
<ul><li>This is how the sequence works: </li></ul><ul><li>cin.get(name, ArSize);  // read first line </li></ul><ul><li>cin...
<ul><li>// getvar2.cpp -- reading more than one word with get() </li></ul><ul><li>#include <iostream> </li></ul><ul><li>in...
<ul><li>One thing to note is how C++ allows multiple versions of functions, provided that they have different argument lis...
<ul><li>Why use get() instead of getline() at all? </li></ul><ul><li>Older implementations may not have getline() </li></u...
<ul><li>In short, getline() is a little simpler to use, but get() makes error checking simpler. </li></ul><ul><li>Empty Li...
<ul><li>In short, getline() is a little simpler to use, but get() makes error checking simpler. </li></ul><ul><li>Empty Li...
<ul><li>Another potential problem is that the input string could be longer than the allocated space. </li></ul><ul><li>If ...
<ul><li>Mixing String and Numeric Input </li></ul><ul><li>Mixing numeric input with line-oriented string input can cause p...
<ul><li>Sample run of the program: </li></ul><ul><li>What year was your house built?  </li></ul><ul><li>1966  </li></ul><u...
<ul><li>You never get the opportunity to enter the address. The problem is that when cin reads the year, it leaves the new...
<ul><li>Use get() with no argument or with a char argument: </li></ul><ul><li>cin >> year; </li></ul><ul><li>cin.get();  /...
Upcoming SlideShare
Loading in...5
×

Lecture 6

821

Published on

Arrays and Strings in C++

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
821
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
55
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • 09/22/09 Programming Languages C Plus Plus
  • Lecture 6

    1. 1. <ul><li>AU/MITM/1.6 </li></ul><ul><li>By Mohammed A. Saleh </li></ul>
    2. 2. <ul><li>An array can hold several values, all of one type. For example: </li></ul><ul><li>60 type int values that represent five years of game sales data </li></ul><ul><li>12 short values that rep- resent the number of days in each month, or </li></ul><ul><li>365 float values that indicate your food expenses for each day of the year </li></ul><ul><li>Each value is stored in a separate array element, and the computer stores all the elements of an array consecutively in memory </li></ul>
    3. 3. <ul><li>To create an array, you use a declaration statement. An array declaration should indicate three things: </li></ul><ul><li>The type of value to be stored in each element </li></ul><ul><li>The name of the array </li></ul><ul><li>The number of elements in the array </li></ul><ul><li>Declaration: </li></ul><ul><li>short months[12]; // creates array of 12 short </li></ul>
    4. 4. <ul><li>The general form for declaring an array: </li></ul><ul><li>typeName arrayName[arraySize]; </li></ul><ul><li>*Note : The expression arraySize , which is the number of elements, must be an integer constant. </li></ul><ul><li>An array comes from the fact that you can access array elements individually. The way to do this is to use a subscript , or an index , to number the elements. C++ array numbering starts with zero. </li></ul>
    5. 5. *Note: the index of the last element is one less than the size of the array. Array declaration: int c [12] c[6] -45 6 0 72 1543 -89 0 62 -3 1 6453 78 Name of array (Note that all elements of this array have the same name, c ) c[0] c[1] c[2] c[3] c[11] c[10] c[9] c[8] c[7] c[5] c[4] Position number of the element within array c
    6. 6. <ul><li>An example showing how to declare, initialize and assign values to array elements: </li></ul><ul><li>// arrayone.cpp -- small arrays of integers </li></ul><ul><li>#include <iostream> </li></ul><ul><li>int main() { </li></ul><ul><li>int yams[3]; // creates array with three elements </li></ul><ul><li>yams[0] = 7; // assign value to first element </li></ul><ul><li>yams[1] = 8; </li></ul><ul><li>yams[2] = 6; </li></ul><ul><li>int yamcosts[3] = {20, 30, 5}; // create, initialize array </li></ul><ul><li>// NOTE: If your C++ compiler or translator can’t initialize </li></ul><ul><li>// this array, use static int yamcosts[3] instead of </li></ul><ul><li>// int yamcosts[3] </li></ul>
    7. 7. <ul><li>cout << “Total yams = “; </li></ul><ul><li>cout << yams[0] + yams[1] + yams[2] << endl; </li></ul><ul><li>cout << “The package with “ << yams[1] << “ yams costs “; </li></ul><ul><li>cout << yamcosts[1] << “ cents per yam. ”; </li></ul><ul><li>int total = yams[0] * yamcosts[0] + yams[1] * yamcosts[1]; </li></ul><ul><li>total = total + yams[2] * yamcosts[2]; </li></ul><ul><li>cout << “The total yam expense is “ << total << “ cents. ”; </li></ul><ul><li>cout << “ Size of yams array = “ << sizeof yams; </li></ul><ul><li>cout << “ bytes. ”; </li></ul><ul><li>cout << “Size of one element = “ << sizeof yams[0]; </li></ul><ul><li>cout << “ bytes. ”; </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>
    8. 8. <ul><li>Output from the program: </li></ul><ul><li>Total yams = 21 </li></ul><ul><li>The package with 8 yams costs 30 cents per yam. </li></ul><ul><li>The total yam expense is 410 cents. </li></ul><ul><li>Size of yams array = 12 bytes. </li></ul><ul><li>Size of one element = 4 bytes. </li></ul><ul><li>Program Notes: </li></ul><ul><li>Array creation and initialization </li></ul><ul><li>Using array value in calculation </li></ul><ul><li>The sizeof operator </li></ul>
    9. 9. <ul><li>Initialization rules </li></ul><ul><li>Use initialization form only when defining an array </li></ul><ul><li>int cards[4] = {3, 6, 8, 10}; </li></ul><ul><li>int hand[4]; </li></ul><ul><li>hand[4] = {5, 6, 7, 9} </li></ul><ul><li>hand = cards; </li></ul><ul><li>You can provide fewer values than array elements </li></ul><ul><li>float hotelTips[5] = {5.0, 2.5}; </li></ul>
    10. 10. <ul><li>If too many initializers, a syntax error is generated </li></ul><ul><li>int i [2] = {10, 20, 30} </li></ul><ul><li>Sets all elements to 0 </li></ul><ul><li>int n [5] = {0} </li></ul><ul><li>If size is omitted the initializers determine it </li></ul><ul><li>int n [] = {1, 2, 3, 4, 5} </li></ul><ul><li>5 initializers, therefore n is an element array. </li></ul>
    11. 11. 1. Initialize array using a declaration 2. Define loop 3. Print out each array element Program Output Element Value 0 32 1 27 2 64 3 18 4 95 5 14 6 90 7 70 8 60 9 37 1 // initialize.cpp 2 // Initializing an array with a declaration 3 #include <iostream> 4 nnnn 5 6 7 8 #include <iomanip> 9 10 11 12 int main() 13 { 14 int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 }; 15 16 cout << &quot;Element&quot; << setw( 13 ) << &quot;Value&quot; << endl; 17 18 for ( int i = 0; i < 10; i++ ) 19 cout << setw( 7 ) << i << setw( 13 ) << n[ i ] << endl; 20 21 return 0; 22 } Notice how they array is declared and elements referenced.
    12. 12. <ul><li>A string is a series of characters stored in consecutive bytes of memory. </li></ul><ul><li>Two ways of dealing with strings are C-style string and String Class Library </li></ul><ul><li>The idea is to store a string in an array of char, with each character kept in its own array element. </li></ul><ul><li>Provide a convenient way to store text information </li></ul>
    13. 13. <ul><li>All strings end with a null character (‘’) </li></ul><ul><li>Examples: </li></ul><ul><li>char dog [5] = { ‘b’, ‘e’, ‘a’, ‘u’, ‘x’}; // not a string! char cat[5] = {‘f’, ‘a’, ‘t’, ‘s’, ‘’}; // a string! </li></ul><ul><li>Only the second declaration is a string because the ‘’ plays a major role C-style string </li></ul><ul><li>Subscription is the same as for a normal array </li></ul><ul><li>char cat [1] is ‘a’; </li></ul><ul><li>char cat [3] is ‘s’; </li></ul>
    14. 14. <ul><li>A better way to initialize a character array to a string. Just use a quoted string, called a string constant or string literal , as in the following: </li></ul><ul><li>char bird[10] = “Mr. Cheeps”; // the is understood char fish[] = “Bubbles”; // let the compiler count </li></ul><ul><li>Include the terminating null character implicitly, so you don’t have to spell it out </li></ul><ul><li>Initializing an array to a string: </li></ul>
    15. 15. <ul><li>char boss[8] = “Bozo”; </li></ul><ul><li>Note that a string constant (with double quotes) is not interchangeable with a character constant (with single quotes). </li></ul><ul><li>A character constant, such as ‘S’, is a shorthand notation for the code for a character. On an ASCII system, ‘S’ is just another way of writing 83. </li></ul>B o z o
    16. 16. <ul><li>Thus, the statement: </li></ul><ul><li>char shirt_size = ‘S’; // this is fine </li></ul><ul><li>But “S” represents the string consisting of two characters, the S and the characters. </li></ul><ul><li>Even worse, “S” actually represents the memory address at which the string is stored. So a statement like </li></ul><ul><li>char shirt_size = “S”; // illegal type mismatch </li></ul><ul><li>assigns a memory address to shirt_size. Because the address is a separate type in C++ </li></ul>
    17. 17. <ul><li>Concatenating String Constants </li></ul><ul><li>Long strings may be concatenated – by combining two quoted strings into one. </li></ul><ul><li>The output statements are equivalent: </li></ul><ul><li>cout << “I’d give my right arm to be” “ a great violinist. ”; cout << “I’d give my right arm to be a great violinist. ”; </li></ul><ul><li>cout << “I’d give my right ar” </li></ul><ul><li> “ m to be a great violinist. ”; </li></ul><ul><li>The first character of the second string immediately follows the last character, not counting , of the first string. The character from the first string is replaced by the first character of the second string. </li></ul>
    18. 18. <ul><li>Concatenating String Constants </li></ul><ul><li>Long strings may be concatenated – by combining two quoted strings into one. </li></ul><ul><li>The output statements are equivalent: </li></ul><ul><li>cout << “I’d give my right arm to be” “ a great violinist. ”; cout << “I’d give my right arm to be a great violinist. ”; </li></ul><ul><li>cout << “I’d give my right ar” </li></ul><ul><li> “ m to be a great violinist. ”; </li></ul><ul><li>The first character of the second string immediately follows the last character, not counting , of the first string. The character from the first string is replaced by the first character of the second string. </li></ul>
    19. 19. <ul><li>Using Strings in an Array </li></ul><ul><li>The two most common ways of getting a string into an array are to initialize an array to a string constant and to read keyboard or file input into an array. </li></ul><ul><li>Program illustration: </li></ul>
    20. 20. <ul><li>// strings.cpp -- storing strings in an array </li></ul><ul><li>#include <iostream> </li></ul><ul><li>#include <string> // for the strlen() function </li></ul><ul><li>int main() </li></ul><ul><li>{ </li></ul><ul><li>const int Size = 15; </li></ul><ul><li>char name1[Size]; // empty array </li></ul><ul><li>char name2[Size] = “C++owboy”; // initialized array </li></ul><ul><li>cout << “Howdy! I’m “ << name2; </li></ul><ul><li>cout << “! What is your name? ”; </li></ul><ul><li>cin >> name1; cout << “Well, “ << name1 << “, your name has “; </li></ul><ul><li>cout << strlen(name1) << “ letters and is stored ”; </li></ul><ul><li>cout << “in an array of “ << sizeof(name1) << “ bytes. ”; </li></ul><ul><li>cout << “Your initial is “ << name1[0] << “. ”; </li></ul><ul><li>name2[3] = ‘’; // null character </li></ul><ul><li>cout << “Here are the first 3 characters of my name: “; </li></ul><ul><li>cout << name2 << endl; </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>
    21. 21. <ul><li>Sample output of the program: </li></ul><ul><li>Howdy! I’m C++owboy! </li></ul><ul><li>What’s your name? Basicman </li></ul><ul><li>Well, Basicman, your name has 8 letters and is stored in an array of 15 bytes. </li></ul><ul><li>Your initial is B. </li></ul><ul><li>Here are the first 3 characters of my name: C++ </li></ul>
    22. 22. <ul><li>Program Notes: </li></ul><ul><ul><li>sizeof operator gives the size of the entire array </li></ul></ul><ul><ul><li>strlen() function returns the size of the string stored in the array and not the size of the array itself. It counts just the visible characters and not the null character </li></ul></ul><ul><ul><li>Because name1 and name2 are arrays, you can use an index to access individual characters in the array e.g. name1[0] finds the first character in that array. </li></ul></ul><ul><ul><li>the program sets name2[3] to the null character </li></ul></ul>
    23. 23. <ul><ul><li>The program uses a symbolic constant for the array size </li></ul></ul>
    24. 24. <ul><li>Adventures in String Input </li></ul><ul><li>Tricky string input: </li></ul><ul><li>// instr1.cpp -- reading more than one string </li></ul><ul><li>#include <iostream> </li></ul><ul><li>int main() { </li></ul><ul><li>const int ArSize = 20; </li></ul><ul><li>char name[ArSize]; </li></ul><ul><li>char dessert[ArSize]; </li></ul><ul><li>cout << “Enter your name: ”; </li></ul><ul><li>cin >> name; </li></ul><ul><li>cout << “Enter your favorite dessert: ”; </li></ul><ul><li>cin >> dessert; </li></ul><ul><li>cout << “I have some delicious “ << dessert; </li></ul><ul><li>cout << “ for you, “ << name << “. ”; </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>
    25. 25. <ul><li>Note that the cin offers no protection against placing a 30-character string in a 20-character array </li></ul>
    26. 26. <ul><li>Reading String Input a Line at a Time </li></ul><ul><li>Reading string input a word at a time is often not the most desirable choice. </li></ul><ul><li>To be able to enter a whole phrase you need to use a different approach to string input. </li></ul><ul><li>Specifically, you need a line-oriented method instead of a word- oriented method. </li></ul><ul><li>cin is an example of a word-oriented method while getline() and get() both read the entire input line up until the newline. </li></ul>
    27. 27. <ul><li>However, getline() then discards the newline character, whereas get() leaves it in the input queue. </li></ul><ul><li>Line-Oriented Input with getline() </li></ul><ul><li>Reads a whole line, using the newline character transmitted by the Enter key to mark the end of input. </li></ul><ul><li>Invoked by using cin.getline() as a function call. </li></ul>
    28. 28. <ul><li>The function takes two arguments: </li></ul><ul><ul><li>name of the array destined to hold the line of input </li></ul></ul><ul><ul><li>a limit on the number of characters to be read </li></ul></ul><ul><li>If this limit is, say, 20, the function reads no more than 19 characters, leaving room to automatically add the null character at the end. </li></ul><ul><li>It stops reading input when it reaches this numeric limit or when it reads a newline character, whichever comes first. </li></ul>
    29. 29. <ul><li>Suppose you want to read a name into a 20-element name array: </li></ul><ul><li>cin.getline(name,20); </li></ul><ul><li>// instr2.cpp -- reading more than one word with getline </li></ul><ul><li>#include <iostream> </li></ul><ul><li>int main() { </li></ul><ul><li>const int ArSize = 20; char name[ArSize]; char dessert[ArSize]; </li></ul><ul><li>cout << “Enter your name: ”; </li></ul><ul><li>cin.getline(name, ArSize); // reads through newline </li></ul><ul><li>cout << “Enter your favorite dessert: ”; cin.getline(dessert, ArSize); </li></ul><ul><li>cout << “I have some delicious “ << dessert; cout << “ for you, “ << name << “. ”; </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>
    30. 30. <ul><li>The getline() conveniently gets the whole line a time. </li></ul>
    31. 31. <ul><li>Line-Oriented Input with get() </li></ul><ul><li>The istream class has another member function, get(), which comes in several variations. </li></ul><ul><li>One variant works much like getline() </li></ul><ul><li>Suppose you use two calls to get() in a row: </li></ul><ul><li>cin.get(name, ArSize); </li></ul><ul><li>cin.get(dessert, Arsize); // a problem </li></ul>
    32. 32. <ul><li>Because the first call leaves the newline character in the input queue, that newline character is the first character the second call sees. </li></ul><ul><li>Thus, get() concludes that it’s reached the end of line without having found anything to read </li></ul><ul><li>The get() just can’t get past that newline character. </li></ul><ul><li>To read the newline use cin.get(), without arguments </li></ul>
    33. 33. <ul><li>// getvariation1.cpp -- reading more than one word with get() </li></ul><ul><li>#include <iostream> </li></ul><ul><li>int main() { </li></ul><ul><li>const int ArSize = 20; </li></ul><ul><li>char name[ArSize]; </li></ul><ul><li>char dessert[ArSize]; </li></ul><ul><li>cout << “Enter your name: ”; </li></ul><ul><li>cin.get(name, ArSize); // reads first line </li></ul><ul><li>cin.get(); // reads the newline </li></ul><ul><li>cout << “Enter your favorite dessert: ”; </li></ul><ul><li>cin.get(dessert, ArSize); </li></ul><ul><li>cout << “I have some delicious “ << dessert; </li></ul><ul><li>cout << “ for you, “ << name << “. ”; </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>
    34. 34. <ul><li>This is how the sequence works: </li></ul><ul><li>cin.get(name, ArSize); // read first line </li></ul><ul><li>cin.get(); // read newline </li></ul><ul><li>cin.get(dessert, Arsize); // read second line </li></ul><ul><li>Another variation is to use get() to concatenate, or join, the two class member functions, as follows: </li></ul><ul><li>cin.get(name, ArSize).get(); // concatenate member functions </li></ul><ul><li>What makes this possible is that cin.get(name, ArSize) returns the cin object, which is then used as the object that invokes the get() function </li></ul>
    35. 35. <ul><li>// getvar2.cpp -- reading more than one word with get() </li></ul><ul><li>#include <iostream> </li></ul><ul><li>int main() </li></ul><ul><li>{ </li></ul><ul><li>const int ArSize = 20; </li></ul><ul><li>char name[ArSize]; </li></ul><ul><li>char dessert[ArSize]; </li></ul><ul><li>cout << “Enter your name: ”; </li></ul><ul><li>cin.get(name, ArSize).get(); // reads string and newline </li></ul><ul><li>cout << “Enter your favorite dessert: ”; </li></ul><ul><li>cin.getline(dessert, ArSize).get(); </li></ul><ul><li>cout << “I have some delicious “ << dessert; </li></ul><ul><li>cout << “ for you, “ << name << “. ”; </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>
    36. 36. <ul><li>One thing to note is how C++ allows multiple versions of functions, provided that they have different argument lists. </li></ul><ul><li>By cin.get(name, ArSize), the compiler notices you’re using the form that puts a string into an array and sets up the appropriate member function. </li></ul><ul><li>Instead when you use cin.get(), the compiler realizes you want the form that reads one character. </li></ul>
    37. 37. <ul><li>Why use get() instead of getline() at all? </li></ul><ul><li>Older implementations may not have getline() </li></ul><ul><li>get() lets you be a bit more careful </li></ul><ul><li>Q: Suppose you used get() to read a line into an array. How can you tell if it read the whole line rather than stopped because the array was filled? </li></ul><ul><li>A: Look at the next input character </li></ul>
    38. 38. <ul><li>In short, getline() is a little simpler to use, but get() makes error checking simpler. </li></ul><ul><li>Empty Lines and Other Problems </li></ul><ul><li>What happens after getline() or get() reads an empty line? </li></ul><ul><li>Current practice is that after get() it sets something called the failbit. </li></ul><ul><li>The implications of this act are that further input is blocked but you can restore input with the following command: cin.clear(); </li></ul>
    39. 39. <ul><li>In short, getline() is a little simpler to use, but get() makes error checking simpler. </li></ul><ul><li>Empty Lines and Other Problems </li></ul><ul><li>What happens after getline() or get() reads an empty line? </li></ul><ul><li>Current practice is that after get() it sets something called the failbit. </li></ul><ul><li>The implications of this act are that further input is blocked but you can restore input with the following command: cin.clear(); </li></ul>
    40. 40. <ul><li>Another potential problem is that the input string could be longer than the allocated space. </li></ul><ul><li>If the input line is longer than the number of characters specified, both getline() and get() leave the remaining characters in the input queue. </li></ul>
    41. 41. <ul><li>Mixing String and Numeric Input </li></ul><ul><li>Mixing numeric input with line-oriented string input can cause problems </li></ul><ul><li>// numstr.cpp -- following number input with line input </li></ul><ul><li>#include <iostream> </li></ul><ul><li>int main() { </li></ul><ul><li>cout << “What year was your house built? ”; </li></ul><ul><li>int year; cin >> year; </li></ul><ul><li>cout << “What is its street address? ”; </li></ul><ul><li>char address[80]; </li></ul><ul><li>cin.getline(address, 80); </li></ul><ul><li>cout << “Year built: “ << year << endl; </li></ul><ul><li>cout << “Address: “ << address << endl; </li></ul><ul><li>cout << “Done! ”; </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>
    42. 42. <ul><li>Sample run of the program: </li></ul><ul><li>What year was your house built? </li></ul><ul><li>1966 </li></ul><ul><li>What is its street address? </li></ul><ul><li>Year built: 1966 </li></ul><ul><li>Address: </li></ul><ul><li>Done! </li></ul>
    43. 43. <ul><li>You never get the opportunity to enter the address. The problem is that when cin reads the year, it leaves the newline generated by the Enter key in the input queue. </li></ul><ul><li>Then, cin.getline() reads the newline as an empty line and assigns a null string to the address array. </li></ul><ul><li>The fix is to read and discard the newline before reading the address. </li></ul>
    44. 44. <ul><li>Use get() with no argument or with a char argument: </li></ul><ul><li>cin >> year; </li></ul><ul><li>cin.get(); // or cin.get(ch); </li></ul><ul><li>Or you can concatenate the call, making use of the fact that the expression cin >> year returns the cin object: </li></ul><ul><li>(cin >> year).get(); // or (cin >> year).get(ch); </li></ul>
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×