'problem in reassigning values in a class in c++ [closed]

so i'm trying to build a c++ terminal app which represents a bank. now the way im creating accounts is by making a class and making objects out of it, every object i make from this class is an account in the bank and all the objects are stored inside a vector which is called accounts.

this is the class

class New_Account {
    public:
        std::string first_name;
        std::string last_name;
        int id_num;
        int current_balance;
        int pin_num;

    public:
        New_Account() {

            id_num = 0;
            current_balance = 0;
            pin_num = 0;

            std::cout << "default constructor for the new account has been called..." << std::endl;
        };

        New_Account(std::string f_name, std::string l_name, int id, int balance, int pin) {
            first_name = f_name;
            last_name = l_name;
            id_num = id;
            current_balance = balance;
            pin_num = pin;
        }

        void show_info() {
            std::cout << "Full name: " << first_name << " " << last_name << std::endl;
            std::cout << "ID number: " << id_num << std::endl;
            std::cout << "Current Balance: " << current_balance << std::endl;
            std::cout << "Pin number: " << pin_num << std::endl;
        }
};

and an object from this class will look something like this

       int main() {
       
            std::vector <New_Account> accounts;

            std::string f_name;
            std::string l_name;
            int id;
            int balance{ 0 };
            int pin;

            std::cout << "type your first name:  ";
            std::cin >> f_name;

            std::cout << "type your last name:  ";
            std::cin >> l_name;

            std::cout << "type your id number:  ";
            std::cin >> id;

            std::cout << "type the pin you want to use:  ";
            std::cin >> pin;

            New_Account acc(f_name, l_name, id, balance, pin);

            accounts.push_back(acc);
    }  
            

now for example i want to deposit money to an account i will ask the user to type the id of that account and add the amount in the current_balance variable however the original value of current balance doesn't actually change which is my problem, for example reassigning values in python is way easier since most things happen automatically.

this is the function that adds the money

       void deposit(){
            int account_id;
            std::cout << "type the id of the account you want to deposit to:  ";
            std::cin >> account_id;

            for (auto acc : accounts) {

                if (acc.id_num == account_id) {

                    std::cout << "current account balance is " << acc.current_balance << std::endl;

                    double amount;
                    std::cout << "type the amount you want to deposit:  ";
                    std::cin >> amount;

                    std::cout << "\nProcessing...\n" << std::endl;
                    acc.current_balance =  acc.current_balance + amount;

                    std::cout << "account was credited with the amount of " << amount << " succesfully\n" << std::endl;
                    std::cout << "account balance is now " << acc.current_balance << std::endl;

                    break;
                }
            }
}
            

and this is a function that show account info and after you run the deposit function successfully and then you run the show_info function you don't see any changes like we never deposited anything.

void show_info(){
            int id_to_view;
            std::cout << "type the id of the account you want to view:  ";
            std::cin >> id_to_view;

            for (auto acc : accounts) {
                if (acc.id_num == id_to_view) {

                    std::cout << "First name: " << acc.first_name << std::endl;
                    std::cout << "Last name: " << acc.last_name << std::endl;
                    std::cout << "ID number: " << acc.id_num << std::endl;
                    std::cout << "Balance: " << acc.current_balance << std::endl;
                    std::cout << "Pin: " << acc.pin_num << std::endl;

                    break;
                }
            }
}

and the second problem is if i wanted to delete a certain account i couldn't figure out a way to remove that specific account from the accounts vector.

hope that i explained my problem clearly. thanks in advance.

c++


Solution 1:[1]

Here:

for (auto acc : accounts) {

You are making and modifying copies.

You probably wanted

for (auto& acc : accounts) {

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Taekahn