I've set a tiny program that reads your name and then checks with you if it's right, but I either repeat some code, or I use goto and I can't find a good way to do it.
#include <iostream>
#include <string>
int main()
{
std::string name, rightname;
std::cout << "Brave adventurer, what is your name?" << "\n";
do getline (std::cin, name);
while (name == "" or name == " " or name == " " or name == " " or name == " ");
std::cout << "\n" << "Your name is " << name << " right?" << "\n";
getline (std::cin, rightname);
while (rightname != "Y" and rightname != "y" and rightname != "yes" and rightname != "Yes")
{
std::cout << "\n" << "Oh, I'm sorry, I'm a little deaf, must be my old age. So what is your name?" << "\n";
do getline (std::cin, name);
while (name == "" or name == " " or name == " " or name == " " or name == " ");
std::cout << "\n" << "Your name is " << name << " right?" << "\n";
getline (std::cin, rightname);
}
return 0;
}
#include <iostream>
#include <string>
int main()
{
std::string name, rightname;
std::cout << "Brave adventurer, what is your name?" << "\n";
do getline (std::cin, name);
while (name == "" or name == " " or name == " " or name == " " or name == " ");
checkname:
std::cout << "\n" << "Your name is " << name << " right?" << "\n";
getline (std::cin, rightname);
while (rightname != "Y" and rightname != "y" and rightname != "yes" and rightname != "Yes")
{
std::cout << "\n" << "Oh, I'm sorry, I'm a little deaf, must be my old age. So what is your name?" << "\n";
do getline (std::cin, name);
while (name == "" or name == " " or name == " " or name == " " or name == " ");
goto checkname;
}
return 0;
}
What would be the correct (and most efficient!) way to do it.
Another thing, how can you check that the first letter in the name is a character (that way you could avoid the long dowhile(...); and the current code will accept a name if it's 5 or more spaces)
#include <iostream>
#include <string>
int main()
{
std::string name, rightname;
std::cout << "Brave adventurer, what is your name?" << "\n";
do getline (std::cin, name);
while (name == "" or name == " " or name == " " or name == " " or name == " ");
std::cout << "\n" << "Your name is " << name << " right?" << "\n";
getline (std::cin, rightname);
if (rightname != "Y" and rightname != "y" and rightname != "yes" and rightname != "Yes")
{
std::cout << "\n" << "Oh, I'm sorry, I'm a little deaf, must be my old age. So what is your name?" << "\n";
do getline (std::cin, name);
while (name == "" or name == " " or name == " " or name == " " or name == " ");
std::cout << "\n" << "Your name is " << name << " right?" << "\n";
getline (std::cin, rightname);
}
return 0;
}
#include <iostream>
#include <string>
int main()
{
std::string name, rightname;
std::cout << "Brave adventurer, what is your name?" << "\n";
do getline (std::cin, name);
while (name == "" or name == " " or name == " " or name == " " or name == " ");
do
{
std::cout << "\n" << "Your name is " << name << " right?" << "\n";
getline (std::cin, rightname);
std::cout << "\n" << "Oh, I'm sorry, I'm a little deaf, must be my old age. So what is your name?" << "\n";
do getline (std::cin, name);
while (name == "" or name == " " or name == " " or name == " " or name == " ");
} while (rightname != "Y" and rightname != "y" and rightname != "yes" and rightname != "Yes");
return 0;
}
#include <iostream>
#include <string>
int main()
{
std::string name, rightname;
std::cout << "Brave adventurer, what is your name?" << "\n";
while(getline (std::cin, name))
{
if (isalpha(name[0])) //Check if first character is space.
{
std::cout << "\n" << "Your name is " << name << " right?" << "\n";
getline (std::cin, rightname);
while (rightname != "Y" and rightname != "y" and rightname != "yes" and rightname != "Yes")
{
std::cout << "\n" << "Oh, I'm sorry, I'm a little deaf, must be my old age. So what is your name?" << "\n";
getline (std::cin, name);
if (!isalpha(name[0]))
{
std::cerr << "Whoops there was an error. Do not start your name with a space" << "\n";
break;
}
std::cout << "\n" << "Your name is " << name << " right?" << "\n";
getline (std::cin, rightname);
}
}
else
{
std::cerr << "Whoops there was an error. Do not start your name with a space" << "\n";
}
if (rightname == "Y" || rightname == "y" || rightname == "yes" || rightname == "Yes")
break;
}
return 0;
}
Not sure if it's the best solution, but here's what I came up with.
Yeah I should have actually read your entire code. I edited it above and I think it's working as you may want it. It could use some tidying up though imo.
string name;
getline(cin, name);
for (auto check : name)
{
if (!isalpha(check)) // Check if the string has any non-characters.
{
cout << "Error" << endl;
}
else
cout << "No errors" << endl;
}
To check isalpha() on each letter is actually bad: Some people will use their nickname and it can have digits.
In addition in some cultures names can have an apostrophe or space in them. Some characters from foreign languages can be not allowed by isalpha() too.
Names and time is the worst nightmare for programmer actually.
Then how can I allow only letters, digits and apostrophes (but not asterisks, spaces, slashes, etc.) ?
Actually only allowing letters is alright, you just have to tell the user to not use digits if they try to.
@Vlad
Your code won't compile without -std=c++11
And it accepts a name if you just press enter. It doesn't accept names with spaces though.
Actually everything in your code can be bypassed by pressing enter, I think it would be better to wait until you get a valid answer.
And how can you get rid of having two variables: answered and yes_no, and replace them by one? I think this is slightly over-complicated.
I updated the code.
As for -std=c++11 then you should always use it.
And I do not think that introducing a variable of type bool that is set to the value of expression yes_no == "Y" || yes_no == "YES"; makes the code sligltly complicated. It makes the code more clear!
Is letter like a function or is it a char array and you have to assign every element in the string to the array and then run this condition for the whole string length?
I'm really confused right now and I can't see how your code can work, unless the compiler knows the alphabet.
Yeah, I'm tired... sorry if this is silly me...