Server Help

Trash Talk - C++ help needed

Doggeti - Sat Jun 21, 2003 4:39 am
Post subject: C++ help needed
I have following code

Code: Show/Hide
char line[256];
   while(afile.getline(line,256))
   {
      if(!inNotes && &line[0] == "[Notes]")
      {
         inNotes = true;
         sendPublic("[Notes] found");
      }


I know that this reads the file correctly and there is a line with '[Notes]' in the file but it never displays the message. Where is the error?
Smong - Sat Jun 21, 2003 7:01 am
Post subject:
Have you tried strcmp()?
I think == can only be used with string, not char (maybe cast it to string?)
Doggeti - Sat Jun 21, 2003 9:06 am
Post subject:
These str-functions might be very usefull if they weren't so confusing:

Why does this line work:

if(inNotes && strchr(tmp, "["))

and this one cause an error:

a = strchr(tmp, "="); // tmp and a are Strings

icon_question.gif
Smong - Sat Jun 21, 2003 12:35 pm
Post subject:
I find them confusing too like strncmp, what is that!? Also:
char * strchr(const char *_s, int _c);
Mine GO BOOM - Sat Jun 21, 2003 1:21 pm
Post subject:
You really should be using the string class if your doing C++ and have no idea how a char array works. But i'll attempt to give you a low-down of how things are:

Letters are stored in the ASCII format. That means that the letter 'A' is actually seen as the number 65. So any type that can store the number 65 can store the letter 'A'.

int is the standard size of whatever processor your using uses. Its been 32-bit for a while, but with the new 64-bit machines coming into public use soon, int will be 64-bit. So when a function uses int for a single character, it is because thats the standard size the processor likes. You could just have it be the char type, as both will work.

A char* is a pointer (as noted by the *). It points to whatever you want it to. It is NOT a string. It is NOT a new variable. It points to another one. So it works like the following:

Code: Show/Hide
char str[255];
char *p;

strcpy(str, "Hello world");

p = strchr(str, 'w');
if (p)
    p = 'k';

puts(str);

That will print out Hello korld, because the pointer p is pointing directly to where the 'w' was found from strchr. If you change the p = 'k'; into strcpy(p, "friend");, it will print out Hello friend.

strchr just attempts to find a certain character inside of an array of characters. So in the above example, it will go through each letter in the str till it finds a 'w'. Once it does, it returns a pointer to it. If it doesn't find it (say you were looking for a 'z'), it will return 0, which is the same as NULL or false.

strcmp will compare two chars, byte by byte, with each other. If they are identical in everyway, it will return 0. When it returns -1 or 1, it means they are different. As to how, i'll doubt you'll ever use it.

strcmpi will compare like strcmp, but with one difference. It is case insensitve. So that means it will find Hello to be equal to heLlO.

The most common method of finding if two strings are the same inside an if structure is to do !strcmpi(string1, "Text you want"). So for your above example, it would be better to do it as the following: if(!inNotes && !strcmpi(line, "[Notes]"))
Doggeti - Sat Jun 21, 2003 2:03 pm
Post subject:
Thanks for your help, Mine. Now I understand it a little bit better.
Smong - Sat Jun 21, 2003 5:33 pm
Post subject:
Are strncmp(), strcasecmp() and stricmp() the same as strcmpi()?
Mine GO BOOM - Sat Jun 21, 2003 11:35 pm
Post subject:
Smong wrote:
Are strncmp(), strcasecmp() and stricmp() the same as strcmpi()?


MSDN is your friend. Enter the function name, hit Search, and it will send back info on it. If they were the same, they wouldn't be named differently, so yes, they are different. Some do casing differently, some can let your shorten the string comparing length, etc.
Smong - Sun Jun 22, 2003 3:28 pm
Post subject:
Thanks for the tip. strcasecmp() isn't there, but I think it's the same as strcmpi() (I did a bit of testing).
Snidjer - Mon Jun 23, 2003 8:44 pm
Post subject:
And for the people using UNIX, man is your friend!

BTW: Unless you want a ton of security problems in your code, always use the strncmp, strnspn, strncpy, etc versions of the functions. (These are ANSI C functions, by the way). These functions require you to specify a length along with the string (read: character pointer or character array). If you don't use these functions, your code is highly susceptible to buffer overflow (and possibly underrun) attacks.

For instance, instead of the strcpy(str, "Hello World") in MGB's example there, you should use strncpy(str, "Hello World", 11) (the terminating null is implied in the ANSI C version).

Kind regards,

Devon H. O'Dell
sitetronics.com
Helicon - Mon Jun 23, 2003 10:44 pm
Post subject:
Quote:
MSDN is your friend.


MSDN is not your friend.
I had to say that.

Get a book. Fight the empire.
Mine GO BOOM - Tue Jun 24, 2003 12:10 am
Post subject:
Nothing wrong with using a free resource if its good, no matter the source. Plus with books you have to flip pages, ugh. PDF all the way there.
Helicon - Tue Jun 24, 2003 12:41 am
Post subject:
~steal~ the o'reilly books on pdf...?
Snidjer - Tue Jun 24, 2003 2:10 am
Post subject:
In any case, I actually prefer *real books* (gasp) over pdf. There are some things that computers just shouldn't replace, IMHO. That or I just really can't study on a computer icon_wink.gif.
Smong - Tue Jun 24, 2003 3:57 pm
Post subject:
Snidjer wrote:
For instance, instead of the strcpy(str, "Hello World") in MGB's example there, you should use strncpy(str, "Hello World", 11)
Kind regards,

Let me guess, this is just as bad as strcpy()?
strncpy(str, "Hello World", strlen("Hello World"));
Snidjer - Tue Jun 24, 2003 4:28 pm
Post subject:
No, it's better, and in real life, when you're doing strncpy with variables, you will probably be using strlen() on those variables. Good insight.

Devon
Doggeti - Sun Jul 20, 2003 8:20 am
Post subject:
I need help again.

I have:

Code: Show/Hide
char *time; // time is for example '12:34'

int minutes;
int seconds;


How do I get the minutes and seconds from the string (in this case mins=12 and secs=34) and convert them into integers so that the values are in the both integer variables?
Smong - Sun Jul 20, 2003 12:28 pm
Post subject:
sscanf(time, "%d:%d", &minutes, &seconds);
You can try "%2d" if there are always two digits, fx: "00:01", though I doubt it will make much difference.
Doggeti - Sun Jul 20, 2003 4:08 pm
Post subject:
Thx! That works fine.
GameMaster - Fri Aug 15, 2003 6:04 pm
Post subject:
Could someone tell me what diffrences are between C++ and Java????
Cyan~Fire - Fri Aug 15, 2003 6:35 pm
Post subject:
Uhhhh.
C++ is a text-oriented language, and is widely used for almost everything. Complex.

Java is graphic/object-oriented language, and is widely used only on the internet. It has some other uses, but mostly for executables, C++ is the way to go. Java's a heck of a lot similar too. (I can understand it icon_razz.gif)

Please anyone more experienced correct me on any points.
GameMaster - Sat Aug 16, 2003 4:16 am
Post subject:
I looked at some of the code and it looks like it is a mix of java and php.
can it be?
Cyan~Fire - Sat Aug 16, 2003 12:29 pm
Post subject:
Lol no way. C++ is based on C which has been around for ages.

Almost all programming languages look the same, if you think about it. They just have very different functions. php is nothing like C++ since it handles web pages, and C++ does kinda behind-the-scenes stuff. And I already said how Java is different.

If you want to get into programming, I suggest starting with Visual Basic, and working your way up to C++.
MadBoy - Sat Aug 16, 2003 6:05 pm
Post subject:
ok nice to know... ill try
GameMaster - Sat Aug 16, 2003 6:06 pm
Post subject:
php handles php-pags
php its if($s==$b){echo "****";}else{echo $b;}

java... (we learned it @ school) ...is useless (there was no useful programm we have done).
in java its if(s==b){system.out.print(ln)("****");}else{system.out.print(ln)( b);}

They look nearly same... no matter what they are for!

... visual basic????
no thx that dosnt even look same!
I think VB is just 4 MS office access... never saw it snywhere else.

Is is really to hard to learn C++ from mixing php & java????

... im posting shit i know.... lol
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group