Iterating through a vector errors

I'm getting weird compiler errors:

line 65: qualified-id in declaration before '(' token
void Account::remove_account(string name)

line 108: error expected '}' at end of input


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
106
107
108
109
#include <iostream>
using namespace std;
#include <sstream>
#include <vector>

class Account {
    public:
        string account_name;
        bool account_type;      //'y' = creidt account = 1, 'n' = basic account = 0
        int account_balance;
        static vector<Account> listAccounts;
        
        //member function declaration
        void init_account(string name, char type);
        void change_balance(string name, int balance);
        void remove_account(string name);
        
};

vector<Account> Account::listAccounts;

//member functions definitions
void Account::init_account(string name, char type) {
    
    Account account;
    
    if (type == 'y') {              //setting the account type  (credit/basic)
        account.account_type = 1;
    } else if (type == 'n') {
        account.account_type = 0;
    }

    account.account_name = name;
    account.account_balance = 0;

    listAccounts.push_back(account);

}

void Account::change_balance(string name, int balance) {
    
    auto iter = listAccounts.begin();
    int n = 0;
    
    for ( ; iter !=  listAccounts.end(); iter++) {
        for (int i = 0; i < listAccounts.size(); ++i) {        //attempt at making a for loop that checks if
             if ((*iter).account_name != name) {               //the number elements in the vector is equal to
                 n ++;                                         //the number element (n) that don't have
             }                                                 //account.name = name
             if (n == listAccounts.size()) {                   //if so, printing "no existing acoounts"
                 cout << "no existing acoounts";                  
             }
        if ((*iter).account_name == name) {
            (*iter).account_balance = balance;
            cout << (*iter).account_balance;
        }
        if (((*iter).account_type == 0) && ((*iter).account_balance < 0)) {
                cout << "account cannot hold negative balance""\n";
                (*iter).account_balance = (((*iter).account_balance) - (balance));
                cout << (*iter).account_balance;
        } 
    }
}

void Account::remove_account(string name) {
    
    auto iter = listAccounts.begin();
    
    for ( ; iter !=  listAccounts.end(); iter++) {
        if ((*iter).account_name == name) {
            listAccounts.erase(iter);
            break;
        }
    }
    
}

//main
int main() {
    
    char action;
    string names;
    char types;
    int balance;
    Account account;
    
    while(action != 'q'){
    //for (int i = 0; i < 11; ++i) {
    
        cin >> action >> names;
    
        if (action == 'c') {
            cin >> types;
            account.init_account(names, types);
        
        }
    
        else if (action == 't') {
            cin >> balance;
            account.change_balance(names, balance);  
        }
    
        else (action == 'r'); {
            account.remove_account(names);
        }
    }
    return 0;
}


I know it's because of the for loop on line: 46 but I'm not sure why
Last edited on
You don't have a closing curly brace for every opening brace. You need to add another } after line 52 to close the inner for loop.
Wow. I'm an idiot.

Thanks!

Another problem i'm getting, on line 46, listAccounts.size() isn't giving me the right size of the vector, is there a way i can count the number of object elements in vector listAccounts?
Last edited on
listAccounts.size() will give you the current number of elements in your listAccounts vector, there is no bug in the standard library. What is your input, what do you expect the size of listAccounts to be after the input, and what is the actual result you're getting?
The inputs are the accounts that the user has created, for example: c josh y, will create a credit account with name josh. The user can create multiple accounts, however when I print listAccounts.size() it always prints 0, no matter how many accounts input to listAccounts vector.
Last edited on
Topic archived. No new replies allowed.