Integer input limited to only four digits

I am making a problem when he asks for an account number that consists of only four digits. This should be done with a basic C ++ newbie.

I need to figure out a way to limit the input of an integer to four digits. The user should be able to enter 0043 or 9023 or 0001, and this should be an acceptable value ....

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

But I don’t know how I could do this with integer input.

+5
source share
6 answers

- . - , . .

#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";
    }
}

, std::vector . ( ):

#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 << ' ';
}

,

+1

, 0043 43, , , .

input.

, input 4.

, <= '9' >= '0'.

- :

std::string read4DigitStringFromConsole()
{
    bool ok = false;
    std::string result;
    while (!ok)
    {
        std::cin >> result;
        if (result.length() == 4)
        {
            bool allDigits = true;
            for(unsigned index = 0; index < 4; ++index)
            {
                allDigits = allDigits && ( 
                    (result[index] >= '0') && 
                    (result[index] <='9') 
                    );
            }
            ok = allDigits;
        }
    }
    return result;
}
+2

string . , "" . . if (sizeof(input)==4) , string. - if (input.length() == 4). , 4 . , . , , ASCII , . , if (input[i] >= '0' && input[i] <= '9') i. , , , - , , -, .

Edit:

, , int int value = atoi(input.c_str());. , int .

+1
// generic solution
int numDigits(int number)
{
    int digits = 0;
    if (number < 0) digits = 1; // remove this line if '-' counts as a digit
    while (number) {
        number /= 10;
        digits++;
    }
    return digits;
}

. , , 4 .

0

, , , , :

std::string fourDigits;
char currentDigit;

std::cout << "Enter 4 digits\n";
for(int i = 0; i < 4; ++i)
{
    currentDigit = getch();
    if(isdigit(currentDigit))
    {
        fourDigits += currentDigit;
        std::cout << currentDigit; // getch won't display the input, if it was a PIN you could simply std::cout << "*";
    }
    else
    {
            // Here we reset the whole thing and let the user know he entered an invalid value
        i = 0;
        fourDigits = "";
        std::cout << "Please enter only numeric values, enter 4 digits\n";
    }
}

std::cout << "\nThe four digits: " << fourDigits.c_str();

. , Enter.

0

So, I went on to how I can use an integer type to enter input, and looked at char ... since it is technically the smallest integer type, it can be used to get the code ... I was able to come up with this, but it definitely not yet specified (and I'm not sure what it might be):

int main() {
    int count=0;

    while(!(count==4)){
        char digit;
        cin.get(digit);
        count++;
    }

    return 0;
}

So, the cycle continues until 4 characters are collected. Well, theoretically it should be. But that will not work. It will stop at 2 digits, 5 digits, etc. I think it could be the character cin.get () capturing a space, not sure.

0
source

All Articles