|
|
freakinn00b wrote: Why do I need to initialize the array?
It's not an array. It's simply an uninitialized pointer.
freakinn00b wrote: What if I do not know the size of the array beforehand?
Guess, or use a worst-case scenario (i.e., allocate more than enough).
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
While I realize you said you were doing this for educational purposes, it is also worth noting that you should also learn to reuse code that has already been written and well tested. Your code could simplify to the following:
#include <iostream>
using namespace std;
int main()
{
string theStr = "";
char ch = '\0';
while (ch != '\n')
{
ch = cin.get();
theStr += ch;
}
cout << "You entered a string with length: "
<< theStr.length() << endl;
return 0;
}
Don't get into the habit of reinventing the wheel. There are a lot of buggy programs out there because of developers doing just that without realizing they could have written much less code to accomplish the very same thing.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
After doing some more homework, I found the 'new' keyword.
This allowed me do designate dynamic memory to keep me from having to know the size of the array beforehand.
anyway.. here's the fixed snippet.
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char *pA;
pA = new char ;
int counter = 0;
gets(pA);
while(*pA != '\0')
{
*pA++;
counter++;
}
cout << "\n\n" << counter << " charcters.";
pA=0;
delete pA;
return 0;
}
btw -- why do some header files have an exetensions and some not like iostream. I have to take the ".h" off for vc++ but leave it on in gcc....
|
|
|
|
|
if i want variable "m_Log" will be of type CXlistBox How to do thta so that on member variable
Type i will get the variable type CXlistBox.
any idea
-- modified at 1:19 Thursday 10th August, 2006
|
|
|
|
|
include XlistBox.h in the header file of the dialog where you are using the subclassed list box and then create a variable for your CListBox as you simply do. Go to the header file and change CListBox to CXListBox. That's all.
You can also change the CListBox to CXListBox when you are associating a control variable to it.
Hope I Understood your question correctly
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
priyanka s wrote: How to do thta so that on member variable
Type i will get the variable type CXlistBox.
Do you mean with IsKindOf() ?
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
I want to print data in list box coming from server,
for this i am using this code
<br />
#define LOG \<br />
if (::IsWindow(m_Log.m_hWnd)) \<br />
m_Log.Printf<br />
<br />
void CWartzDlg::Onconnect() <br />
{<br />
CString login= m_arielapi.Login("wwtest2","RD9234","wwtest2");<br />
CString serveradd=m_arielapi.GetServerAddress(login);<br />
<br />
<br />
BOOL bSuccess=FALSE;<br />
<br />
for(int n=0;n<3;n++)<br />
{<br />
if(login)<br />
{<br />
bSuccess=TRUE;<br />
break;<br />
}<br />
}<br />
if(bSuccess)<br />
{<br />
<br />
LOG(0,_T("==>Connected to server[%s]"),login,serveradd);<br />
}<br />
}<br />
<br />
error C2039: 'Printf' : is not a member of 'CString'
This error i am getting,what should i do if dont use the printf,or is there any function in string declartion that can b used to print
thanks for any help from your side,Sometime a small hint will do a great work.
-- modified at 0:40 Thursday 10th August, 2006
|
|
|
|
|
priyanka s wrote: print in list box
m_listbox.AddString();
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
I am continusely getting this error i am just fed up now,do u have any idea how to come out of this,
Any idea any kind of header to b included,and change in declration,I have tried AddString(); but the same error
<br />
:\myproject\wartz\wartzDlg.cpp(313) : error C2039: 'm_hWnd' : is not a member of 'CString'<br />
c:\program files\microsoft visual studio\vc98\mfc\include\afx.h(368) : see declaration of 'CString'<br />
C:\myproject\wartz\wartzDlg.cpp(313) : error C2039: 'Printf' : is not a member of 'CString'<br />
c:\program files\microsoft visual studio\vc98\mfc\include\afx.h(368) : see declaration of 'CString'<br />
XListBox.cpp<br />
Clipboard.cpp<br />
Generating Code...<br />
Error executing cl.exe.<br />
Creating browse info file...<br />
<br />
wartz.exe - 2 error(s), 0 warning(s)<br />
|
|
|
|
|
I think that rather than creating a control variable for the CListBox you have associated a CString variable by the name m_list1 to it.Can you just check for this in the header file of your dialog.
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
thanks your idea has done a good job for me,
|
|
|
|
|
Try this if m_Log is control variable
#define LOG \
if (::IsWindow(m_Log.m_hWnd)) \
m_Log.InsertString // Use Insert String
void CWartzDlg::Onconnect()
{
CString login= m_arielapi.Login("wwtest2","RD9234","wwtest2");
CString serveradd=m_arielapi.GetServerAddress(login);
BOOL bSuccess=FALSE;
for(int n=0;n<3;n++)
{
if(login)
{
bSuccess=TRUE;
break;
}
}
if(bSuccess)
{
CString cstrDispData;
cstrDispData.Format(_T("==>Connected to server[%s]"),serveradd);
LOG(0,cstrDisplayData);
}
}
Regards
Amar
-- modified at 1:52 Thursday 10th August, 2006
|
|
|
|
|
I have no words to say u thank you....its working
By this method i am printing single ,single variable,suppose if i want to print the whole string
that is comming from server ie marketno,tradetype,clientref,price etchow to do that,is there any function for that.
-- modified at 12:18 Thursday 10th August, 2006
|
|
|
|
|
You can use also m_ListBox.InsertString
|
|
|
|
|
I have a list of 60 non-identical Chinese names (MBCS build). I am trying to create a routine that will count how many non-identical names there are. Simple I thought, use the STL set which is a unique list. Thing is, using those 60 Chinese names, the resulting set size is 58. It has discarded 2 names for no reason. OK, maybe a set is not up to the challenge, so I instead used the vector class. A vector of 60 names, sort() and unique() again becomes 58.
Unless anybody can point me in the right direction, I'm going to have to create something of my own.
std::vector< std::string > names(sheet->UsedRows() );
std::set< std::string > list;
short col = -1;
for (int i=0;i<sheet->UsedCols();i++) {
if (head[i].type == NAME)
col = head[i].column;
}
if (col == -1)
return 0;
for (i=xRow;i<sheet->UsedRows();i++) {
names[i] = sheet->Cell(col,i);
list.insert( sheet->Cell(col,i) );
}
int sz = names.size();
sort(names.begin(),names.end() );
names.erase( unique(names.begin(),names.end() ),names.end() );
int foo = list.size();
int bar = names.size();
|
|
|
|
|
I doubt this has anything to do with the STL, or at least, not specific containers. For some reason, the algorithm used to detect what is unique is failing. Perhaps if you wrote your own algorithm and called it using foreach ? Or at least work out which items are disappearing, then step into the code to work out why.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
To go along with that Christian said, start with a smaller set of names (say, 10 known unique names) to test the algorithm you are using (or that unique is using in this case). Also, instead of removing the names, try printing them first to see if the ones being removed are the same ones each time.
As a side note ...
waldermort wrote: std::set< std::string > list;
This is bad practice. Avoid naming variables the same as typenames for standard types.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
I do not understand why this is happening, however the following functions work for small numbers of data, but for larger numbers (ie 100,000 to 1,000,000) I get a stack overflow. These are protected functions of the class "Sample." Sample has a one dimensional dynamically allocated array of doubles pointed to by the m_data variable.
Any help would be appreciated because I have not been able to find a solution to this
Thanks
<br />
void Sample::quicksort(signed long int top, signed long int bottom){<br />
signed int long middle;<br />
if (top < bottom){<br />
middle = partition(top, bottom);<br />
quicksort(top, middle);<br />
quicksort(middle+1, bottom);<br />
}<br />
return;<br />
}<br />
<br />
<br />
signed long int Sample::partition(signed long int top, signed long int bottom){<br />
double x = *(m_data + top);<br />
signed long int i = top - 1;<br />
signed long int j = bottom + 1;<br />
double temp;<br />
do{<br />
do{<br />
j--;<br />
}while (x > *(m_data+j));<br />
<br />
do{<br />
i++;<br />
}while (x < *(m_data+i));<br />
if (i < j){<br />
temp = *(m_data+i);<br />
*(m_data+i) = *(m_data+j);<br />
*(m_data+j) = temp;<br />
}<br />
}while (i < j); <br />
return j;
}<br />
|
|
|
|
|
You have recursive function calls. Every time a recursive function is called, the state of the caller is pushed onto the stack so it can be popped off the stack when the called function returns in order to restore the state. You are simply running out of stack space as a result.
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
Ok. Why would that happen after a sort size of 3000 for instance????
|
|
|
|
|
Any recursive function can cause a stack overflow if it recurses too often.
JKallen wrote: signed int long middle;
Is this legal ? It's certainly redundant. Variables are signed by default, and long and int are the same, so long would have done. Why is it there, this can't be C, it's a class. You've created a variable before it needs to exist ( and when sometimes it does not ), and not given it a default value. This is very poor form.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
It is legal. It is only redundant on a per compiler basis. long signed int specifies for all compilers where some platforms may default to short as the old C++ compilers do,..etc. the definition is fine. in fact I changed the "ints" to short signed unsigned lnog etc,...no difference. The problem is that I am running out of stack,...what i dont get is why this happenes sorting 3000 items.
|
|
|
|
|
JKallen wrote: It is only redundant on a per compiler basis.
Yes, it's true that the standard only sets minimum sizes for variable types, a long may be bigger than an int. But I don't see how 'long' wouldn't work everywhere ?
JKallen wrote: in fact I changed the "ints" to short signed unsigned lnog etc,...no difference
I know that, I was making a general comment about the efficiency and cleanness of the code.
JKallen wrote: The problem is that I am running out of stack,...what i dont get is why this happenes sorting 3000 items.
Obviously because each call is placing two more calls on the stack, and you're running out.
What if you add a counter to the code, a static variable or something ? Perhaps the loop is running for more often than it needs to, and there's a bug in the partition code ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
So. Is the message you cannot sort more than 3000 items using a quick sort algorithm? Is there a way around this limitation? ie is there a way to manage the recursive calls dynamically?
|
|
|
|