Click here to Skip to main content
15,881,089 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Get LVN_MARQUEEBEGIN in CListCtrl Pin
Victor Nijegorodov21-Aug-23 21:47
Victor Nijegorodov21-Aug-23 21:47 
AnswerRe: Get LVN_MARQUEEBEGIN in CListCtrl Pin
Richard MacCutchan20-Aug-23 21:51
mveRichard MacCutchan20-Aug-23 21:51 
GeneralRe: Get LVN_MARQUEEBEGIN in CListCtrl Pin
Li Lin 202321-Aug-23 20:45
Li Lin 202321-Aug-23 20:45 
GeneralRe: Get LVN_MARQUEEBEGIN in CListCtrl Pin
Richard MacCutchan21-Aug-23 21:28
mveRichard MacCutchan21-Aug-23 21:28 
AnswerRe: Get LVN_MARQUEEBEGIN in CListCtrl Pin
Randor 22-Aug-23 1:23
professional Randor 22-Aug-23 1:23 
GeneralRe: Get LVN_MARQUEEBEGIN in CListCtrl Pin
Li Lin 202322-Aug-23 14:30
Li Lin 202322-Aug-23 14:30 
GeneralRe: Get LVN_MARQUEEBEGIN in CListCtrl Pin
Randor 23-Aug-23 1:11
professional Randor 23-Aug-23 1:11 
QuestionUpdate a value inside a class using a function Pin
alex136317-Aug-23 4:19
alex136317-Aug-23 4:19 
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:

C++
#include<iostream>
#include<fstream>
#include<ctype.h>
#include<iomanip>
#include<stdio.h>

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. : "<<acno;
        cout<<"\nAccount Holder Name : ";
        cout<<name;
        cout<<"\nBalance amount : "<<deposit;
    }   //function to show data on screen

    void dep(int x){    //function to accept amount and add to balance amount
        deposit+=x;
        cout << deposit;
    }

    int retacno(){  //function to return account number
        return acno;
    }
        void report(){  //function to show data in tabular format
        cout<<acno<<setw(14)<<name<<setw(15)<<deposit<<endl;
    }

};         //class ends here

void write_account();   //function to write record info in a file
void deposit_amount(int); // function to desposit amount for given account
void display_all();     //function to display all account details



int main()
{
    int num, ch;

    do
    {
        cout<<endl;
        cout<<"\n\n\n\tMAIN MENU";
        cout<<"\n\n\t01. NEW ACCOUNT";
        cout<<"\n\n\t02. DEPOSIT AMOUNT";
        cout<<"\n\n\t03. ALL ACCOUNT HOLDER LIST";
        cout<<"\n\n\t04. EXIT";
        cout<<"\n\n\n\tSelect Your Option (1-4) ";
        cin>>ch;
        cout<<endl;

        switch(ch)
        {
        case 1:
            write_account();
            break;

        case 2:
            cout<<"\n\n\tEnter The account No. : ";
            cin>>num;
            deposit_amount(num);
            break;

        case 3:
            display_all();
            break;

        case 4:
            cout<<"\n\n\tThanks";
            break;

        default:
            cout<<"Select Your Option (1-4): ";
            break;
        }

    }while(ch!=4);

    return 0;
}

account ac;

void write_account()
{

    fstream inFile;
    bool flag = true;
    int ac_number;

    inFile.open("info.txt", ios::in);

    cout << "Enter Your Account Number: ";
    cin >> ac_number;

    while(inFile.read((char *) &ac, (int)sizeof(ac)))

        {
        if(ac.retacno() == ac_number)
            flag = false;
    }

    inFile.close();

    fstream outFile;
    outFile.open("info.txt",  ios::out | ios::app);

    if(flag){

        ac.create_account(ac_number);
        outFile.write((char *) &ac, (int)sizeof(ac));
    }else
        cout << "\n\nAccount number exist..." << endl;

    outFile.close();
}

void display_all()
{
    ifstream inFile;
    inFile.open("info.txt");

    cout<<"\n\n\t\tACCOUNT HOLDER LIST\n\n";
    cout<<"=============================================\n";
    cout<<"A/c no.      NAME              Balance\n";
    cout<<"=============================================\n";
    while(inFile.read((char *) &ac, sizeof(ac)))
    {
        ac.report();
    }
    inFile.close();
}

void deposit_amount(int n)
{
    int amt=0;
    fstream File;
    File.open("info.txt", ios::in|ios::out);

    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";
        }
    }
    File.close();
}

AnswerRe: Update a value inside a class using a function Pin
k505417-Aug-23 5:07
mvek505417-Aug-23 5:07 
GeneralRe: Update a value inside a class using a function Pin
alex136317-Aug-23 5:20
alex136317-Aug-23 5:20 
AnswerRe: Update a value inside a class using a function Pin
Richard MacCutchan17-Aug-23 5:19
mveRichard MacCutchan17-Aug-23 5:19 
GeneralRe: Update a value inside a class using a function Pin
alex136317-Aug-23 5:44
alex136317-Aug-23 5:44 
GeneralRe: Update a value inside a class using a function Pin
Richard MacCutchan17-Aug-23 6:01
mveRichard MacCutchan17-Aug-23 6:01 
GeneralRe: Update a value inside a class using a function Pin
alex136317-Aug-23 6:04
alex136317-Aug-23 6:04 
GeneralRe: Update a value inside a class using a function Pin
k505417-Aug-23 6:53
mvek505417-Aug-23 6:53 
Questionpostmessage Pin
utcode9-Aug-23 20:52
utcode9-Aug-23 20:52 
AnswerRe: postmessage Pin
Richard MacCutchan9-Aug-23 21:33
mveRichard MacCutchan9-Aug-23 21:33 
GeneralRe: postmessage Pin
utcode10-Aug-23 18:07
utcode10-Aug-23 18:07 
GeneralRe: postmessage Pin
Richard MacCutchan10-Aug-23 21:22
mveRichard MacCutchan10-Aug-23 21:22 
GeneralRe: postmessage Pin
Victor Nijegorodov10-Aug-23 21:45
Victor Nijegorodov10-Aug-23 21:45 
QuestionWriting console c++ apps... console API or library? Pin
Texas45191-Aug-23 0:04
Texas45191-Aug-23 0:04 
AnswerRe: Writing console c++ apps... console API or library? Pin
Victor Nijegorodov1-Aug-23 0:15
Victor Nijegorodov1-Aug-23 0:15 
AnswerRe: Writing console c++ apps... console API or library? Pin
Richard MacCutchan1-Aug-23 0:31
mveRichard MacCutchan1-Aug-23 0:31 
AnswerRe: Writing console c++ apps... console API or library? Pin
CPallini1-Aug-23 1:02
mveCPallini1-Aug-23 1:02 
AnswerRe: Writing console c++ apps... console API or library? Pin
k50541-Aug-23 5:09
mvek50541-Aug-23 5:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.