validate integer length?

Hi. I need help adding input validation to my program.

When the user inputs his account number, it should only be 5 digits long.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//Begin execution
int main(){
    //Declare variables
    unsigned short bal,totChk, totDep;//account number,balance, total checks, total deposits
    float newBal, ovrBal;//new balance, overdrawn balance if account is overdrawn
    int accNum;

    //Prompt reader for inputs
    cout<<" This program will determine if you "
       <<" have overdrawn your checking account "<<endl;
    cout<<endl;
    cout<<" Begin by entering your 5-digit account number "<<endl;
    cin>>accNum;
    cout<<" Enter your balance at the beginnning of the month "<<endl;
    cin>>bal;
    cout<<" Enter the total of all checks written this month "<<endl;
    cin>>totChk;
    cout<<" Enter the total of all deposits credited to your account this month "<<endl;
    cin>>totDep;

    //Calculate new balance
    newBal=bal+(totDep-totChk);
    cout<<" Total Balance for account number "<<accNum<<endl;
    cout<<" Is: "<<newBal<<" Dollars"<<endl;

    //Charge account $17.50 if overdrawn
    if(newBal<0){
        cout<<" Your account has been overdrawn "
           <<" A fee of $17.50 has been charged "<<endl;
        cout<<" Your new account balance is "<<endl;
        ovrBal=newBal-17.50;
        cout<<ovrBal;

    }
    return 0;
}


I've tried adding something like
1
2
3
4
if(accNum<0&&accNum<9999){
        cout<<" Account numbers are five digits long "<<endl;
        cin>>accNum;
    }


But it still allows me to enter more and less than 5 digits.

I'd appreciate the help
if(accNum<0&&accNum<9999)

This reads like: if accNum is less than 0 and accNum is less than 9999.
In other words, it means it will never be triggered unless accNum is less than 0.
so would this work?
if(accNum>0&&accNum<9999)
Last edited on
What happened when you tried it?
It validates it the first time if i put a number with less than 5 digits and then the second it does not validate it.
It doesn't validate it for numbers bigger than 5 either
So when you entered a number less than 9999 it asked for another and then went to the next statement right? If I understand right, it's not supposed to ask again. So first you need to reverse the condition

if(accNum < 0 || accNum > 9999)

Also to get it to continue asking until a right number is input, you need to use a loop or it will only ask once before going to the next statement.
Would it be a for loop or while loop?
for loops are used mainly when you know how many times you need to iterate through the loop so that's not really appropriate in this case. Most people would use a do/while loop, but I generally would just use a while loop for this.

Also, those values don't work to only accept 5 digit numbers, but I'm sure you can figure out how to change them on your own.
5 digits could also includes negative values.
True, but can you give an instance of an account number that does? It would be more likely that it contained letters and my first thought was to use string instead of int, that way you only needed to check its length. In my opinion that's over complicating this problem though.
Topic archived. No new replies allowed.