|
why r u doing this *strA++; u can simply do strA++;
never say die
|
|
|
|
|
Here's your code that will execute:
#include "stdafx.h"
#include "iostream"
#include "stdlib.h"
#include "stdio.h"
using namespace std;
int my_arrLen( char *strA ){
int loopCtr=0;
while (*strA != '\0'){
cout << *strA;
*strA++;
loopCtr++;
cout << loopCtr;
}
return loopCtr;
}
char *theStr=new char[100];
int main()
{
gets(theStr);
puts("You entered a string with length: ");
cout << my_arrLen(theStr);
}
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
freakinn00b wrote:
char *theStr;
you havn't allocated memory to same!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
Why do I need to initialize the array?
What if I do not know the size of the array beforehand?
|
|
|
|
|
other wise pointer will refer to garbage value
never say die
|
|
|
|
|
otherwise u use getline
never say die
|
|
|
|
|
freakinn00b wrote: o initialize the array?
You need to allocate the memory for the array rather than initializing it.If you don't know what would be the size of the array before hand you can ask the user to input it for you and then allocate that much amount of memory. Keep checking if the user exceeds the bounds and throw an exception etc. if the user does so
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
freakinn00b wrote:
Why do I need to initialize the array?
pointer is just a Mmeory address.. if don't refer to it some valoid mmeory associated with it.. it doesn't work well!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
|
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
|
|
|
|