Update a value inside a class using a function
-
I have a c++ code that receives 3 information including the account number, name and deposit (Balance) as input and shows the information on the screen in a table. I have a function (deposit_amount) that is supposed to receive a new deposit and add it to the previous balance. The user has different options by pressing 1 (Making a new account), 2 (adding deposit to the balance), 3 showing the information of the accounts in a table and 4 (Exit the code). emphasized text Everything looks fine in my code in terms of receiving the information and creating accounts by pressing 1. In addition, I can see the information of the accounts in the table by pressing 3. The problem is when I want to add deposit by pressing 2. Although everything looks correct, after entering the amount of deposit, I expect that this amount should be added to the previous balance and when I press 3, I expect to see the updated balance. However, after pressing 3, the previous balance still shows up in the table. My guess is that I am missing something in the deposit_amount function but I was not able to capture it. Could you please help me with this issue to figure out how this could be fixed? Here is my full code:
#include #include #include #include #include using namespace std; class account { int acno; char name\[50\]; int deposit; public: void create\_account(int accno) { acno = accno; cout<<"\\nEnter The Name of The account Holder : "; cin>>name; cout<<"\\nEnter The amount: "; cin>>deposit; cout<<"\\n\\n\\n === Account Created... ==="; } void show\_account() { cout<<"\\nAccount No. : "<
-
I have a c++ code that receives 3 information including the account number, name and deposit (Balance) as input and shows the information on the screen in a table. I have a function (deposit_amount) that is supposed to receive a new deposit and add it to the previous balance. The user has different options by pressing 1 (Making a new account), 2 (adding deposit to the balance), 3 showing the information of the accounts in a table and 4 (Exit the code). emphasized text Everything looks fine in my code in terms of receiving the information and creating accounts by pressing 1. In addition, I can see the information of the accounts in the table by pressing 3. The problem is when I want to add deposit by pressing 2. Although everything looks correct, after entering the amount of deposit, I expect that this amount should be added to the previous balance and when I press 3, I expect to see the updated balance. However, after pressing 3, the previous balance still shows up in the table. My guess is that I am missing something in the deposit_amount function but I was not able to capture it. Could you please help me with this issue to figure out how this could be fixed? Here is my full code:
#include #include #include #include #include using namespace std; class account { int acno; char name\[50\]; int deposit; public: void create\_account(int accno) { acno = accno; cout<<"\\nEnter The Name of The account Holder : "; cin>>name; cout<<"\\nEnter The amount: "; cin>>deposit; cout<<"\\n\\n\\n === Account Created... ==="; } void show\_account() { cout<<"\\nAccount No. : "<
Works for me. Even compiles using g++ -Wall -Wextra with no warnings, so :thumbsup: on that. One issue I do have is that the program cannot handle input for a full name. e.g.
Bill
works fine, butBill Smith
causes the program to loop indefinitely. You may wan to fix that. One suggestion would be to usegetline()
rather thatstd::cin
.Keep Calm and Carry On
-
I have a c++ code that receives 3 information including the account number, name and deposit (Balance) as input and shows the information on the screen in a table. I have a function (deposit_amount) that is supposed to receive a new deposit and add it to the previous balance. The user has different options by pressing 1 (Making a new account), 2 (adding deposit to the balance), 3 showing the information of the accounts in a table and 4 (Exit the code). emphasized text Everything looks fine in my code in terms of receiving the information and creating accounts by pressing 1. In addition, I can see the information of the accounts in the table by pressing 3. The problem is when I want to add deposit by pressing 2. Although everything looks correct, after entering the amount of deposit, I expect that this amount should be added to the previous balance and when I press 3, I expect to see the updated balance. However, after pressing 3, the previous balance still shows up in the table. My guess is that I am missing something in the deposit_amount function but I was not able to capture it. Could you please help me with this issue to figure out how this could be fixed? Here is my full code:
#include #include #include #include #include using namespace std; class account { int acno; char name\[50\]; int deposit; public: void create\_account(int accno) { acno = accno; cout<<"\\nEnter The Name of The account Holder : "; cin>>name; cout<<"\\nEnter The amount: "; cin>>deposit; cout<<"\\n\\n\\n === Account Created... ==="; } void show\_account() { cout<<"\\nAccount No. : "<
In the
deposit_amount
function you should break out of the loop after updating the record. So change it to:while(File.read((char \*) &ac, (int)sizeof(ac))) { if(ac.retacno()==n) { ac.show\_account(); cout<<"\\n\\nEnter The amount to be deposited: "; cin>>amt; ac.dep(amt); int pos = -1 \* (int)sizeof(ac); File.seekp(pos,ios::cur); File.write((char\*)&ac, (int)sizeof(ac)); cout<<"\\n\\n\\t Record Updated"; break; // \*\*\*do not need to read any further records } }
In the
display_all
function change the while statement to:while(!inFile.read((char \*) &ac, sizeof(ac)).eof()) // break if end of file
-
Works for me. Even compiles using g++ -Wall -Wextra with no warnings, so :thumbsup: on that. One issue I do have is that the program cannot handle input for a full name. e.g.
Bill
works fine, butBill Smith
causes the program to loop indefinitely. You may wan to fix that. One suggestion would be to usegetline()
rather thatstd::cin
.Keep Calm and Carry On
Thanks for your feedback. I am running it using vs-code and it is compiled without any issue. If I run it for the first time I pressed 1, then I entered 100 (Account number), Mark (Name) and 200 (Balance). Then, I pressed 3 to see the table that works fine (I can see all information for this account on the table):
=============================================
A/c no. NAME Balance100 Mark 200
Then I pressed 2 to add a deposit and I entered 350. Now I expect this value is added to the previous balance (200) and the new balance should be 550 now. However, after pressing 3, I still see the above table and the balance has not been updated. Do you know where the problem could be? Thanks
-
In the
deposit_amount
function you should break out of the loop after updating the record. So change it to:while(File.read((char \*) &ac, (int)sizeof(ac))) { if(ac.retacno()==n) { ac.show\_account(); cout<<"\\n\\nEnter The amount to be deposited: "; cin>>amt; ac.dep(amt); int pos = -1 \* (int)sizeof(ac); File.seekp(pos,ios::cur); File.write((char\*)&ac, (int)sizeof(ac)); cout<<"\\n\\n\\t Record Updated"; break; // \*\*\*do not need to read any further records } }
In the
display_all
function change the while statement to:while(!inFile.read((char \*) &ac, sizeof(ac)).eof()) // break if end of file
-
I have a c++ code that receives 3 information including the account number, name and deposit (Balance) as input and shows the information on the screen in a table. I have a function (deposit_amount) that is supposed to receive a new deposit and add it to the previous balance. The user has different options by pressing 1 (Making a new account), 2 (adding deposit to the balance), 3 showing the information of the accounts in a table and 4 (Exit the code). emphasized text Everything looks fine in my code in terms of receiving the information and creating accounts by pressing 1. In addition, I can see the information of the accounts in the table by pressing 3. The problem is when I want to add deposit by pressing 2. Although everything looks correct, after entering the amount of deposit, I expect that this amount should be added to the previous balance and when I press 3, I expect to see the updated balance. However, after pressing 3, the previous balance still shows up in the table. My guess is that I am missing something in the deposit_amount function but I was not able to capture it. Could you please help me with this issue to figure out how this could be fixed? Here is my full code:
#include #include #include #include #include using namespace std; class account { int acno; char name\[50\]; int deposit; public: void create\_account(int accno) { acno = accno; cout<<"\\nEnter The Name of The account Holder : "; cin>>name; cout<<"\\nEnter The amount: "; cin>>deposit; cout<<"\\n\\n\\n === Account Created... ==="; } void show\_account() { cout<<"\\nAccount No. : "<
-
Thank you so much for your response. After breaking the while as you suggested, it is working fine now :thumbsup:
You are welcome. But, as k5054 suggested, there are a few areas that cpuld be tidied up. For example I would read all accounts into memory at the beginning of the program, and only write out new or updated entries. That way you will always have the most up to date information available.
-
You are welcome. But, as k5054 suggested, there are a few areas that cpuld be tidied up. For example I would read all accounts into memory at the beginning of the program, and only write out new or updated entries. That way you will always have the most up to date information available.
-
As you're using C++, you might consider using a
std::map
for the internal data structure. e.gstd::map
. If you do that, then you can access an account via its account number, without searching for itstd::map accounts;
accounts[account_no].dep(amount); // add amount to an account;Keep Calm and Carry On