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

  • SEARCH
  • Home
  • 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 6766851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:52:51+00:00 2026-05-26T14:52:51+00:00

I am trying to write a function that prompts a user for a five

  • 0

I am trying to write a function that prompts a user for a five digit number, and I want to write an exception block to handle bad input incase the user tries to enter a string or some non-integer input.

I know how to write an exception handling block for something like a division function where you thrown an exception for the denominator being 0, but I have no clue how to do this for input that I have no control over.

  • 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-05-26T14:52:52+00:00Added an answer on May 26, 2026 at 2:52 pm

    First of all, I’d generally advise against this — bad input from the user is nearly the rule rather than the exception.

    If you insist on doing it anyway, you’d probably do it by accepting anything as input, and then throwing an exception when/if it’s not what you want:

    // Warning: untested code.    
    // Accepting negative numbers left as an exercise for the reader.
    int get_int(std::istream &is) { 
        std::string temp;
    
        std::getline(temp, is);
    
        for (int i=0; i<temp.size(); i++)
            if (!isdigit(temp[i]))
                throw std::domain_error("Bad input");
        return atoi(temp);
    }
    

    I repeat, however, that I don’t think this is generally a good idea. I’d note (for one example) that iostreams already define a fail bit specifically for situations like this (and the normal operator>> to read an int uses that mechanism).

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

Sidebar

Related Questions

I'm trying to write a function that accepts a variable number of parameters like
I'm trying to write a function that takes a revision number (int) and turns
I am trying to write a function that can handle both char & wchar_t
I'm trying to write a function that will take the email of the user
I'm trying to write a function that will return the number of levels from
I'm trying to write a function that has a concept of a serial number.
I'm trying to write a function that repeatedly matches regexp patterns against an input
I'm trying to write a function that pulls the lowest order number out of
I'm trying to write a function that formats every (string) member/variable in an object,
I am trying to write a function that will pull the name of a

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.