Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: C++ Help (Read 10785 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

C++ Help

I'm new to C++ so cut me some slack.

I have some code here

Code: [Select]
char temp[32]; 

//cut out everything inbetween

sprintf(temp,"%d",st->wDayOfWeek);


I now want to test temp in an if statement, but can't seem to be able to do it.  As I understand it, temp should be a number 0-6, but I can't get the if statement to work.  Do I need to cast, and if so, how do I do that in C++.

Edit: As an appendum to this, how do you evaluate TAGZ in the SDK.

C++ Help

Reply #1
Quote
I have some code here

Code: [Select]
char temp[32]; 

//cut out everything inbetween

sprintf(temp,"%d",st->wDayOfWeek);


I now want to test temp in an if statement, but can't seem to be able to do it.  As I understand it, temp should be a number 0-6, but I can't get the if statement to work.  Do I need to cast, and if so, how do I do that in C++.
[a href="index.php?act=findpost&pid=297669"][{POST_SNAPBACK}][/a]

Is there a reason why you are converting the (presumably) int st->wDayOfWeek to a string?  Strings are very hard to work with in C/C++...

Why not just access wDayOfWeek as an int?
Code: [Select]
if(st->wDayOfWeek >= 1 && st->wDayOfWeek <=5)
{
  // This is a week day
}
else
{
 // Everybody is working for the weekend...
}

C++ Help

Reply #2
I just realised it would be better if I had it output numbers only (and not written days of the week) and then users could format it with TAGZ.

The problem is that some days have six letters (Sunday, Monday, Friday), some have seven (Tuesday) and some have eight (Wednesday, Thursday, Saturday).

However, your post has still be most helpful, and I wil take note.  BTW, I had noticed reading through MSDN that strings are an absolute pain in the ass with C++.  VB and Java are so much simpler (better not say that to loud ).

I also still need to know how to evaluate TAGZ.

C++ Help

Reply #3
Another question.

Code: [Select]
static cfg_int

//removed

   cfg_seconds("seconds",0);

//removed

case (EN_CHANGE<<16)|IDC_SECONDS:
   cfg_seconds = uSendMessage(uGetDlgItem(wnd,IDC_IDC_SECONDS),EM_GETLINE,0,0);
   break;

I know it's wrong, and that EM_GETLINE writes to a buffer, but I'm not sure how to set it up properly.

C++ Help

Reply #4
For reference, I solved the problem with this:

Code: [Select]
      
case (EN_CHANGE<<16)|IDC_SECONDS:
   BOOL result;
   if ( true )
   {
       unsigned temp_seconds = GetDlgItemInt(wnd, IDC_SECONDS, &result, FALSE);
       if ( result )
           cfg_seconds = temp_seconds;
   }
break;

C++ Help

Reply #5
Seeing as you find C strings hard to work with, you might want to try the string class (part of the Standard Template Library which is part of the C++ standard). The strings in it are much more like Pascal/Java strings: they auto-resize, you can concatenate them with '+' and do all sorts of other nifty things. #include <string> to use it.

C++ Help

Reply #6
Thank you very much, concatenate strings was something I was trying to figure out how to do earlier.

C++ Help

Reply #7
There is a lighter string8 class, and even lighter string_basic class, both of which are in PFC and used extensively by foobar2000, parts of the SDK, and various components. You may want to consider if this will fill your needs before you pull in larger dependencies like the STL. I'm not sure if they support "blah" + "blah" + "blah", but they certainly support += "blah". Search around the SDK for useful examples.

C++ Help

Reply #8
Yeah, I found string8 which is suitable for most/all my needs.

C++ Help

Reply #9
I can't find basic_string, but anyway... I have another question.  How do I compare two string8's.  == doesn't seem to work.

What I really want to do is use a not operator.

Code: [Select]
string8 val1 = info.meta_get("LAST_PLAYED",0);
string8 val2 = info.meta_get("FIRST_PLAYED",0);

if ( val1 != val2 )
{
   //blah
}

C++ Help

Reply #10
Solved above with:

Code: [Select]
  
if ( cfg_write_play_stamp == 1 )
   {
       if (info.meta_get_count_by_name("FIRST_PLAYED") > 0)
       {
           string8 val1 = info.meta_get("LAST_PLAYED",0);
           string8 val2 = info.meta_get("FIRST_PLAYED",0);
           if ( stricmp_utf8(val1,val2) )
           {
               if ( !val1.is_empty() )
                   info.meta_add("PLAY STAMP",val1);
           }
       }
   }

C++ Help

Reply #11
Just a little input that might help... C-style strings 101.

C-style strings like you were originally having problems with are not really "strings" as such. They're arrays of characters. By convention, the "string" is null-terminated, meaning that the last byte in it is a NULL, or '\0' if you prefer.

char temp[32];
This defines an array of 32 chars. If you were to do:
sprintf(temp,"test");
You'd get values like this:
temp[0] = 't';
temp[1] = 'e';
temp[2] = 's';
temp[3] = 't';
temp[4] = '\0';

For handling these style of strings, look into the "str*" functions.
strcat - Concatenates a couple of strings.
strcpy - Copies one string to another.
strcmp - Compares two strings.
And so on... There's variations on these, like stricmp which is case insensitive, and strncpy which copies only a number of characters and so on. There's a lot of them, and looking into some documentation would be a good idea.

Note that C-style strings are prone to buffer overflows, because there's no bounds checking. That temp variable is 31 characters long, plus a character for the NULL terminate. If you tried to put something in longer than that, it would overwrite the end of it and basically overwrite stuff on the stack. Meaning that if you use these, you need to be very careful and be sure that you don't do that. Usually this means more complicated code.

In C++, most people prefer to use an actual string class, which does it's own bounds checking, can resize as needed, and so forth. std::string is good for this sort of thing. Just #include <string> and then declare your variable like this:
string temp;

string is actually a basic_string in disguise. If you want to be able to handle stuff like Unicode and such, then it's worth the effort to look into other string classes designed with this stuff in mind.

Old C-Style strings still come in handy every once in a while for quick coding because they're actually pretty easy to use, and they're incredibly fast by comparison to most string classes. But in production level code, generally they should be avoided.

C++ Help

Reply #12
Thanks Otto, that is unbelievably helpful.

So basically there are no strings (without including additional classes), just arrays of char's.  Despite the fact that this was staring me right in the face (can't count the number of times I've seen char blah[32] ), my brain just didn't figure that out on it's own.  It actually makes a lot of sense now that you've stated it plainly.

Edit: Some things are so backwards in C++, whilst others seem to be fairly progressive (compared to VB6/Java anyway).  Quite an interesting (and absurd at times) language.

C++ Help

Reply #13
Quote
Thanks Otto, that is unbelievably helpful.

So basically there are no strings (without including additional classes), just arrays of char's.  Despite the fact that this was staring me right in the face (can't count the number of times I've seen char blah[32] ), my brain just didn't figure that out on it's own.  It actually makes a lot of sense now that you've stated it plainly.

Edit: Some things are so backwards in C++, whilst others seem to be fairly progressive (compared to VB6/Java anyway).  Quite an interesting (and absurd at times) language.
[a href="index.php?act=findpost&pid=298051"][{POST_SNAPBACK}][/a]

Just a minor nitpick, but C++ does have real strings. That's what std::string is. The array of chars thing is a C thing, not a C++ thing. In other words, "char blah[32]" is C code, not C++ code.

C++ is actually quite non-absurd, if you stick to pure C++. But the fact is that you have all this original C stuff underlying it all, and when you mix the two of them, it seems more absurd than it really is. And you almost have to mix them to get anything useful done.

Trying to learn C++ without knowing what's original C and what is added on by C++ could be somewhat difficult... Might start with C instead, and work up to C++. Or at least keep in mind that "classes" are entirely a C++ invention along with other things like templates and so forth.

C++ Help

Reply #14
I see.  Indeed I have a lot to learn.  One day...

Edit:  Have you looked at the D Programming Language.  Very interesting stuff.

C++ Help

Reply #15
Another question.

In tags, how do I specify a new line.  For example say I had two pieces of information, A and B.  A is currently store in a tag, and I want to store B on a new line in the same tag.


Code: [Select]
string8 A = info.meta_get("PLAY_STAMP",0);
string8 B = "blah2";
A += B;


Obviously I need to add some sort of UTF-8 Line Feed character between A and B.  How do I do this?

C++ Help

Reply #16
I tried using:
Code: [Select]
char val4[2] = "\n";
as the character in between, but that does not work.

Edit:
This works.
Code: [Select]
char val4[4] = "\r\n";

C++ Help

Reply #17
Ahh..the joys of C++. Can't call myself an expert, only have a year or two of experience from class, but I'll try to help as well

C++ Help

Reply #18
Quote
I tried using:
Code: [Select]
char val4[2] = "\n";
as the character in between, but that does not work.

Edit:
This works.
Code: [Select]
char val4[4] = "\r\n";

[a href="index.php?act=findpost&pid=298238"][{POST_SNAPBACK}][/a]

IMHO \n and \r count as one character, so you don't need a doubled length array to contain them.

e.g.
Code: [Select]
char a = '\n';
char b = '\r';
Life is Real...
(But not in audio :) )

