|
Well if toxcct is correct and you want the middle character, then you will have to use strlen(), which counts the characters.
char* s = "any string of characters";
int len = strlen(s);
char mc = s[len/2];
This assumes single-byte character coding (like ASCII). Multi-byte character is a whole nother story.
Note: Both std::string and CString both store the length, so getting the length is faster then character counting.
INTP
"The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes."
Andrew W. Troelsen
|
|
|
|
|
I'm not allowed to get the length of the string.
I think this can be done using a Binary Search Tree. and using a recursive function.
Anyhow it was an interview question, in reality all your answers are acceptable otherwise.
|
|
|
|
|
We sill want to know what a Middle Node is, strings do not have nodes. If I was asked that at an interview, that is what I would tell them.
The question does not realy make any since!
Now if the string was not a string but a series text objects (aka. nodes) strung together to represent a string (like a rich-text document), then yes a binary search would probably be best.
Note: All the text in a rich-text document (I believe internaly) is in one node. Until you change the color or font of part of the text, then is must be split up into more than one node. Each of those nodes can be accessed individualy (via code) and the attributs changed.
Oh well! If you know what a Middle Node in a string is please tell us!
INTP
"The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes."
Andrew W. Troelsen
|
|
|
|
|
Hi! All
I dont know if this question is relevant to this forum but I have no other options. I have an MFC application for which I have created an installion using Install Shield. Iam installing the application on a Windows XP System. If I login under an administrator account, the software installs and runs properly. However when I login as any other user who does not have administrator privileges, the software refuses to run. I thought this might be a problem with registration of some of the related dll files and tried to register them manually but I get an error message saying "DllRegisterServer failed Error code 0x80040200". Some of these dlls have been created using ATL and some are the standard Microsoft dlls like Capicom.dll. Can anyone please help . Thanks in advance.
Regards
Pradeep.
|
|
|
|
|
Unfortunately you have no choice. If the registration requires administative privileges then you must install with an account that has such privileges.
After installation you will be able to use the software under the other accounts that do not posess administrative privileges.
Ant.
I'm hard, yet soft. I'm coloured, yet clear. I'm fruity and sweet. I'm jelly, what am I? Muse on it further, I shall return! - David Walliams (Little Britain)
|
|
|
|
|
Hi!
Thanks for the reply. I tried to login as a non-administrator and debug the application source code to see where it is crashing. The application object gets created but the control never reaches InitInstance. It crashes midway showing error in Kernel32.dll
Any ideas?
Thanks
Pradeep.
|
|
|
|
|
Hello,
I have a problem where I want to update two varibales in my View class through a dialog class CModalmodeDlg. When I press the "OK" button in the dialog I want two separate const char * members to be updated.
The first case is when I select a Cstring from a dropdown and sends it to a const char * member (m_Vectorname) in the view class. That works great,but It doesnt work for the secondview member (m_Modefreq). When a try to display m_Modefreq in the view class it gets all corrupted. But it gets it right when I display it in a messagebox through the dialog.
example.
m_Modefreq in messagebox= "45.23 Hz"
m_Modefreq in ViewClass = "^''*´"
Another observation is that when I in the dialogs OnBnClickedOk() set the second variable (m_Modefreq) to always be the same static value it displays it correct in the view.
below is a code snibbet,
[code]
void CModalmodeDlg::OnBnClickedOk()
{
//get the active view
CFrameWnd* wnd = (CFrameWnd*) AfxGetMainWnd();
if(wnd) m_pView = (CVtkSDIView*) wnd->GetActiveView();
//get the selected string from the dropdown.
CString str;
m_DropDown.GetLBText( idx,str);
const TCHAR *ptr;
ptr = str;
//update the first view member const char *m_Vectorname
//works fine
m_pView->m_Vectorname=(LPCTSTR)str;
int idx = m_DropDown2.GetCurSel();
m_pView->m_Modeindex=idx;
//the method ReturnMode returns a string depending on which index is selected
//from the DropDown.
Currentstring =m_pView->ReturnMode(idx);
//Here I want the other const char * member (m_Modefreq) to be updated.
//it prints out the right in a messagebox but as soon as I try to display it in the view //class it gets all corrupted.
m_pView->m_Modefreq=Currentstring.c_str();
[/code]
When I set it to a static value
[code]
like this:
m_pView->m_Modefreq="45.2 Hz" it manage to update it right.
[/code]
I dont really know what to do about this, please letme know if you have any ideas!
Regards Peter
|
|
|
|
|
m_pView->m_Modefreq=Currentstring.c_str();
This is the error. Currentstring is dialog local std::string. You are just doing a shallow copy of its internal pointer. When the dialog ends, the strings destructor destroys its contents.
You need a deep copy.
If you use a std::string or a CString in your view class, it will be ok. Otherwise you'll have to do the memory management yourself with new, strcpy and delete, which also works.
|
|
|
|
|
The problem most likely lies here:
m_pView->m_Modefreq=Currentstring.c_str(); You are storing a pointer to the internal contents of Currentstring into m_pView->m_Modefreq , but this internal contents can change (or be deleted) as soon as Currentstring is modified or destroyed. In your snippet it is not clear whether Currentstring is a local variable or not: if the former, then the char buffer pointed to by m_Modefreq will be destroyed as soon as CModalmodeDlg::OnBnClickedOk exists.
As a general rule of thumb, never store the pointer returned by std::string::c_string . To solve your particular problem, change the type of m_Modefreq to a std::string .
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
|
|
|
|
|
Hi,
Thanks for your quick replys, to bad that I didn't notice until now
I will try both of the tips that has been suggested. Niklas you suggested that I should do a deepcopy, I suppose you mean create a new, strcpy, and then pass it, then delete it.
I realized that the buffer that the m_Modefreq points to will be destroyed but how come the other passing of data works.If you remember from my first thread I converted an CString str and passed it to a const char * m_Vectorname. Shouldn't that pointer also be corrupted as the other one since it points to a buffer that is being destroyed when it exists the metohd?
//get the selected string from the dropdown.
CString str;
m_DropDown.GetLBText( idx,str);
const TCHAR *ptr;
ptr = str;
//update the first view member const char *m_Vectorname
//works fine
m_pView->m_Vectorname=(LPCTSTR)str;
|
|
|
|
|
Shouldn't that pointer also be corrupted as the other one since it points to a buffer that is being destroyed when it exists the metohd?
Yep... seems to me you're just being lucky here; the memory pointed to by m_pView->m_Vectorname is free memory, and sooner or later it'll get corrupted. I suggest you also change m_pView->m_Vectorname to be std::string .
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
|
|
|
|
|
Hi
Friends,
I have come across a problem where i want a presice Timer Intrupt using Windows timer.
i want a resolution of 1 Ms . I am facing a problem that i dont get tick at each time elapse.
Sometimes the response is good ,and sometimes worse.
can any body just pass a few words of comment.
Manish Saha
Whirlpool of India Ltd.
Senior Engineer,EDC
saha_manish@rediffmail.com
|
|
|
|
|
Hi Manish, I recommend you take a look at Nemanja Trifunovic's excellent Timers tutorial[^].
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
|
|
|
|
|
Have Gone through the Timer Tutotorial . That was excelent . Great.
Lot of Thanks to Nemanja Trifunovic . And Cheers to CODE PROJECT !!!
I have gone through the Documnet ,and decided to go for Queue Timers.
Since Win 2000 is ok for me .
What do u think about reply from toxcct?
if the windows Latency of 55 Ms (as toxcct Says) then
is it possible that widows will attend to any of the timer messages?
Lets see .. i will try Out. And let u all Know.
|
|
|
|
|
you cannot be 1ms precise just because Windows scheduler is already up to about 55ms. and windows is not a real-time OS, so, timer interrupts don't have priority uppon the kernel.
TOXCCT >>> GEII power [VisualCalc]
|
|
|
|
|
Hello !
I open a serial port using the CreateFile function. I would like to know the number of bytes that are in the in queue of this port. Of course, witout removing them from the queue...
Any idea of how I can do that ?
Thanks
|
|
|
|
|
You can use the ClearCommError function (even though the name does not suggest it.)
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
|
|
|
|
|
Yes, the name of the function was not really what I was expecting.
Thank you
|
|
|
|
|
I have a CEdit control on a dialog box and I'm creating a dynamic rectangle on the CEdit but it doesn't scroll vertically when I put the rectangle on the bottom of the CEdit?!! I've set the multiline+ vertical scroll parameters
Please help me! my code has come below:
CRect rc, statusRect;
GetDlgItem(IDC_GRAPH_EDIT)->GetWindowRect(rc);
CWnd* pWnd = GetDlgItem(IDC_GRAPH_EDIT);
ScreenToClient(rc);
statusRect.top = rc.bottom - 40;
statusRect.bottom = statusRect.top + 20;
statusRect.left = rc.Width()/2 - 40;
statusRect.right = statusRect.left + 80;
m_dynamic = new CButton;
m_dynamic->Create("Dynamic",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|BS_OWNERDRAW|BS_MULTILINE,statusRect,pWnd,2000);
m_dynamic->SetFont(fontGeorgia);
m_dynamic->SetColor(BLUE, LBLUE);
m_dynamic->ShowWindow(SW_SHOW);
I've tried the following codes but it didn't help:
((CEdit *)(GetDlgItem(IDC_GRAPH_EDIT)))->SetSel(GetWindowTextLength(),-1);
or
((CEdit *)(GetDlgItem(IDC_GRAPH_EDIT)))->PostMessage(WM_VSCROLL, SB_BOTTOM);
UpdateData(TRUE);
or
((CEdit *)(GetDlgItem(IDC_GRAPH_EDIT)))->LineScroll(2);
Any idea or suggestion will be appreciated.
thanks
|
|
|
|
|
CEdit represents a simple text editor, even if you subclassed the control I dought you could do it. The edit-control client area does not actualy scroll, that's an illusion. When you scroll the text it just changes the position of the upper left courner character, where drawing is to start. The edit-control would have to think that your custom-control is part of the text.
If you need to insert an object into an edit type control then you should be using CRichEditCtrl, and even then it may need to be an OLE control.
INTP
"The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes."
Andrew W. Troelsen
|
|
|
|
|
Well.....actually I'm going to draw a graph (some colored rectangles that are connected by some colored arrows) on a dialog. Since the display sould be always like a single vertical line of boxes with a scrolling feature, I though CEdit might be a good choice.
Now.. I'm waiting for your suggestion for such an implementation
Thanks a lot
|
|
|
|
|
If you have not already visited controls section of CP, then give it a shot. There are two graph controls at CP that I know of (one is it ATL/COM section). The other two choice would be to: look into how to use a view (CScrollView) in a dialog box or write your own code to handle the scrolling.
INTP
"The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes."
Andrew W. Troelsen
|
|
|
|
|
;PThrough registry, will I able to know the information from a mounted drive. (ie LBA, PID, VID, drive letter).
|
|
|
|
|
Hi
Read the keys at
HKEY_LOCAL_MACHINE\System\MountedDevices
Regards
The Best Relligion is Science.
Once you understand it, you will know God.
|
|
|
|
|
I encountered a problem today. I have a dialog containing a rich edit control which I use to highlight certain parts of a text. The control is set to readonly in the dialog editor. I also use SetOptions() to set it as readonly in OnInitDialog. This works pretty much ok.
When I select a text snippet, I am able to drag the text and drop it into any other window accepting text. But when I do, the selected text is deleted from the read-only rich edit control.
Any ideas?
|
|
|
|