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.
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.
Should you want to store it in an integer form, holding the integers in an
std::vectormight be beneficial. You can do this easily (loop unrolling was for clarity):You can see it running here