0

i've started learning c++ today. Now i think that i've got the basics, i couldn't get with this program i'm trying to execute. Basically all program need to do is, output the user first name, and last name. Now i got it to work, but i want that if the user input a random number, the program won't execute, and output "Only Text allowed. Please enter your name again: " also, how do i create space between the first and last name? would appreciate help. thanks!

this is the code:

int main()
{

    int Num;
    string First;
    string Last;

    cout << "Type in your first name please:";

    cin >> First;

    if (not sure what to declare here)
    {
        cout >> "No numbers allowed. only Text.";
    }

    cout << "Type your last name please:";

    cin >> Last;

    cout << "your full name is:" << " " << First + Last;
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • There is nothing in your sample code that would output the "Only Text allowed" string. – 0x5453 Dec 10 '20 at 18:41
  • What does your C++ book say about `if`s? – Stephen Newell Dec 10 '20 at 18:42
  • You should work on finishing your assignment yourself. You'll learn much more that way, and it gives you a chance to apply what you're learning in class. If you can't get started, you should ask your instructor for help; it may mean that they're not doing a good job while teaching concepts. – Ken White Dec 10 '20 at 18:42
  • 3
    Unrelated: I'm sure your teacher taught your to do `using namespace std;` but for your own sake, unlearn that quickly. [Why is `using namespace std;` considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Ted Lyngmo Dec 10 '20 at 18:44
  • i've deleted it, because it looked vry messy. ill post it any ways now int main() { int Num; string First; string Last; cout << "Type in your first name please:"; cin >> First; if(not sure what to declare here) { cout >> "No numbers allowed. only Text."; } cout << "Type your last name please:"; cin >> Last; cout << "your full name is:"<< " " << First + Last; } – Newbie2312 Dec 10 '20 at 18:48
  • 2
    `cout << "Your full name is:" << First << " " << Last << ".\n";` – Eljay Dec 10 '20 at 18:49
  • thanks elijay that did it. but what about the if statement? – Newbie2312 Dec 10 '20 at 18:56
  • You need to search the string or find any numbers in the string. – Thomas Matthews Dec 10 '20 at 18:57
  • Computers are only smart because they can do lots of really stupid things really, really quickly. The same thing you would do to check whether or not there was a number in a batch of characters will work when done on a computer. But because a computer is really good at repetitive, simple tasks like book-keeping, you can often find more round about ways to get jobs done faster that would choke most human brains. Start with the way you'd solve a problem with your brain, and if the result is too slow start trying to think like a computer to find patterns you can exploit. – user4581301 Dec 10 '20 at 19:15

1 Answers1

2

You will need to search the string for digits.
Here is one method to find numeric digits in a string. There are many others.

static const char digits[] = "0123456789".
if (First.find_first_of(digits) != std::string::npos)
{
   cout << "First name has at least one digit.\n";
}

Edit 1:
If that is not easily understood, here is a more basic version:

const unsigned int length = First.length();
for (unsigned int i = 0; i < length; ++i)
{
    const char c = First[i];
    if (std::isdigit(c))
    {
        std::cout << "Name has a digit, at position " << i << "\n";
        break;
    }
}
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154