how can i make it so that when i use input strings to ask for a name can i make it so if the user does not put in a first and last name i can make some "if" error messege?
basically what code differentiates between 1 or 2 or more words?
so far just the basic string code down...i was thinking i would add an if command after it asks my name but i have no idea what the code would look like:
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string str1, str2;
cout<<" Please enter you name: ";
getline (cin, str1);
cout<<" Thank you, "<<str1<<".\n";
cout<<" What is you age? ";
getline (cin, str2);
cout<<" Great "<<str1<<" you are "<<str2<<" years old.";
cin.get();
return 0;
}
The string contains's at least two words if there is one or more white space.
So if we found one white space then the input is valid.
Now you either write the iterating loop yourself (not recommended), or use standard functions of the string class (which does essentially the same as writing it by hand, but is more readable (and sometimes faster as well)).