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 9187951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:53:53+00:00 2026-06-17T19:53:53+00:00

I’m doing a problem where it asks to input an account number, which consists

  • 0

I’m doing a problem where it asks to input an account number, which consists only of four digits. This has to be accomplished with basic beginner C++.

I need to figure out a way to restrict the input of the integer to four digits. A user should be able to put in 0043 or 9023 or 0001 and it should be an acceptable value….

I think I know how to accomplish it with a string…. getline(cin,input) and then check if input.length()==4?

But I’ve no idea how I would even do this with an integer 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-17T19:53:54+00:00Added an answer on June 17, 2026 at 7:53 pm

    Something like this should work. Once the user enters something with exactly four characters you can validate it. The rest of the logic is up to you.

    #include <iostream>
    #include <string>
    
    int main() {
        std::cout << "Enter a PIN Number: ";
        std::string pinStr;
        while(std::getline(std::cin,pinStr) && pinStr.size() != 4) {
            std::cout << "Please enter a valid value\n";
        }
    }
    

    Should you want to store it in an integer form, holding the integers in an std::vector might be beneficial. You can do this easily (loop unrolling was for clarity):

    #include <iostream>
    #include <string>
    #include <vector>
    
    int main() {
        std::cout << "Enter a PIN Number: ";
        std::string pinStr;
        while(std::getline(std::cin,pinStr) && pinStr.size() != 4 ) {
            std::cout << "Please enter a valid value\n";
        }
        std::vector<int> pin;
        pin[0] = pinStr[0] - '0';
        pin[1] = pinStr[1] - '0';
        pin[2] = pinStr[2] - '0';
        pin[3] = pinStr[3] - '0';
    
        //pin now holds the integer value.
        for(auto& i : pin)
            std::cout << i << ' ';
    }
    

    You can see it running here

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an array which has BIG numbers and small numbers in it. I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,

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.