I've been working my way through a beginning C++ book and have been rather frustrated with the lack of information on strings. Can anyone recommend a good book/website/something that can help me learn more about how to use strings and their member functions?
Well, I saw that link and was trying to work through it, but I think I'm missing some fundamentals of it that could be described better. For example, I kept seeing 'size_t' and searched it, but I guess I didn't really understand the definition and how all it could be used. The articles here seem very short and concise, but I can try working through them and posting questions I have about it here.
The reason I ask is because I've been trying to write a program that gets an entire sentence from the user and divides it up into separate words (putting them into a vector<string>), but I keep getting lost due to (what I think is) lack of information. I was thinking if I could understand substrings, the process would be a cinch, but I didn't quite get it.
#include <iostream>#include <string>usingnamespace 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.
#include <iostream>#include <string>#include <sstream>usingnamespace 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-negativeif (!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.
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";
elseif (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;
}