C++ Help

Reply #19
Quote
Ahh..the joys of C++. Can't call myself an expert, only have a year or two of experience from class, but I'll try to help as well
[a href="index.php?act=findpost&pid=298296"][{POST_SNAPBACK}][/a]

Thanks.  Until I started modifying foo_playcount, I had aboslutely no experience in C++.  Now I still no basically nothing, but I'm learning.  Have written lots of VB6 in the past (which was a bad idea, and I picked up a lot of bad habits).  In the last year I've learnt the basics of Java, but haven't used it extensively (I haven't written any large apps anyway).

C++ Help

Reply #20
Quote
IMHO \n and \r count as one character, so you don't need a doubled length array to contain them.[a href="index.php?act=findpost&pid=298301"][{POST_SNAPBACK}][/a]
That's two characters, so the char array needs to have 3 elements (for the terminating zero).

Going back to the code snipped that used string8, you could add a (DOS) linebreak to a string like this:
Code: [Select]
string8 A = info.meta_get("PLAY_STAMP",0);
A += "\r\n";
A += "blah";


C++ Help

Reply #22
Quote
That's two characters, so the char array needs to have 3 elements (for the terminating zero)
Yep, I know, I tried to clarify what I meant in the example  I was not really successful, right?
Life is Real...
(But not in audio :) )

C++ Help

Reply #23
Quote
Thanks Otto, that is unbelievably helpful.

So basically there are no strings (without including additional classes), just arrays of char's.  Despite the fact that this was staring me right in the face (can't count the number of times I've seen char blah[32] ), my brain just didn't figure that out on it's own.  It actually makes a lot of sense now that you've stated it plainly.

Edit: Some things are so backwards in C++, whilst others seem to be fairly progressive (compared to VB6/Java anyway).  Quite an interesting (and absurd at times) language.
[a href="index.php?act=findpost&pid=298051"][{POST_SNAPBACK}][/a]


I had the same problems with these strings. I had to interface C++ code with VB6 components with VB style strings, and as 'strings' is a term independent of implementation it gets a bit confusing. Thanks for adding clarity to this thread, Otto.

C++ Help

Reply #24
i'm sorry,,,i can't the class of string8, can everybaby tell me? thx!