Question

The following program is to get an account number input by the user, using class, which is of a predefined format - first two are alphabets, next three digits are the branch code and the remaining ten digits are some random numbers. If the entered input is not of the format, it should prompt the user to enter again. I tried to implement it as follows. If I give the right input at the first try itself it works fine, but when I check for the else part codes, there is a problem in returning the value of 'acno' to 'tac'. Can somebody rectify the error in the code pls....Thanks....

#include<iostream>
#include<string>
using namespace std;

class saving
{
    string acc_no,tac;
    string br_code;
    public:
    void input();
} sav;

string chkno(string acno)
{
    string cc,bc,no,ano;
    int i,len,a=1,b=1,c=1;
    for(len=0; acno[len]!='\0'; len++);
    if(len!=15)
    {
        cout<<"Account No. is Invalid.Please enter again.";
        cout<<"\nAccount No : "; cin>>ano;
        chkno(ano);
    }
    else
    {
        cc=acno.substr(0,2);  bc=acno.substr(2,3);  no=acno.substr(5,10);
        for(i=0; i<2; i++)
        {
            if(!isalpha(cc[i]))
            {
                a=0; break;
            }
        }
        for(i=0; i<3; i++)
        {
            if(!isdigit(bc[i]))
            {
                b=0;  break;
            }
        }
        for(i=0; i<10; i++)
        {
            if(!isdigit(no[i]))
            {
                c=0;   break;
            }
        }
        if(a==1&&b==1&&c==1)
        {
           cout<<"\nValid - Account no : "<<acno;
            return acno;
        }  
        else
        {
            cout<<"Incorrect format.Please enter again";
            cout<<"\nAccount No : "; cin>>ano;
            chkno(ano);
        }
    }
}

void saving::input() 
{
    cout<<"Account No : ";
    cin>>acc_no;
    tac=chkno(acc_no);
    acc_no=tac;
    cout<<"\nAcc.No :"<<acc_no;
    br_code=acc_no.substr(2,3);
    cout<<"\nBranch Code is :"<<br_code;
}sav;

int main()
{
 sav.input(); return 0;
}
Was it helpful?

Solution

This is wrong

for(len=0; acno[len]!='\0'; len++);

C++ strings are not like C strings, they don't have '\0' terminators. If you want to know how long a C++ string is do this

len = acno.size();

Also when you have an error you call chkno again which is a bit strange but not actually wrong. But what you do do wrong is never return the value from the second call to chkno. What you should do is this

cout<<"Incorrect format.Please enter again";
cout<<"\nAccount No : "; cin>>ano;
return chkno(ano);

See how I've added a return.

For top marks for this exercise I think you should redesign your code. chkno should be a function which just checks the account number (that's what it's called after all). So you should have it return a boolean value, true of false, depending on whether the account number is correct or not. Then you should put the enter/reenter your number loop in main. Like this

bool chkno(string acno)
{
    ...
}

int main()
{
    cout<<"Account No : ";
    cin>>acc_no;
    while (!chkno(acc_no))
    {
        cout<<"Incorrect format.Please enter again";
        cout<<"\nAccount No : ";
        cin>>acc_no;
    }
    ...
}

This is the normal way of writing this kind of task. It's better design because it has a cleaner separation of the two tasks, prompting the user to enter a number and checking if that number is correct.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top