0

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: 
a-one
  • 103
  • 11
  • I'd say all those `||` (ORs) should be `&&` (ANDs)? – alk May 27 '16 at 15:34
  • Does this compile? The last `else` in `setAccountNumber()` looks suspicious to me. – alk May 27 '16 at 15:38
  • Just remove all three `else` in `setAccountNumber()`. – alk May 27 '16 at 15:39
  • I updated the code, I had posted the version I was tinkering around with I updated it with the compiled version. I added the && sign the check values if that is the right term show that some are being verified this time and some are not as opposed to all being verified. But it does not accept any value this time. – a-one May 27 '16 at 15:57
  • provided output as well. Thanks for feedback so far! – a-one May 27 '16 at 16:03
  • You should not change your question significantly after comments/answers had been give, as this might render those comments/answers un-understandable. Add updates is the way to go. Please revert the changes you did to your code. – alk May 27 '16 at 16:05
  • updated and reverted. – a-one May 27 '16 at 16:35
  • @alk yea i edited the code to what it was before is there something that you see that is a Problem? – a-one May 27 '16 at 19:25

2 Answers2

1

Your if-statements always evaluate to true. For example, take a look at one of them:

if (bankc >= MIN_BANKCODE || bankc <= MAX_BANKCODE)

In this statement, because you are using the '||' operator, you are basically checking if only one of the boolean sub-expressions is true. However, to validate your numbers, you need both boolean sub-expressions to be true.

Instead, what you probably want is this:

if (bankc >= MIN_BANKCODE && bankc <= MAX_BANKCODE)

Using '&&' ensures that both of your boolean sub-expressions will be checked.

hopia
  • 4,880
  • 7
  • 32
  • 54
0

This

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

should be

if (an >= MIN_ACCNO && an <= MAX_ACCNO)
alk
  • 69,737
  • 10
  • 105
  • 255
  • @a-one: You want to learn how to use a debugger, to solve those issues yourself the next time, won't you? ;-) – alk May 27 '16 at 16:14
  • @a-one: Also please read my last comment to your question, and act accordingly, *at least* when posing the next question. – alk May 27 '16 at 16:15
  • Sure will do.. if you have any recommendations as well? Thanks again! – a-one May 27 '16 at 16:16