|
|
any easy way to scan a string that a user inputs in an editbox control,
for characters and remove them from the string?
charachters like: \ / ? * : " < > |
say i have this function...
void CMyDlg::OnCheck()
{
GetDlgItemText(IDC_EDIT1, str);
//do a scan of the string to see if it contains the characters \ / ? * : " < > |
//remove them from the string
//return the string without the characters
MessageBox("Your string: "+str,"");
}
or any other way to only let the user input only a-z and A-Z characters in the editbox?
thanks.
|
|
|
|
|
you can take advantage of CString.
There is a member function that wil automatically remove the chars or substrings that you pass to it.
I think it's called .Remove("");
You should call:
UpdateData();
editboxstring.Remove("/");
From the MSDN:
CString::Remove
int CString::Remove( TCHAR ch );
Return Value
The count of characters removed from the string. Zero if the string isn't changed.
Parameters
ch
The character to be removed from a string.
Remarks
Call this member function to remove instances of ch from the string. Comparisons for the character are case-sensitive.
Example
//remove the lower-case letter 't' from a sentence:
CString str("This is a test.");
int n = str.Remove('t');
ASSERT(n == 2);
ASSERT(str == "This is a es.");
|
|
|
|
|
thanks, this really helped a lot, it worked like a charm.
|
|
|
|
|
have a look at CString::Remove .
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.
|
|
|
|
|
CString::Remove() will do that, although you'll need to call it once for each character.
|
|
|
|
|
hobbyprogrammer wrote: //do a scan of the string to see if it contains the characters \ / ? * : " < > |
//remove them from the string
//return the string without the characters
Why not just disalloow them to begin with?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
hobbyprogrammer wrote: or any other way to only let the user input only a-z and A-Z characters in the editbox?
You can have look at http://www.codeproject.com/editctrl/enhfocusedit.asp[^] for some ideas on how to filter the input.
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
could anyone please guide me how can i get rid of the warning messges-
warning: format '%s' expects type 'char *', but argument 3 has type 'unsigned char (*)[3]'
warning: pointer targets in passing argument 1 of 'strlen' differ in signedness
---------------------------
struct test
{
unsigned char cont_type[3];
}
hits = sscanf(argv[i], "%s",&(test.cont_type));
printf("\nSending test packets with %s.\n", test.cont_type);
if(strlen(test.cont_type)>3)
------------------
thx
|
|
|
|
|
it seems that you want to store some kind of string inside cont_type .
Holding that hypothesis, I cannot understand why are you passing &(test.cont_type) , instead of (char *)test.cont_type . Note the cast that is needed also in the following statements in order to suppress the warning.
Hope that helps.
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.
|
|
|
|
|
thanks.. I change the data type of cont_type from unsigned char to char...and it works..
|
|
|
|
|
ikbahrian wrote: hits = sscanf(argv[i], "%s",&(test.cont_type));
test.cont_type is already an address, so there is no need to preface it with the address-of operator.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
thx..ya man you are right.
|
|
|
|
|
Thank for most who helped me to find out random fun before..
I am new to programming .. I just writing simple twenty one game..
Now my random out is like that ... k 5 6 q j .. mix char and int together randomly.. it might be suck..! now i want to add evth together to get some value (number) for twenty one game..BUT what i want is to set char with numerial value... like 'k'is 10. for example.. k +5 , the result be 17 etc.. how can set char with numberial value..here is some of my simple code..:lol:
int compcard=3,urcard;//urtotal=0,comptotal=0;
int num;
string randomChar = "kqja";
srand( (unsigned int)time( 0 ) );
cout<<"u want ?"<<endl;
cin="">>urcard;
for ( int i = 0; i < urcard; i++ ) {
if ( rand() < RAND_MAX / 2 )
//in real, ur total .. have to put.. urtotal+=.....
cout << rand() % 10 + 1 << '\n';
else
cout << randomChar[rand() % randomChar.length()] <
|
|
|
|
|
This is not the exact answer that you ask, but...
Why don't you create a vector with the values that have been appearing. this vector should be dinamically modified.
then you could show the values making an easy function that would compare some values and substitute those values for the right letters.
In this way you could work always with numbers and you would separate the data from the visualization.
Hope this helps.
|
|
|
|
|
While not a C++ solution, how about something like:
struct
{
char face;
unsigned int value;
} card[13] = {
{'2', 2},{'3', 3},{'4', 4},
{'5', 5},{'6', 6},{'7', 7},
{'8', 8},{'9', 9},{'t', 10},
{'j', 10},{'q', 10},{'k', 10},
{'a', 11}
};
void main( void )
{
srand(time(NULL));
int x = rand() % 13;
printf("Card %c is worth %u points.\n", card[x].face, card[x].value);
x = rand() % 13;
printf("Card %c is worth %u points.\n", card[x].face, card[x].value);
} You'll need to figure out how to handle the Ace, however.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Thank a lot .. .. It really work .. but when I make adding process .. it not print out the right number .. I dont know why..here is my code ..
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
using namespace std;
struct
{
char face;
unsigned int value;
} card[13] = {
{'2', 2},{'3', 3},{'4', 4},
{'5', 5},{'6', 6},{'7', 7},
{'8', 8},{'9', 9},{'10', 10},
{'j', 10},{'q', 10},{'k', 10},
{'a', 11}
};
int main( void )
{
//int num
urtotal=0;
//srand(time(NULL));
srand( (unsigned int)time( 0 ) );
for (int i=0;i<4;i++)
{
int x = rand() % 13;
printf("Card %c is worth %u points.\n", card[x].face, card[x].value);
card[x].value+=card[x].value;
urtotal=card[x].value;
}
cout<<urtotal;
cin="">>num;
return 0;
}
|
|
|
|
|
davvid wrote: {'8', 8},{'9', 9},{'10', 10},
This should have produced a compiler error.
davvid wrote: urtotal=0;
Is this a global variable?
davvid wrote: urtotal=card[x].value;
You're not accumulating anything here.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Hello all,
I would like to know what do this dump means:
Detected memory leaks!
Dumping objects ->
strcore.cpp(118) : {381557} normal block at 0x018A57F0, 22 bytes long.
Data: < 00:0> 01 00 00 00 09 00 00 00 09 00 00 00 30 30 3A 30
strcore.cpp(118) : {381308} normal block at 0x018A5BB8, 22 bytes long.
Data: < 00:0> 01 00 00 00 09 00 00 00 09 00 00 00 30 30 3A 30
strcore.cpp(118) : {381059} normal block at 0x018A2C80, 22 bytes long.
Data: < 00:0> 01 00 00 00 09 00 00 00 09 00 00 00 30 30 3A 30
strcore.cpp(118) : {380810} normal block at 0x018A42D0, 22 bytes long.
Data: < 02:2> 01 00 00 00 09 00 00 00 09 00 00 00 30 32 3A 32
strcore.cpp(118) : {380560} normal block at 0x018A5028, 22 bytes long.
Data: < 01:1> 01 00 00 00 09 00 00 00 09 00 00 00 30 31 3A 31
strcore.cpp(118) : {376187} normal block at 0x018A5B60, 24 bytes long.
Data: < 0.00> 01 00 00 00 05 00 00 00 0B 00 00 00 30 2E 30 30
strcore.cpp(118) : {376186} normal block at 0x018A5A00, 24 bytes long.
Data: < 0.00> 01 00 00 00 05 00 00 00 0B 00 00 00 30 2E 30 30
strcore.cpp(118) : {376162} normal block at 0x018A59A8, 24 bytes long.
Data: < 0.00> 01 00 00 00 05 00 00 00 0B 00 00 00 30 2E 30 30
strcore.cpp(118) : {376161} normal block at 0x018A5950, 24 bytes long.
Data: < 0.00> 01 00 00 00 05 00 00 00 0B 00 00 00 30 2E 30 30
...
In the VC++6 IDE when I close the app that I'm developing this appears and I would like to know how to interpret this information just in order to be able to solve the related problems.
Thank you in advance.
|
|
|
|
|
Seems you have not released dynamically allocated CStrings. It reports the data inide the strings.
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.
|
|
|
|
|
CStrings... yes, if I'm not wrong, they are released by calling their member function ReleaseBuffer();
I'll have to read again the documentation on the CStrings just in order to clarify when do they deserve to be freed and when do they don't need it.
Thank you.
|
|
|
|
|
Joan Murt wrote: CStrings... yes, if I'm not wrong, they are released by calling their member function ReleaseBuffer();
No, ReleaseBuffer(...) is used after you are done messing with the pointer returned by calling GetBuffer(...) .
CString objects clean themselves up normally upon destruction.
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|
|
Anytime you've done a GetBuffer() or GetBufferSetLength() call on a CString object, you need to have a corresponding ReleaseBuffer() call.
Software Zen: delete this;
|
|
|
|
|
Do you know where do I can get more information on the data that is shown in the debug window? I always think that I must know more on that data in order to be able to repair some problems without having to search for them "manually".
Thank you again.
|
|
|
|
|
IMO the problem is in your allocation of CString objects. i.e. You've done something similar to the code below:
CString * pStr = new CString[20];
and you never deallocated them (CString never creates itself a memory leak, unless you hack its internal buffer...). Thus CString documentation will not help you.
The data reported there should help you to find such strings (for instance you can find some patterns of chars you used to initialize the strings).
Hope that helps
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.
|
|
|
|
|