Hi please help! Some problem with my program

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

void updateBankDetails(int a, double b);
void printBankDetails();

struct bankAcct
{
int AccountID;
int Type;
double Balance;
int Term;
string AccountType;
};

bankAcct Acct[20];
int update;
double amount;

int main()
{
for(int i = 0; i < 3; i++)
{
cout << "Please enter your account ID: ";
cin >> Acct[i].AccountID;
cout << "Current = 1, Savings = 2, Fixed Deposit = 3"<<endl;
cout << "Please enter your account Type: ";
cin >> Acct[i].Type;
if(Acct[i].Type == 1)
{
Acct[i].AccountType = " Current";
}
else if(Acct[i].Type == 2)
{
Acct[i].AccountType = " Savings";
}
else if(Acct[i].Type == 3)
{
Acct[i].AccountType = "Fixed D";
}
cout << "Please key in your account Balance: ";
cin >> Acct[i].Balance;
cout << "Please key in your term(Months): ";
cin >> Acct[i].Term;
}
printBankDetails();

cout << "\nEnter your AccountID : ";
cin >> update;
cout << "Enter Your Amount to Update : ";
cin >> amount;
updateBankDetails(update, amount);
cout << "0 = Updated balance is positive! " << endl;
cout << "1 = Updated balance is negative! " << endl;
cout << "-1 = AccountID not found!" << endl;
cout << "\nReturn Value is : " << update;
cin.ignore();
cin.ignore();
}

void printBankDetails()
{
cout << "\nDetails Are as Shown Below" << endl;
cout << "AccID AccType Balance Term" << endl;
cout << "----- ------- ------- ----" << endl;
for(int c = 0; c < 3; c++)
{
cout << Acct[c].AccountID << setw(15) << Acct[c].AccountType << setw(10) << fixed << setprecision(2) << Acct[c].Balance << setw(7) << Acct[c].Term << endl;
}
cin.ignore();

}
void updateBankDetails(int a, double b)
{
int run = 20;
for(int i = 0; i < run; i++)
{
if( a == Acct[i].AccountID )
{
Acct[i].Balance = Acct[i].Balance + b;
if( b > 0 )
{
update = 0;
return;
}
if( b < 0 )
{
update = 1;
return;
}
else if ( a!= Acct[i].AccountID)
{
update = -1;

return;
}
}

}
cin.ignore();
}

__________________________________________________________________

I have problems getting w "else if ( a!= Acct[i].AccountID)"
by right, there should be an output of (-1) but it just display the wrong Account ID i cin!

Please help thanks!

anyway heres the qns!

Write a function, updateBankDetails that updates the balance of a given
account ID. The function takes in two input parameters, namely the account ID
and the amount. The function checks if the account ID is in the table and if it is
found, the balance is updated accordingly. The amount is either added or
subtracted from the existing balance. The function prototype is int
updateBankDetails(int, double). The return value is determined by the
following outcome:

0 - positive
1 - negative
-1 - AccountID not found

Thank you in advance!
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;


struct bankAcct
{
    int AccountID;
    int Type;
    double Balance;
    int Term;
    string AccountType;
};

// return 0 (non-negative), 1 (negative) or -1 (AccountID not found)
// sz == numbrer of accounts in the array
int updateBankDetails( bankAcct Acct[], int sz, int acno, double amount );

// sz == numbrer of accounts in the array
void printBankDetails( const bankAcct Acct[], int sz );


int main()
{
    const int MAXSZ = 20 ;
    bankAcct Acct[MAXSZ] ;

    int sz = 3 ;

    for(int i = 0; i < sz ; ++i )
    {
        cout << "Please enter your account ID: ";
        cin >> Acct[i].AccountID;
        cout << "Current = 1, Savings = 2, Fixed Deposit = 3"<<endl;
        cout << "Please enter your account Type: ";
        cin >> Acct[i].Type;
        if(Acct[i].Type == 1)
        {
            Acct[i].AccountType = " Current";
        }
        else if(Acct[i].Type == 2)
        {
            Acct[i].AccountType = " Savings";
        }
        else if(Acct[i].Type == 3)
        {
            Acct[i].AccountType = "Fixed D";
        }
        cout << "Please key in your account Balance: ";
        cin >> Acct[i].Balance;
        cout << "Please key in your term(Months): ";
        cin >> Acct[i].Term;
    }

    printBankDetails( Acct, sz );

    cout << "\nEnter your AccountID : ";
    int account_number ;
    cin >> account_number ;

    cout << "Enter Your Amount to Update : ";
    double amount ;
    cin >> amount;

    int result = updateBankDetails( Acct, sz, account_number, amount);
    if( result == 0 ) cout << "0 = Updated balance is zero or positive! " << endl;
    else if( result == 1 ) cout << "1 = Updated balance is negative! " << endl;
    else cout << "-1 = AccountID not found!" << endl;
}

// sz == numbrer of accounts in the array
void printBankDetails( const bankAcct Acct[], int sz )
{
    cout << "\nDetails Are as Shown Below" << endl;
    cout << "AccID AccType Balance Term" << endl;
    cout << "----- ------- ------- ----" << endl;
    for( int c = 0; c < sz ; ++c )
    {
        cout << Acct[c].AccountID << setw(15) << Acct[c].AccountType << setw(10) << fixed << setprecision(2) << Acct[c].Balance << setw(7) << Acct[c].Term << endl;
    }
}

// locate and return the posion of the accounrt in the array
// return -1 if not found
// sz == numbrer of accounts in the array
int find_account( const bankAcct Acct[], int sz, int ac_no )
{
    for(int i = 0; i < sz ; ++i )
        if( Acct[i].AccountID == ac_no ) return i ;

    return -1 ; // not found
}

// return 0 (non-negative), 1 (negative) or -1 (AccountID not found)
// sz == numbrer of accounts in the array
int updateBankDetails( bankAcct Acct[], int sz, int acno, double amount )
{
    int pos = find_account( Acct, sz, acno ) ;
    if( pos == -1 ) return -1 ;

    Acct[pos].Balance = Acct[pos].Balance + amount ;

    return Acct[pos].Balance < 0 ? 1 : 0 ;
}

Topic archived. No new replies allowed.