|
Updated my question, added related codes.
|
|
|
|
|
Where is the MFC ON_NOTIFY table that defines the captures? Note also that the notification goes first to the list control's parent window, so you need to capture it there.
|
|
|
|
|
Li Lin 2023 wrote: BOOL CListCtrlEx::OnNotify
You cannot capture it there. You will need to use ON_NOTIFY_REFLECT if you want to capture LVN_MARQUEEBEGIN in your CListCtrl derived class.
Or as Richard pointed out you can capture that message in the parent window.
|
|
|
|
|
|
Good job, happy to see you solved it.
|
|
|
|
|
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<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;
}
void dep(int x){ deposit+=x;
cout << deposit;
}
int retacno(){ return acno;
}
void report(){ cout<<acno<<setw(14)<<name<<setw(15)<<deposit<<endl;
}
};
void write_account(); void deposit_amount(int); void display_all();
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();
}
|
|
|
|
|
Works for me. Even compiles using g++ -Wall -Wextra with no warnings, so on that.
One issue I do have is that the program cannot handle input for a full name. e.g. Bill works fine, but Bill Smith causes the program to loop indefinitely. You may wan to fix that. One suggestion would be to use getline() rather that std::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 Balance
=============================================
100 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; }
}
In the display_all function change the while statement to:
while(!inFile.read((char *) &ac, sizeof(ac)).eof())
|
|
|
|
|
Thank you so much for your response. After breaking the while as you suggested, it is working fine now
|
|
|
|
|
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.
|
|
|
|
|
I will keep it mind for sure!
|
|
|
|
|
As you're using C++, you might consider using a std::map for the internal data structure. e.g std::map<int, account> . If you do that, then you can access an account via its account number, without searching for it
std::map<int, account> accounts;
accounts[account_no].dep(amount);
Keep Calm and Carry On
|
|
|
|
|
Hi,
In MFC, when using sending or posting message to UI in a worker threads, is better to call global ::postmessage or postmessage?. CWnd vs. MFC.
|
|
|
|
|
It does not matter, since CWnd::PostMessage is just a wrapper for the Win32 function.
|
|
|
|
|
within worker thread, could a pointer to CDialog access its member variables or even functions, nothing to do with its UI elements, without using postmessage or sendmessage?. I expect the answer is YES.
|
|
|
|
|
wizQ wrote: I expect the answer is YES. No, the answer is "maybe", as it depends on the way the code is written. Without considerably more detail about your code any answer is guesswork.
|
|
|
|
|
You probably would like to read this great Joe Newcomer's essay about using WorkerThreads
|
|
|
|
|
For those writing console apps which do you think is best?
|
|
|
|
|
"is best" for what?
|
|
|
|
|
Console apps are best for console apps. For other types not so much.
|
|
|
|
|
It really depends on your needs.
I usually just use C++ streams for console I/O .
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
I'm not sure what the difference is in your mind. std::cout and std::format (C++ 20) aren't really API's in my mind. They're fine for a simple utility (think ping or netstat), writing log files or doing data I/O, but quickly become unweildly when trying to do full screen I/O like menus, dialogs, pop-ups etc. If you're pursuing the latter, then you probably want to use a library that handles all the screen painting for you, managing overlays, pull down menus, etc. You might want to look at this page to see if any of them meet your needs C++ Library TUI libraries | LibHunt
You can probably find other examples googling for C++ TUI libraries.
Keep Calm and Carry On
|
|
|
|
|
Long ago I just did it myself.
The libraries that get fancy tended to rely on stuff that I couldn't be sure existed or at least would existed in the future.
Not to mention that a console app should be pretty simple in the first place. If you need complex user interactions then a console app probably isn't the way.
If I didn't want a normal UI then I would be more likely to control it via either command line options and/or configuration files. Actually I have been doing just those for years without any need for anything else.
|
|
|
|
|
Maybe there's a simpler way to do this. Or a good tutorial ?
I have a time string formatted like this (with spaces): "07 h 08 min 51 s"
According to the documentation, should the format string should be "%H h %M min %S s" ?
And if I understand how this should work, I should be able to do something like that, no?
Is there something I am missing ? or just the documentation too obtuse ?
const std::string time("07 h 08 min 51 s");
const std::string format("%H h %M min %S s");
std::chrono::time_point<std::chrono::system_clock> tp;
std::stringstream iss(time);
iss >> std::chrono::parse(format, tp);
CI/CD = Continuous Impediment/Continuous Despair
|
|
|
|