|
Hi Anu,
Richard's hit the reason on the head as to why your code doesn't work. If you have this sort of problem again it always pays to see that the string you're doing the comparison with is correct - something as simple as:
::AfxMessageBox( "\\.\DISPLAY1" );
would have shown you something was wrong with the string you're comparing against.
Cheers,
Ash
PS: Three other quick point:
- You probably don't need to use CString::Format to copy the string from the structure, a constructor would have done the job.
- While I don't use CString that often doesn't it have a comparison operator you can use instead of strcmp?
- Instead of using CString why not use the string type that comes with the language? You only need CString when you're interfacing with MFC, the rest of the time std::string is a bit more flexible and with fewer conversion operators is less likely to blow up on you.
|
|
|
|
|
If you wish to compare only specific string parts in C/C++ you could just use strncmp .
Life is a stage and we are all actors!
|
|
|
|
|
Not quite. The strncmp() function lexicographically compares, at most, the first N characters in both strings. That is vastly different from which characters (e.g., middle 5, last 5) to compare.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
Yes you are right but of course, but actually with a bit of simple logic you could compare any substrings using the same function, even the last n symbols for example:
char* a="Demo string";
char *b="Another string";
if(strncmp((char*)(a+strlen(a)-6),(char*)(b+strlen(b)-6),6)==0)
{
printf("Last 6 symbols are equal.\n");
}
else
{
printf("Last 6 symbols aren't equal\n");
}
It's not the best or even recommended practise, but It's possible.
Life is a stage and we are all actors!
|
|
|
|
|
Of course you can, since they are just addresses.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
simple and excellent logic...
Truth Can'nt be changed
|
|
|
|
|
Can someone please give an explanation of the following instruction? const size_t size = A.size();
|
|
|
|
|
The answer depends on what A is, since size is one of its members.
Steve
|
|
|
|
|
The constant size is initialized with the return value of the call to the size method of the A object (probably an instance of a container class).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
computerpublic wrote: A.size();
can you let me know the datatype of A
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
Hey everyone, my problem is I keep getting compile errors and i can not pin point the problem
errors:
2>c:\users\rob\school work\information technology\computer programming with c++\projects\etc\ect_1\countptr.hpp(37) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
2> c:\users\rob\school work\information technology\computer programming with c++\projects\etc\ect_1\countptr.hpp(71) : see reference to class template instantiation 'CountedPtr<T>' being compiled
2>c:\users\rob\school work\information technology\computer programming with c++\projects\etc\ect_1\countptr.hpp(37) : error C2143: syntax error : missing ',' before '&'
2>c:\users\rob\school work\information technology\computer programming with c++\projects\etc\ect_1\countptr.hpp(37) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
2> c:\users\rob\school work\information technology\computer programming with c++\projects\etc\ect_1\refsem1.cpp(11) : see reference to class template instantiation 'CountedPtr<T>' being compiled
2> with
2> [
2> T=int
2> ]
2>c:\users\rob\school work\information technology\computer programming with c++\projects\etc\ect_1\countptr.hpp(37) : error C2143: syntax error : missing ',' before '&'
.hpp file
#ifndef COUNTED_PTR_HPP
#define COUNTED_PTR_HPP
template <typename T>
class CountedPtr
{
private:
T* ptr;
long* count;
public:
explicit CountedPtr(T* p = 0) : ptr(p), count(new long(1))
{
}
CountedPtr(const CountedPtr<T>& p) throw() : ptr(p.ptr), count(p.count)
{
++count;
}
~CountedPtr() throw()
{
release();
}
CountedPtr<T>& operator= (const CounterPtr<T>& p) throw ()
{
if (this != &p)
{
release();
ptr = p.ptr;
count = p.count;
++*count;
}
return *this;
}
T& operator*() const throw ()
{
return *ptr;
}
T* operator->() const throw()
{
return ptr;
}
private:
void release()
{
++*count;
if (*count == 0)
{
delete count;
delete ptr;
}
}
};
#endif //COUNTED_PTR_HPP
.cpp file
#include <iostream>
#include <list>
#include <deque>
#include <algorithm>
#include "countptr.hpp"
using namespace std;
void printCountedPtr(CountedPtr<int> elem)
{
cout << *elem << ' ';
}
int main()
{
typedef CountedPtr<int> IntPtr;
deque<IntPtr> coll1;
list <IntPtr> coll2;
static int values [] = {3,5,9,1,6,4};
for (unsigned i = 0; i < sizeof(values)/sizeof(values[0]); ++i)
{
IntPtr ptr(new int(values[i]));
coll1.push_back(ptr);
coll2.push_front(ptr);
}
for_each(coll1.begin(), coll1.end(), printCountedPtr);
cout << endl;
for_each(coll2.begin(), coll2.end(), printCountedPtr);
cout << endl;
*coll1[2] *= *coll1[2];
(**coll1.begin()) *= -1;
(**coll2.begin()) = 0;
for_each(coll1.begin(), coll1.end(), printCountedPtr);
cout << endl;
for_each(coll2.begin(), coll2.end(), printCountedPtr);
cout << endl;
}
Thanks For all the help,
RobNO
|
|
|
|
|
First, it would have been helpful if you had marked the error locations for us
I counted to line 37 and found this:
CountedPtr<T>& operator= (const CounterPtr<T>& p) throw ()
{
if (this != &p)
{
release();
ptr = p.ptr;
count = p.count;
++*count;
}
return *this;
}
Have a look at the spelling where I've underlined.
Steve
|
|
|
|
|
Thank you very much! Next Time I will try to indicate where the error is.
RobNO.
|
|
|
|
|
You got your message posted while I was still counting lines!
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|
I actually pasted it into an editor which shows line numbers...
Steve
|
|
|
|
|
time to use Notepad++
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
RobNO wrote:
CountedPtr<T>& operator= (const CounterPtr<T>& p) throw ()
{
Spelling the class name consistently would probably help...
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|
|
Watch-out for typos in your program in the operator =, you've used CounterPointer instead of CountedPointer
-Sarath.
Rate the answers and close your posts if it's answered
|
|
|
|
|
Hi All,
I am trying to get a splitter window for MDI when a Menu Item in Menu is clicked. The splitter window is been created but when i am trying to drag that Splitter it is not able to split also it is not performing the events of the pane. (i.e) if in Pane2 of the splitter i am having a Button Control it is not been handled.
Help me out!
Thanks,
Uday.
Here is the Code.
-----------------
void CMainFrame::OnMytreeTree()
{
CCreateContext ctx;
ctx.m_pNewViewClass = RUNTIME_CLASS(CudayView);
// GetActiveDocument() call before you destroy m_splitter.
ctx.m_pCurrentDoc = GetActiveDocument();
//ASSERT(ctx.m_pCurrentDoc!=NULL);
// m_pSplitter !=NULL when there is a visible splitter.
if (m_pSplitter)
{
// Destroy splitter window.
delete m_pSplitter;
m_pSplitter = NULL;
// Create and initialize CMyView.
SetActiveView((CView*)CreateView(&ctx));
GetActiveView()->OnInitialUpdate();
}
else
{
if(m_pSplitter == NULL)
{
//MDIGetActive()->DestroyWindow();
// Create new splitter window.
m_pSplitter = new CSplitterWnd;
if (!m_pSplitter->CreateStatic(this, 1, 2))
{
TRACE0("Can't create splitter window.\n");
return;
}
else
{
if ((!m_pSplitter->CreateView(0, 0,
RUNTIME_CLASS(CMyTreeView),
CSize(200, 0), &ctx))
||(!m_pSplitter->CreateView(0, 1,
RUNTIME_CLASS(CFirstView),
CSize(500, 0), &ctx)))
{
TRACE0("Can't create one of the splitter panes.\n");
return;
}
// Initialize the two panes (each containing a view
// associated with the current document).
((CView*)m_pSplitter->GetPane(0, 0))->OnInitialUpdate();
((CView*)m_pSplitter->GetPane(0, 1))->OnInitialUpdate();
SetActiveView((CView*)m_pSplitter->GetPane(0, 0));
}
}
}
// Redisplay frame.
RecalcLayout();
}
|
|
|
|
|
Hello,
Can anyone tell me why the "index" variable isn't changing in my while loop, despite the fact that the vector "vec" has several "0." values stored in it? My goal is to find the index of each zero value in the vector "vec". The very first value calculated for the "index" variable is correct, but for the following loops in the while statement, it never updates from this value.
Thanks!
vector <long double>::iterator varying_start;
varying_start=vec.begin();
vector <long double>::iterator start;
start=vec.begin();
while(true)
{
varying_start= find(varying_start,vec.end() , 0.);
int index=varying_start-start;
}
|
|
|
|
|
Don't you want to use varying_start= vec.find(varying_start,vec.end() , 0.); instead of varying_start= find(varying_start,vec.end() , 0.); ? I'm not saying this is the answer, but it could be.
|
|
|
|
|
Hello,
If I do that then I get the following error:
eror C2039: 'find' : is not a member of 'std::vector<_Ty>'
|
|
|
|
|
What are the elements of your vector?
|
|
|
|
|
0.000000
0.000000
0.013180
0.424146
1.300951
2.690247
4.658776
6.345779
7.057368
7.186754
6.796806
6.209223
6.240955
6.789929
7.257117
7.541815
7.229695
5.995904
4.311499
2.421349
0.651686
-0.396626
-0.931562
-1.667433
-3.014668
-5.174474
-7.164814
-7.445005
0.000000
|
|
|
|
|
b-rad311 wrote: Can anyone tell me why the "index" variable isn't changing in my while loop, despite the fact that the vector "vec" has several "0." values stored in it?
Because varying_start always "points" to the same starting location. Increment it after the index assignment. You'll also want to provide a way out of the while() loop.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|