Cant figure this out

Sep 5, 2013 at 7:16pm
Ok i have been trying to figure out how to make it so if my string is empty then ask for a name until it isnt empty but i cant figure it out the problem is on lines 5-11

i also tried if(!name.empty()) but that didnt work either.

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
37
38
39
40
41
42
43
while(enterNameLoopEnd != true)
    {
        getline(cin, name);

        while(nameInputError != true)
        {
            if(name.length() >= 1)
            {
                nameInputError = true;
                break;
            }
            cout << "You must enter a name to use!" << endl;
            cout << "Name: ";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }

        cout << "Are you satisfied with the name " << name << "?" << endl;
        cout << "1) Yes" << endl;
        cout << "2) No" << endl;
        cin >> satisfied;

        //The code below is error handling so if the user enters an undesired input
        //then the program wont flip out.
        if(satisfied == 1)
        {
            break;
        }
        else if(satisfied == 2)
        {
            cout << "Ok please enter the name you wish to use\n" << endl;
            cout << "Name: ";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
        else
        {
            cout << "Invalid response please enter the name you wish to use\n" << endl;
            cout << "Name: ";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
    }
Last edited on Sep 5, 2013 at 7:19pm
Sep 5, 2013 at 7:27pm
closed account (28poGNh0)
why not at this way

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# include <iostream>
# include <limits>
using namespace std;

int main()
{
    string name;
    int satisfied;
    bool enterNameLoopEnd = false;

    while(enterNameLoopEnd != true)
    {
        getline(cin, name);

        while(true)
        {
            if(name[0])break;
            else
            {
                cout << "You must enter a name to use!" << endl;
                cout << "Name: ";
                getline(cin,name);
            }
        }

        cout << "Are you satisfied with the name " << name << "?" << endl;
        cout << "1) Yes" << endl;
        cout << "2) No" << endl;
        cin >> satisfied;

        //The code below is error handling so if the user enters an undesired input
        //then the program wont flip out.
        if(satisfied == 1)
        {
            break;
        }
        else if(satisfied == 2)
        {
            cout << "Ok please enter the name you wish to use\n" << endl;
            cout << "Name: ";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
        else
        {
            cout << "Invalid response please enter the name you wish to use\n" << endl;
            cout << "Name: ";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
    }
    return 0;
}
Sep 5, 2013 at 7:29pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
        bool valid_name = true;
      
        do
        {
                cout << "Name: ";

                getline(cin, name);

                valid_name = name.find_first_not_of(( " \t" ) != string::npos;

                if ( !valid_name )
                {
                        cout << "You must enter a name to use!" << endl;
                }
        } while ( !valid_name );
Sep 5, 2013 at 7:43pm
ok thanks, but i dont understand this line
valid_name = name.find_first_not_of(( " \t" ) != string::npos;

I looked up find_first_not_of and if i understand it correctly if it doesnt find a tab then it doesnt equal a new position? why not use '\n' instead of '\t'? also why is valid_name being assigned name.find_first_not_of(( " \t" ) != string::npos; when its a boolean value and already has a value in it? wont that overide it?
Last edited on Sep 5, 2013 at 7:47pm
Sep 5, 2013 at 7:53pm
There are two characters: the blank and the tab. So if there is no other characters except blanks and tabs in the string then it means that the string is empty.

In every iteration of the loop you should check whether the input is valid. So variable valid_name is set in each iteration.
Last edited on Sep 5, 2013 at 7:55pm
Sep 5, 2013 at 9:05pm
ah i see so its checking fot the blank at the space " \t" then the tab so if either of those are in the string then its declared to be empty.-------------------------------- ^
Last edited on Sep 5, 2013 at 9:06pm
Sep 5, 2013 at 9:29pm
@Ch1156


If there are no other characters except blanks and tabs then it means that the string is empty. That is its length (or size) can be greater than 0 but it contains nothing except blanks and tabs.
Sep 5, 2013 at 10:29pm
I see so basically that line of code is saying "if valid_name equals tabs or spaces then it doesnt equal a new position so therefore its blank" correct?
Sep 6, 2013 at 7:49pm
bump
Topic archived. No new replies allowed.