c++macosc++11conditional-statementsrange-checking

conditional statement to verify range provided by #define(d) integers in header for transaction module (.cpp)


First time posting on stackoverflow. I am not sure if the question is clear enough so I will try to explain the problem and provide my code as well.

I am guessing my problem is some issue that has to do with the conditional if statements that I have created in the definition of the member functions that are mentioned in the header. The header has #define statements that have integer values for MIN and MAX for integer variables that will be accepted from input of a user. I cannot seem to be able to validate the number from the input provided. I have run a test with checkvalues issued before and after conditional statement and they seem to be verified no matter what is entered. I had all the conditions in one if statement as well if that makes any difference.

The following is code for a simple accounting app it contains a header and transaction(.cpp) module and main.cpp:

// BTP200 Workshop 2: Compound types and privacy MAIN.CPP

#include <iostream>
#include "AccountNumber.hpp"

using namespace std;
using namespace sict;

int main()
{
    AccountNumber myNumber;
    char name[41];
    int bankCode;
    int branchCode;
    int accNumber;

    cout << "Bank account app" << endl <<
    "===================" << endl << endl;
    cout << "Please enter your name: ";
    cin >> name;
    myNumber.setName(name);
    cout << "please enter your bank account, branch code" <<
    ", and account number as follows:" << endl << "999 999 99999: ";

    do
    {
        cin >> bankCode >> branchCode >> accNumber;
        myNumber.setAccountNumber(bankCode, branchCode, accNumber);
        myNumber.display();
    } while(!myNumber.isValid() && cout << "Invalid account number, (999 999 99999), try again: ");

    cout << "Thank you!" << endl;

    return 0;
}

ACCOUNTNUMBER.CPP

#include <iostream>
#include <cstring>
#include "AccountNumber.hpp"

#define MAX_NAME_LENGTH 40
#define MIN_BANKCODE    100
#define MAX_BANKCODE    999
#define MIN_BRANCHCODE  1
#define MAX_BRANCHCODE  220
#define MIN_ACCNO       10000
#define MAX_ACCNO       99999

using namespace std;

namespace sict
{
    void AccountNumber::setName(const char n[])
    {
        strcpy(name, n);
    };

    void AccountNumber::setAccountNumber(int bankc, int branchc, int an)
    {
        validAccNumber = false;

        int bankc_v=0, branchc_v=0, an_v=0;

        cout << bankc_v << branchc_v << an_v; //TESTING

        if (bankc >= MIN_BANKCODE || bankc <= MAX_BANKCODE)
        {
            bankCode = bankc;
            bankc_v=1;
        }
        else if (branchc >= MIN_BRANCHCODE || branchc <= MAX_BRANCHCODE)
        {
            branchCode = branchc;
            branchc_v=1;
        }
        else if (an_v >= MIN_ACCNO || an_v <= MAX_ACCNO)
        {
            accountNumber = an;
            an_v=1;
        }

        cout << bankc_v << branchc_v << an_v; //TESTING



        else if(bankc_v==1 && branchc_v==1 && an_v==1)
        {
            validAccNumber = true;
        }
    }

    void AccountNumber::display() const
    {
        if(isValid() == 1)
        {
            cout << "Name: " << name << ", Account number: " << bankCode << "-"
            << branchCode << "-" << accountNumber << endl;
        }
        else
        {
            cout << name << " does not have a valid account number." << endl;
        }
    }

    bool AccountNumber::isValid() const
    {
        return AccountNumber::validAccNumber;
    }   
}

ACCOUNTNUMBER.HPP

#ifndef AccountNumber_hpp
#define AccountNumber_hpp

#include <stdio.h>

#define MAX_NAME_LENGTH 40
#define MIN_BANKCODE    100
#define MAX_BANKCODE    999
#define MIN_BRANCHCODE  1
#define MAX_BRANCHCODE  220
#define MIN_ACCNO       10000
#define MAX_ACCNO       99999

namespace sict
{
    class AccountNumber
    {
        char name[MAX_NAME_LENGTH + 1];
        int  bankCode;
        int  branchCode;
        int  accountNumber;
        bool validAccNumber;

    public:
        void setName(const char n[]);
        void setAccountNumber(int bankc, int branchc, int  an);
        void display() const;
        bool isValid() const;
    };
}


#endif /* AccountNumber_hpp */

***UPDATED****

// BTP200 Workshop 2: Compound types and privacy MAIN.CPP

#include <iostream>
#include "AccountNumber.hpp"

using namespace std;
using namespace sict;

