Mixing numeric input with line-oriented string input can cause problems. Consider the simple program in Listing 4.6.
// numstr.cpp -- following number input with line input
#include <iostream>
int main()
{
using namespace std;
cout << "What year was your house built?
";
int year;
cin >> year;
cout << "What is its street address?
";
char address[80];
cin.getline(address, 80);
cout << "Year built: " << year << endl;
cout << "Address: " << address << endl;
cout << "Done!
";
return 0;
}
Running the program in Listing 4.6 would look something like this:
What year was your house built?
1966
What is its street address?
Year built: 1966
Address
Done!
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. Then cin.getline()
reads the newline as an empty line and assigns a null string to the address
array. The fix is to read and discard the newline before reading the address. This can be done several ways, including by using get()
with a char
argument or with no argument, as described in the preceding example. You can make these calls separately:
cin >> year;
cin.get(); // or cin.get(ch);
Or you can concatenate the calls, making use of the fact that the expression cin >> year
returns the cin
object:
(cin >> year).get(); // or (cin >> year).get(ch);
If you make one of these changes to Listing 4.6, it works properly:
What year was your house built?
1966
What is its street address?
43821 Unsigned Short Street
Year built: 1966
Address: 43821 Unsigned Short Street
Done!
C++ programs frequently use pointers instead of arrays to handle strings. We’ll take up that aspect of strings after talking a bit about pointers. Meanwhile, let’s take a look at a more recent way to handle strings: the C++ string
class.
string
ClassThe ISO/ANSI C++98 Standard expanded the C++ library by adding a string
class. So now, instead of using a character array to hold a string, you can use a type string
variable (or object, to use C++ terminology). As you’ll see, the string
class is simpler to use than the array and also provides a truer representation of a string as a type.
To use the string
class, a program has to include the string
header file. The string
class is part of the std
namespace, so you have to provide a using
directive or declaration or else refer to the class as std::string
. The class definition hides the array nature of a string and lets you treat a string much like an ordinary variable. Listing 4.7 illustrates some of the similarities and differences between string
objects and character arrays.
// strtype1.cpp -- using the C++ string class
#include <iostream>
#include <string> // make string class available
int main()
{
using namespace std;
char charr1[20]; // create an empty array
char charr2[20] = "jaguar"; // create an initialized array
string str1; // create an empty string object
string str2 = "panther"; // create an initialized string
cout << "Enter a kind of feline: ";
cin >> charr1;
cout << "Enter another kind of feline: ";
cin >> str1; // use cin for input
cout << "Here are some felines:
";
cout << charr1 << " " << charr2 << " "
<< str1 << " " << str2 // use cout for output
<< endl;
cout << "The third letter in " << charr2 << " is "
<< charr2[2] << endl;
cout << "The third letter in " << str2 << " is "
<< str2[2] << endl; // use array notation
return 0;
}
Here is a sample run of the program in Listing 4.7:
Enter a kind of feline: ocelot
Enter another kind of feline: tiger
Here are some felines:
ocelot jaguar tiger panther
The third letter in jaguar is g
The third letter in panther is n
You should learn from this example that, in many ways, you can use a string object in the same manner as a character array:
• You can initialize a string
object to a C-style string.
• You can use cin
to store keyboard input in a string
object.
• You can use cout
to display a string
object.
• You can use array notation to access individual characters stored in a string
object.
The main difference between string
objects and character arrays shown in Listing 4.7 is that you declare a string
object as a simple variable, not as an array:
string str1; // create an empty string object
string str2 = "panther"; // create an initialized string
The class design allows the program to handle the sizing automatically. For instance, the declaration for str1
creates a string
object of length zero, but the program automatically resizes str1
when it reads input into str1
:
cin >> str1; // str1 resized to fit input
This makes using a string
object both more convenient and safer than using an array. Conceptually, one thinks of an array of char
as a collection of char
storage units used to store a string but of a string
class variable as a single entity representing the string.
18.218.212.102