Expected a declaration error

Sample of my code. What's wrong with my program? Where is the error? Please help. Thank you.

void Acct ();
{
for (int i=0; i<50;i++)

{ if(Table[i].cheque == true)

{
cout << Table[i].account << endl;
cout << Table[i].name << endl;
}

}


}
Last edited on
Remove semicolon
void printChequeAcct ();
Thank you, it worked.

Can I ask how do I call out this function? Is there any problem with code?
I tried running my program but nothing comes out. Just shows Press any key to continue...
Well, I think you need to show the rest of the code where you are calling the function. Of course if the condition tested in the if statement is not true, there would be no output.
Thanks
Last edited on
This can not possibly be your real code because this doesn't even compile.
In function main() this line void printChequeAcct(); is a declaration, it tells the compiler that the function exists. But you need to put this, in order to actually call the function:
 
    printChequeAcct();


There are several other problems. Array acctTable[] is used without having been defined. The loop for (int i=0; i<30;i++) goes from index 0 to 29, but the array Table[] has only 10 elements.

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
#include <iostream>
#include <iomanip>
#include <ctime>
#include <string>
using namespace std;

struct bank
{
    string account;
    string name;
    double balance;
    bool cheque;
};

const int SIZE = 10;

bank Table[SIZE];

void printChequeAcct();

int main()
{
    Table[0].account = "B123";
    Table[0].name = "Elisa";
    Table[0].balance = 7000;
    Table[0].cheque = true;

    printChequeAcct();
}

void printChequeAcct ()
{
    for (int i=0; i<SIZE; i++)
    {
        if (Table[i].cheque)
        {
            cout << Table[i].account << endl;
            cout << Table[i].name << endl;
        }
    }
}


Notice at line 19 the function is declared so the compiler knows what parameters it expects, and what type of value it returns.

Note also at line 15 the use of a defined constant SIZE. That makes it simpler to modify the program if required, as changing the value in just one place will affect everywhere that it is used.
Last edited on
Really thank you so much. Your explanations really helps :)

If you dun mind, can you teach me more on arguments?

for eg. Now I have to create a function called Transfer.

int Transfer(string, string, double);

from account to account and the amount.

How do I go about declaring them?

Please teach me more on functions and arguments. Thank you so much.
Last edited on
I would recommend that you read through the tutorial pages on functions.
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/
Thank you. I will read them now. Thank you so much.
Can I ask for line 35

if (Table[i].cheque)

why do we not include if (Table[i].cheque==true)

??
It is optional. Both ways are correct.

If you have a boolean type such as this bool cheque; its value may be either true or false.

If it is true, the test becomes if (true == true) which is the same as simply if (true)

And similarly for false,
if (false == true) is the same as if (false)
I have a question. Even after reading I cant seem to solve.
Please take a look at my code. How can I traverse and declare so that I can carry out the xferAmount function

Last edited on
You need to match the account number which was entered by the user with the existing accounts in the table. Use a loop to search the table.

You can call function xfer() from main(). This will get the account numbers and amount from the user, before calling the function xferAmount()

I added an extra parameter called "reason" which you may remove if you don't want it.

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
void xfer()
{
    string fromaccount;
    string toaccount;
    double amount;

    cout << "Please enter your Account Number to transfer: ";
    cin >> fromaccount;

    cout << "Please enter the Account Number you wish to transfer to: ";
    cin >> toaccount;

    cout << "Please enter the Amount you wish to transfer: $";
    cin >> amount;


    string reason;

    int result = xferAmount (fromaccount, toaccount, amount, reason);

    if (result != 0)
    {
        cout << "Failed: " << reason;
    }
    else
    {
        cout << "OK: " << reason;
    }

}

int xferAmount (string fromaccount, string toaccount, double amount, string & reason)
{
    if (fromaccount==toaccount)
    {
        reason = "The two accounts have the same account number and "
                 "do not carry out the transfer of funds";
        return 1; // indicate failed.
    }

    int from = -1;
    int to   = -1;

    for (int i=0; i<SIZE; i++)
    {
        if (acctTable[i].account == fromaccount)
            from = i;
        if (acctTable[i].account == toaccount)
            to = i;
    }

    if (from >= 0)
    {
        if (acctTable[from].balance - amount <= 10)
        {
            reason = "The transfer causes the balance of the first account to "
                     "drop below $10 and do not carry out the transfer of funds";
            return 1; // indicate failed
        }
    }

    if ((from >= 0) && (to >= 0))
    {
        acctTable[from].balance -= amount;
        acctTable[to].balance   += amount;
        reason = "Success";
        return 0;  // indicate success
    }

    reason = "Account number not found";
    return 1; // indicate failed
}

Actually, I think a more modular approach would be to have a function like this which simply searches for the account number and returns the index of that account in the table, or -1 if not found.
1
2
3
4
5
6
7
8
9
10
int findAccount(string account)
{
    for (int i=0; i<SIZE; i++)
    {
        if (acctTable[i].account == account)
            return i;  // success
    }

    return -1; // failed
}

Thank you so much Chervil. :)
Topic archived. No new replies allowed.