|
Hi,
i encounter a similar problem in a combobox embedded edit control. if you managed to overcome this without drawing text through device context, your solution would be highly appreciated.
Thank you!
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
Email: y_becker@netvision.net.il
|
|
|
|
|
Given these requirements for a console application:
Enter a series of integers seperated by whitespace. The first integer represents the quantity of integers to follow.
Ex. 3 5 18 21
or. 5 17 -46 3 198 2
I have been attempting the following to read the integers and store them in a vector:
scanf("%d", &cnt);
for(int i = 1; i <= cnt + 1; i++)
{
scanf("%d", &n);
aInts.push_back(n);
}
This works fine, unless someone enters fewer integers than the initial integer indicates. (ex. 3 19 5)
How can I test to see if the end of the input stream has been reached?
I have attempted the same type of logic using cin, to no avail.
Thank you in advance for your help.
|
|
|
|
|
First of all, scanf ? Are you required by your class to use C instead of C++ for streaming ? If so, why ? You're using STL anyhow.
Secondly:
for(int i = 1; i <= cnt + 1; i++)
why not
i < cnt ???
If you use cin, you can check the stream state to see if there is anything else there. The whole first integer represents the quantity idea is flawed IMO, you should just accept the line, and read the stream while it is good.
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
Apparently some of my last advice was a little off. I've not done any C++ for a while now. The following is a complete solution that reads a line, then it extracts the numbers from the string, it assumes that only numbers are entered ( which is bad ), but it will only read the number of items you entered as the first digit, but if there is less, it just breaks and does not blow up. You could make it present an error if you'd like.
This is obviously your homework, and I ask you not to use this code unless you first do the research to be able to reproduce it yourself, and understand/explain what it does and how it does it.
<br />
<br />
#include <iostream><br />
#include <vector><br />
#include <string><br />
#include <algorithm><br />
<br />
using std::cin;<br />
using std::cout;<br />
using std::vector;<br />
using std::string;<br />
<br />
int _tmain(int argc, _TCHAR* argv[])<br />
{<br />
string s;<br />
int nCount;<br />
int n;<br />
<br />
std::getline(cin, s);<br />
<br />
int nPos = s.find(' ', 0);<br />
<br />
nCount = ::atoi(s.substr(0, nPos).c_str());<br />
<br />
vector<int> vecInt;<br />
<br />
int nStart = nPos + 1;<br />
<br />
for (int i = 0; i < nCount; ++i)<br />
{<br />
nPos = s.find(' ', nStart);<br />
<br />
if (nPos > 0)<br />
{<br />
vecInt.push_back(::atoi(s.substr(nStart, nPos).c_str()));<br />
nStart = nPos + 1;<br />
}<br />
else<br />
{<br />
vecInt.push_back(::atoi(s.substr(nStart, s.size()).c_str()));<br />
break;<br />
}<br />
}<br />
<br />
std::vector<int>::iterator first = vecInt.begin();<br />
std::vector<int>::iterator end = vecInt.end();<br />
<br />
bool bFirst = true;<br />
<br />
for(;first != end; ++first)<br />
{<br />
if (!bFirst) cout << ", ";<br />
cout << *first;<br />
bFirst = false;<br />
}<br />
<br />
cin >> s;<br />
<br />
return 0;<br />
}<br />
<br />
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
Thanks for your reply.
This is actually not a school assignment, though it is very similar to the assignments I was given in C and C++ class. This is a "test" I was given at work. I was able to make it work nicely by reading everything as one long string and parsing it myself but, for reasons that escape me, I was asked to re-do it. The "boss" said I can use any references I like to get this accomplished.
I attempted scanf out of desperation! I could not get cin to work like I wanted it to work.
Thanks for your help.
roger
|
|
|
|
|
Has anybody here worked on LSP? I'm looking in developing an LSP to redirect the TCP calls to a file? Has anybody tried it? any suggestions/sample code? I've already have gone through the sample comes with the Platform SDK feb 2003.
Thanx
mustcat
|
|
|
|
|
almost a homework question!
Don't try it, just do it!
|
|
|
|
|
what exactly do you mean? it's too simple just go ahead and do it maybe for you cuz I'm even stuck on debuging the sample code right now
here is the error you maybe able to help me... I've already downloaded the symbols and also checked the "Load COAF & Export" under Tools-->Options-->Debug tab
Loaded 'C:\WINNT\system32\msvcrt.dll', no matching symbolic information found.
Loaded 'C:\WINNT\system32\rpcrt4.dll', no matching symbolic information found.
Loaded 'C:\WINNT\system32\ws2help.dll', no matching symbolic information found.
Loaded 'C:\WINNT\system32\OLE32.DLL', no matching symbolic information found.
Thanx
BTW as for the redirector, do you have sample code you can share... it's an important homework I would appreciate your help
mustcat
|
|
|
|
|
Hi all,
I have an application that I like to debug but I get the following error
Preloaded symbols may not match 'E:\instlsp.exe'.
Loaded 'C:\WINNT\system32\ntdll.dll', no matching symbolic information found.
Loaded '\\Farjomandi-l\Main\WINNT\system32\NTDLL.DLL', no matching symbolic information found.
Loaded 'C:\WINNT\system32\msvcrt.dll', no matching symbolic information found.
Loaded 'C:\WINNT\system32\KERNEL32.DLL', no matching symbolic information found.
Loaded 'C:\WINNT\system32\ADVAPI32.DLL', no matching symbolic information found.
Loaded 'C:\WINNT\system32\rpcrt4.dll', no matching symbolic information found.
Loaded 'C:\WINNT\system32\ws2help.dll', no matching symbolic information found.
Loaded 'C:\WINNT\system32\USER32.DLL', no matching symbolic information found.
Loaded 'C:\WINNT\system32\GDI32.DLL', no matching symbolic information found.
Loaded 'C:\WINNT\system32\OLE32.DLL', no matching symbolic information found.
The thread 0x608 has exited with code -1 (0xFFFFFFFF).
The program 'C:\RemoteDebug\LSP\Debug\instlsp.exe' has exited with code -1 (0xFFFFFFFF).
how should I setup my project setting to avoid this error and enabling me to debug my app.
thanx
mustcat
|
|
|
|
|
In my program, I allocated a 2D array and it works, the first time the function runs. The debugger seems to blame free() for some reason. The program freezes completely and seems to go into some sort of infinite loop, which is strange because the code is nowhere near an infinite loop. What could possibly be causing this?
-- Steve
|
|
|
|
|
perhaps posting your code would be a start. Why do you think it's in an infinite loop ? How do you deallocate the array ? When do you ?
See how much easier this will be when you post the code ?
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
I am writing an application that open multiple sockets to a server. A number of threads (about 10) are each given a pointer to functions previously resolved from a DLL that handle the sockets.
My code works fine for 1-4 threads, but any more than that and WSAEnumNetworkEvents() returns a socket error. My code is in a DLL and have two global variables SOCKET* pSocket and WSAEVENT* hEvent.
I know that multiple threads cannot safely access the same value, so I made them pointers and each time a thread calls this function the new operator allocates memory to the value. Is this ok?
The code I use for socket connection is show below:
WORD wVersionRequested = MAKEWORD(2,0);
WSADATA wsaData;
int nRet;
nRet = WSAStartup(wVersionRequested, &wsaData);
if(nRet) {
WSACleanup();
return -1;
}
if(wsaData.wVersion != wVersionRequested) {
WSACleanup();
return -1;
}
LPHOSTENT lpHostEntry;
lpHostEntry = gethostbyname(lpServerName);
if(lpHostEntry == NULL) {
WSACleanup();
return -1;
}
SOCKADDR_IN sa;
sa.sin_family = AF_INET;
sa.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
sa.sin_port = htons(nPort);
pSocket = new SOCKET;
*pSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(*pSocket == INVALID_SOCKET) {
WSACleanup();
return -1;
}
hEvent = new WSAEVENT;
*hEvent = WSACreateEvent();
if(*hEvent == WSA_INVALID_EVENT) {
AfxMessageBox("WSACreateEvent()");
closesocket(*pSocket);
WSACleanup();
return -1;
}
nRet = WSAEventSelect(*pSocket, *hEvent, FD_READ|FD_CONNECT|FD_CLOSE);
if(nRet == SOCKET_ERROR) {
AfxMessageBox("EventSelect()");
closesocket(*pSocket);
WSACloseEvent(*hEvent);
WSACleanup();
return -1;
}
nRet = connect(*pSocket, (LPSOCKADDR)&sa, sizeof(SOCKADDR_IN));
if(nRet == SOCKET_ERROR) {
nRet = WSAGetLastError();
if(nRet == WSAEWOULDBLOCK) {
}
else {
closesocket(*pSocket);
WSACloseEvent(*hEvent);
WSACleanup();
return -1;
}
}
WSANETWORKEVENTS events;
while(1) {
DWORD dwRet;
dwRet = WSAWaitForMultipleEvents(1,
hEvent,
FALSE,
5000,
FALSE);
if (dwRet == WSA_WAIT_TIMEOUT) {
AfxMessageBox("Wait timed out1");
closesocket(*pSocket);
WSACloseEvent(*hEvent);
return -1;
}
nRet = WSAEnumNetworkEvents(*pSocket, *hEvent, &events);
if (nRet == SOCKET_ERROR) {
AfxMessageBox("WSAEnumNetworkEvents()");
closesocket(*pSocket);
WSACloseEvent(*hEvent);
return -1;
}
if (events.lNetworkEvents & FD_CLOSE) {
AfxMessageBox("closing...");
closesocket(*pSocket);
WSACloseEvent(*hEvent);
}
if (events.lNetworkEvents & FD_CONNECT) {
if(events.iErrorCode[FD_CONNECT_BIT]!=0)
return -1;
return 0;
}
}
return 0;
There are 10 types of people in the world
Those who understand binary, and those who don't
|
|
|
|
|
I'm trying to write a console app that I can use to feed in a user name and get back all the machines that user is ucrrently logged into by doing a query to a WINS database. But I can't seem to find any documentation whatsoever about WINS. So any links/examples/wisdom would be welcome. Thanks.
|
|
|
|
|
Anyone??? I think it could be done with the Netbios NCBFINDNAME, but am not sure how to use it. Any help whatsoever would be greatly appreciated. Thanks.
|
|
|
|
|
What is the number (not MACRO because I have VC++ 6 so I dought I have that macro defined anywhere) I would pass to GetSysColor() to retreive the color of an non-active menu item like XP displays for menu items which weren't used recently...
Now unless I was dreaming...cuz I just checked my menus in start menu and they no longer had that difference in color for not very active menu items (i must have changed it and not noticed ) these should be a color index for GetSysColor() which I can use
if not or even better still...does there exist a formula I can apply to say...GetSysColor(COLOR_BTNFACE) which will make it lighter by a certain percentage???
This would be ideal...that way my app will still work in 98 Even though i'm thinking a simple GetSysColor() call with incorrect index wouldn't crash my app...I would still feel better about it
Thanks a bunch
Cheers
I'm drinking triples, seeing double and acting single
|
|
|
|
|
Hi ...
Umm, I didn't get time to test it but I threw this together. I think it's what you want. It'll return a colour based on the colour you supply it and the value of dBrighten and bContrast. These double values are to be between 0 and 1 (0% to 100% divided by 100).
Hope this helps,
Mike
#include <math.h>
.
.
COLORREF LightenColour(COLORREF colour, double dBrighten, double dContrast)
{
const int GREY = 0x7f;
const int m_nContrast = 90;
int nStepB = (int)floor(dBrighten * GREY);
double dStepC = dContrast;
int nColourValue;
nColourValue = GetRValue(colour) + nStepB;
nColourValue = (int)floor((nColourValue - GREY) * dStepC) + GREY;
int nR = (nColourValue < 0x00) ? 0x00 : (nColourValue > 0xff) ? 0xff : nColourValue;
nColourValue = GetGValue(colour) + nStepB;
nColourValue = (int)floor((nColourValue - GREY) * dStepC) + GREY;
int nG = (nColourValue < 0x00) ? 0x00 : (nColourValue > 0xff) ? 0xff : nColourValue;
nColourValue = GetBValue(colour) + nStepB;
nColourValue = (int)floor((nColourValue - GREY) * dStepC) + GREY;
int nB = (nColourValue < 0x00) ? 0x00 : (nColourValue > 0xff) ? 0xff : nColourValue;
return RGB(nR, nG, nB);
};
|
|
|
|
|
Coolness...alot more code than I thought it would take, but still cool
Thank again
I'm drinking triples, seeing double and acting single
|
|
|
|
|
I am calling a modal dialog from a dll. I have a button on my main application that when pressed calls a dialog from the dll. Note I am not saying this approach is correct but it is what I have.
void CHTPCCOREDlg::OnButton1()
{
PLUGIN.m_pPlugin->Initialize();
}
void CPlugin::Initialize()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CPlugin dlg;
dlg.DoModal();
}
Which seems to work fine. Then in the dll when I want to return to the main application I have the following code.
void CPlugin::CheckKeyState() // called from a timer
{
if(IsKeyDown(VK_BACK))
{
Cleanup();
CDialog::OnOK();
}
}
All is fine and dandy the first go around. I start the app. I press the button to call the dll. The dll shows it's dialog as expected. I hit the Backspace key and the plugins dialog closes and all is fine. Well, until the press the button to initialize the plugin for a second time. The app just hangs. It appears that the dialog is attempting to display but it never does. I can then press the backspace key again then a press of the button to init the plugin works again.
Any pointers would be appreciated.
|
|
|
|
|
Check AfxSetResourceHandle() function: It may help...
"...Ability to type is not enough to become a Programmer. Unless you type in VB. But then again you have to type really fast..."
Me
|
|
|
|
|
Thanks for the suggestion. I will look at the function you mentioned.
In the mean time I found the problem. I have a function to go full screen.
fs = !fs;
GoFullScreen(this->m_hWnd, fs, &sizerect);
In early stages I had fs defined globally to 0. So the first time thru the function went fullscreen. The second time thru it was being set to not use full screen which I have not tested and which obviously does not work yet.
Thanks for your response.
|
|
|
|
|
Hi everyone!
I need to create a button array that can be resized.
I tought it was easy like in VB but I got a problem. I'm unable to create buttons!
For example, I placed this is my header file :
CButton* myButton;<br />
<br />
int nbButtons;<br />
<br />
void InsertButton(LPCTSTR lpszText);
Then in the implementation file, I've set
void CLinkList::InsertButton(LPCTSTR lpszText)<br />
{<br />
if(nbButtons<1)
myButton=(CButton*)malloc(sizeof(CButton)*(nbButtons+1)); <br />
else
myButton=(CButton*)realloc(myButton, sizeof(CButton)* (nbButtons+1)); <br />
<br />
CRect rect(0,nbButtons*25,60,20);<br />
<br />
myButton[nIndex].Create( lpszText, WS_VISIBLE | WS_CHILD, rect, this, GetDlgCtrlID() );<br />
<br />
nbButtons++;<br />
<br />
}
But I'm not able to create any button. I didn't get any assertion error but it stop within the CButton::Create function. I don't have any idea of what is the problem.
I know that if I create a static array like :
CButton myButton[30];
it works very well...
What's wrong with the way I do, and how can I solve it? Notice that I want my array to be resized because I don't want to hold a "big" static array. And probably because I'm curious to know how it works too!
It will be very appreciated if somebody here could help me!!
Stef
Progamming looks like taking drugs...
I think I did an overdose.
|
|
|
|
|
I just had this problem yesterday (sorta).
Instead I used MFC CArray and the problem I was having was that CButton doesn't have a default copy constructor and I wasn't interested in writting one, so I did what was most obvious
Use CArray like this:
CArray<CButton*, CButton*> m_pButtons;
I'm drinking triples, seeing double and acting single
|
|
|
|
|
Most likely the problem lies in your use of realloc . Simply put, realloc won't work except if the types contained in the array are primitive (integer types, pointers, little more.) The reason is that realloc , when moving to a larger block of memory, copies the original contents as if with memcpy , and this is not guaranteed to work for types which provide their own copy constructors.
My suggestion is that you store pointers to CButton instead of CButton objects. This implies an extra allocation per button:
myButton[nIndex]=new CButton;
myButton[nIndex]->Create(...); and don't forget to delete these objects when done.
Also, do yourself a favor and forget about realloc . C++ provides for instance std::vector which does the work in a far easier way.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
It doesn't work...
It stop at:
myButton[nIndex]=new CButton;
I have declared myButton like this :
CButton** myButton;
Any Idea. I have an headhache so, my brain doesn't work as much as I want !
Progamming looks like taking drugs...
I think I did an overdose.
|
|
|
|
|
I get it!!
I just forgot size the array of CButton pointers...
myButton = new CButton*;<br />
myButton[nIndex] = new CButton;
Now, I just have to find out how to resize it !
Tanks a lot
Stef
Progamming looks like taking drugs...
I think I did an overdose.
|
|
|
|