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.
# include <iostream>
# include <limits>
usingnamespace 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;
}
elseif(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;
}
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?
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.
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.-------------------------------- ^
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.
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?