The
size_t is an old C type that basically means "some potentially-huge, non-negative integer".
Using strings is pretty straight-forward. There are just a couple things you need to be aware of.
Example 1: get a string from the user
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#include <iostream>
#include <string>
using namespace std;
int main()
{
string users_full_name, users_age_string;
// Ask the user to "enter" something
cout << "Please enter your full name> ";
// Get everything he typed up-to but not including the Enter key
getline( cin, users_full_name );
cout << "Please enter your age> ";
getline( cin, users_age_string );
...
return EXIT_SUCCESS;
}
|
Once you have the user's input as a string, you can use a
stringstream to parse and convert it into other things.
Example 2: convert a string to a number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int users_age;
... //(same stuff as before)
// Let's see if the user actually entered a non-negative number for his age
{
// (The extra parentheses make a local block, so that the stringstream we
// are about to create is only temporary to this block and not the whole function.)
stringstream age_stream( users_age_string );
// Try to convert it to a number
age_stream >> users_age;
// Check that 1: it was a valid number, and 2: it is non-negative
if (!age_stream or (users_age < 0))
{
if (!age_stream)
cout << "Your age must be a number.\n";
else
cout << "Hey there, you can't be less than 1.\n";
return EXIT_SUCCESS;
}
} // The age_stream disappears here. That's fine because we don't need it anymore.
...
return EXIT_SUCCESS;
}
|
Now that we've got his age, let's see if we can get the user's first name. A user can enter his full-name in one of two ways, given name(s) followed by surname(s), or surname(s), comma, given name(s):
John Jacob Jingleheimer Schmidt
Jingleheimer Schmidt, John Jacob
We'd like to allow the user to enter it either way. We'll use the very same technique to split both strings.
First, we'll separate the string into two strings if there is a comma in there, and keep only the second string. Now whatever string we have should begin with the first name.
Next, we'll separate the string by spaces. The very first word is the first name, and we can forget everything else.
Example 3: extracting the first name
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
int main()
{
string users_first_name;
... // (same stuff as before)
{
string temp_name;
if (users_full_name.find( ',', 0 ) != string::npos)
{
// The string contains a ",", so we'll get everything following that
stringstream users_names( users_full_name );
getline( users_names, temp_name, ',' ); // temp <-- everything before the ","
users_names >> ws; // skip any whitespace between the "," and the name
getline( users_names, temp_name ); // Get everything remaining
}
else
// No "," in user's name, so we'll continue normally
temp_name = users_full_name;
// Use the very same technique to split out the first name
stringstream first_names( temp_name );
getline( first_names, users_first_name, ' ' ); // users_first_name <-- everything before the first space
}
// Finally, complain if he didn't enter anything useful:
if (users_first_name.empty())
{
cout << "Yo, you must have a name like \"John Jones\" or something?\n";
return EXIT_SUCCESS;
}
...
return EXIT_SUCCESS;
}
|
At this point you can play with the user:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
...
if (users_age < 17)
cout << "Hey there " << users_first_name << ", you're too young to drive!\n";
else
if (users_age > 70)
cout << "Hey " << users_first_name << ", you old fella. Don't drive!\n";
else
cout << users_first_name << ", you are the right age to drive safely.";
return EXIT_SUCCESS;
}
|
Hope this helps.