"if" question

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?

does my question make sense?
does my question make sense?

Not yet.

Do you want to test whether the inputted string contains two words?
okay i want a program that asks me for my name-
if i pput it only one word like just Vlad
i want the program to say "first and last name plz"
I see, what do you have so far?
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
using namespace 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)).

Links :
http://cplusplus.com/doc/tutorial/control/ <-- for the if-else structrure
http://cplusplus.com/reference/string/string/find_first_of/ <-- for finding characters in the string.
Topic archived. No new replies allowed.