int main()
{
    AccountNumber myNumber;
    char name[41];
    int bankCode;
    int branchCode;
    int accNumber;

    cout << "Bank account app" << endl <<
    "===================" << endl << endl;
    cout << "Please enter your name: ";
    cin >> name;
    myNumber.setName(name);
    cout << "please enter your bank account, branch code" <<
    ", and account number as follows:" << endl << "999 999 99999: ";

    do
    {
        cin >> bankCode >> branchCode >> accNumber;
        myNumber.setAccountNumber(bankCode, branchCode, accNumber);
        myNumber.display();
    } while(!myNumber.isValid() && cout << "Invalid account number, (999 999 99999), try again: ");

    cout << "Thank you!" << endl;

    return 0;
}

ACCOUNTNUMBER.CPP

#include <iostream>
#include <cstring>
#include "AccountNumber.hpp"

#define MAX_NAME_LENGTH 40
#define MIN_BANKCODE    100
#define MAX_BANKCODE    999
#define MIN_BRANCHCODE  1
#define MAX_BRANCHCODE  220
#define MIN_ACCNO       10000
#define MAX_ACCNO       99999

using namespace std;

namespace sict
{
    void AccountNumber::setName(const char n[])
    {
        strcpy(name, n);
    };

    void AccountNumber::setAccountNumber(int bankc, int branchc, int an)
{
    validAccNumber = false;

    int bankc_v=0, branchc_v=0, an_v=0;

    cout << bankc_v << branchc_v << an_v;

    if (bankc >= MIN_BANKCODE && bankc <= MAX_BANKCODE)
    {
        bankCode = bankc;
        bankc_v=1;
    }
    if (branchc >= MIN_BRANCHCODE && branchc <= MAX_BRANCHCODE)
    {
        branchCode = branchc;
        branchc_v=1;
    }
    if (an_v >= MIN_ACCNO && an_v <= MAX_ACCNO)
    {
        accountNumber = an;
        an_v=1;
    }

    cout << bankc_v << branchc_v << an_v;

    if(bankc_v==1 && branchc_v==1 && an_v==1)
    {
        validAccNumber = true;
    }
}


    void AccountNumber::display() const
    {
        if(isValid() == 1)
        {
            cout << "Name: " << name << ", Account number: " << bankCode << "-"
            << branchCode << "-" << accountNumber << endl;
        }
        else
        {
            cout << name << " does not have a valid account number." << endl;
        }
    }

    bool AccountNumber::isValid() const
    {
        return AccountNumber::validAccNumber;
    }   
}

ACCOUNTNUMBER.HPP

#ifndef AccountNumber_hpp
#define AccountNumber_hpp

#include <stdio.h>

#define MAX_NAME_LENGTH 40
#define MIN_BANKCODE    100
#define MAX_BANKCODE    999
#define MIN_BRANCHCODE  1
#define MAX_BRANCHCODE  220
#define MIN_ACCNO       10000
#define MAX_ACCNO       99999

namespace sict
{
    class AccountNumber
    {
        char name[MAX_NAME_LENGTH + 1];
        int  bankCode;
        int  branchCode;
        int  accountNumber;
        bool validAccNumber;

    public:
        void setName(const char n[]);
        void setAccountNumber(int bankc, int branchc, int  an);
        void display() const;
        bool isValid() const;
    };
}


#endif /* AccountNumber_hpp */

OUTPUT:

    Bank account app
===================

Please enter your name: John
please enter your bank account, branch code, and account number as follows:
999 999 99999: 1 123 12345
000010John does not have a valid account number.
Invalid account number, (999 999 99999), try again: 1234 123 12345
000010John does not have a valid account number.
Invalid account number, (999 999 99999), try again: 123 0 12345
000100John does not have a valid account number.
Invalid account number, (999 999 99999), try again: 123 1234 12345
000100John does not have a valid account number.
Invalid account number, (999 999 99999), try again: 123 123 123
000110John does not have a valid account number.
Invalid account number, (999 999 99999), try again: 123 123 123456
000110John does not have a valid account number.
Invalid account number, (999 999 99999), try again: 123 123 12345
000110John does not have a valid account number.
Invalid account number, (999 999 99999), try again: 123 12345
123
000100John does not have a valid account number.
Invalid account number, (999 999 99999), try again: 123 123 12345
000110John does not have a valid account number.
Invalid account number, (999 999 99999), try again: 123 123 
123
000110John does not have a valid account number.
Invalid account number, (999 999 99999), try again: 

Solution

  • This

    else if (an_v >= MIN_ACCNO || an_v <= MAX_ACCNO)
    

    should be

    if (an >= MIN_ACCNO && an <= MAX_ACCNO)