Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8709463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:22:05+00:00 2026-06-13T04:22:05+00:00

Hey i want to stop the user from entering integers when i ask the

  • 0

Hey i want to stop the user from entering integers when i ask the user to input a name. I have achieved this for an integer and a char. Can anyone help me adapt my code for a string.

int getNum()
{
    int num;
    std::cout << "\nWhat is your age? ";
    while (!(std::cin >> num))
    {
        // reset the status of the stream
        std::cin.clear();
        // ignore remaining characters in the stream
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cout << "Enter an *integer*: ";
    }
    std::cout << "You entered: " << num << std::endl;
    return num;
}

char getChar(string q)
{
    char input;
    do
    {
        cout << q.c_str() << endl;
        cin >> input;
    }
    while(!isalpha(input));
    return input;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:22:07+00:00Added an answer on June 13, 2026 at 4:22 am
    string q = "This is a test123";
    
    for(string::iterator i = q.begin(); i != q.end(); i++)
    {
        if((*i < 'A' || *i > 'z') && (*i != ' '))
        {
            return false;
        }
    }
    

    would also be an option, if you allow for spaces and other characters.

    Edit: updated for checking a single char:

    char c;
    bool finished = false;
    printf("Please enter your sex, M/F?\n");
    while(!finished)
    {
        cin >> c;
        if(!(c == 'm' || c == 'M' || c== 'f' || c=='F'))
        {
            printf("Please try again...\n");
        }
        else
        {
            finished = true;
        }
    }
    

    Note that c is only input, char by char, when Enter is pressed, before that the line feed does not happen.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hey every one I want to alert value of n from this string $('.orbit-bullets:has(
Hey, So I have 5 images I want in a UIScrollView the user can
Hey I want to select records with name O'Neil, How can i do that
hey i guys i want help from you. i am working on website project,
Hey guys, I want to store a categorized list of URLs. This is an
hey, hi i want put limit on object creation means a class can have
Hey everyone, I have achieved the effect described in the title using the following
Hey guys, I want to make a function loop over all elements that have
Hey i want to use php to redirect a user to a url depending
hey I want to make a call from my application, so far I found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.