| Author | Message | 
	
		| hellzlaker Registered Cap Buster
 Popping men in the ass since Oct 2005
 
 
 Gender: NEVER ENOUGH!
 Joined: Oct 27 2005
 Posts: 34
 Offline
 
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| Cyan~Fire I'll count you!
 
  
 
 Age:37
 Gender:
  Joined: Jul 14 2003
 Posts: 4608
 Location: A Dream
 Offline
 
 | 
			
			  | 
				
					|  Posted: Sat Dec 09, 2006 11:47 am     Post subject: |  |   |  |  
				| 
 |  
				| What are hi and quit? You need double quotes around string literals to tell the compiler you're not talking about a variable. _________________
 This help is informational only. No representation is made or warranty given as to its content. User assumes all risk of use. Cyan~Fire assumes no responsibility for any loss or delay resulting from such use.
 Wise men STILL seek Him.
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| hellzlaker Registered Cap Buster
 Popping men in the ass since Oct 2005
 
 
 Gender: NEVER ENOUGH!
 Joined: Oct 27 2005
 Posts: 34
 Offline
 
 | 
			
			  | 
				
					|  Posted: Sat Dec 09, 2006 11:50 am     Post subject: |  |   |  |  
				| 
 |  
				| also is it suposed to be 
 if (pass="hi")
 {
 cout<<"hi";
 }
 
 because i read tut again and it had only single "=" sign not "=="
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| Purge Episode I > Eposide III
 Jar-Jar is kool
 
  
 Age:36
 Gender:
  Joined: Sep 08 2004
 Posts: 2019
 Offline
 
 | 
			
			  | 
				
					|  Posted: Sat Dec 09, 2006 12:48 pm     Post subject: |  |   |  |  
				| 
 |  
				| Since you're comparing strings you should use strcmp. 
 So it should be:
 if (!strcmp("hi", pass))
 {
 cout << "hi";
 }
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| Muskrat Server Help Squatter
 
  
 Age:38
 Joined: Aug 24 2004
 Posts: 829
 Location: Swamp
 Offline
 
 | 
			
			  | 
				
					|  Posted: Sat Dec 09, 2006 12:53 pm     Post subject: |  |   |  |  
				| 
 |  
				| A single(=) defines or assigns, double(==) compares. |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| Bak ?ls -s
 0 in
 
  
 Age:26
 Gender:
  Joined: Jun 11 2004
 Posts: 1826
 Location: USA
 Offline
 
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| Mine GO BOOM Hunch Hunch
 What What
 
  
 
 Age:42
 Gender:
  Joined: Aug 01 2002
 Posts: 3615
 Location: Las Vegas
 Offline
 
 | 
			
			  | 
				
					|  Posted: Sat Dec 09, 2006 1:11 pm     Post subject: |  |   |  |  
				| 
 |  
				|  	  | Purge wrote: |  	  | Since you're comparing strings you should use strcmp. | 
 string is different than char*, so strcmp should not be used here.
 
 For your program, look into using a loop instead of calling main again and again. Each time you call a function, it uses up more of the stack space. Do it too much, and it can corrupt other memory or crash the application.
 
 Capital letters are very handy when writing to others. I know you can do it, your application has them in their sentences. "tut"?? I'm guessing that is your shorthand for tutorial?
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| hellzlaker Registered Cap Buster
 Popping men in the ass since Oct 2005
 
 
 Gender: NEVER ENOUGH!
 Joined: Oct 27 2005
 Posts: 34
 Offline
 
 | 
			
			  | 
				
					|  Posted: Sat Dec 09, 2006 5:10 pm     Post subject: |  |   |  |  
				| 
 |  
				| yea it is sorry im just too used to messangers  also is there any advantages between switch
 
 statement and
 
 if (x==0){}
 
 ?
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| Purge Episode I > Eposide III
 Jar-Jar is kool
 
  
 Age:36
 Gender:
  Joined: Sep 08 2004
 Posts: 2019
 Offline
 
 | 
			
			  | 
				
					|  Posted: Sat Dec 09, 2006 9:04 pm     Post subject: |  |   |  |  
				| 
 |  
				|  	  | Mine GO BOOM wrote: |  	  | [..] 
 
 string is different than char*, so strcmp should not be used here.
 
 | 
 
 Isn't he using strings here?
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| Mine GO BOOM Hunch Hunch
 What What
 
  
 
 Age:42
 Gender:
  Joined: Aug 01 2002
 Posts: 3615
 Location: Las Vegas
 Offline
 
 | 
			
			  | 
				
					|  Posted: Sat Dec 09, 2006 9:33 pm     Post subject: |  |   |  |  
				| 
 |  
				| He is using the class called string. strcmp and its ilk work on character arrays. Two different things. |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| Cerium Server Help Squatter
 
  
 Age:43
 Gender:
  Joined: Mar 05 2005
 Posts: 807
 Location: I will stab you.
 Offline
 
 | 
			
			  | 
				
					|  Posted: Sat Dec 09, 2006 11:59 pm     Post subject: |  |   |  |  
				| 
 |  
				| You can always just use str[] (or str[0]) for use with strcmp, where "str" is an instance of string. _________________
 There are 7 user(s) ignoring me right now.
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| Bak ?ls -s
 0 in
 
  
 Age:26
 Gender:
  Joined: Jun 11 2004
 Posts: 1826
 Location: USA
 Offline
 
 | 
			
			  | 
				
					|  Posted: Sun Dec 10, 2006 1:09 am     Post subject: |  |   |  |  
				| 
 |  
				|  	  | hellzlaker wrote: |  	  | is there any advantages between switch
 
 statement and
 
 if (x==0){}
 
 ?
 | 
 
 if your if statements are in order like
 case 1:
 case 2:
 case 3:
 
 ect., the compiler can turn it into a jump table, which is faster than doing all the comparisons.
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| Cyan~Fire I'll count you!
 
  
 
 Age:37
 Gender:
  Joined: Jul 14 2003
 Posts: 4608
 Location: A Dream
 Offline
 
 | 
			
			  | 
				
					|  Posted: Sun Dec 10, 2006 1:41 pm     Post subject: |  |   |  |  
				| 
 |  
				|  	  | Cerium wrote: |  	  | You can always just use str[] (or str[0]) for use with strcmp, where "str" is an instance of string. | 
 No, you'd use str.c_str(). str[0] will return a char, not a char*.
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| Cerium Server Help Squatter
 
  
 Age:43
 Gender:
  Joined: Mar 05 2005
 Posts: 807
 Location: I will stab you.
 Offline
 
 | 
			
			  | 
				
					|  Posted: Sun Dec 10, 2006 2:28 pm     Post subject: |  |   |  |  
				| 
 |  
				| I know the function works, but I'm pretty damn sure I used the brackets for the same thing. I'll check some of my old assignments for it. |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| Bak ?ls -s
 0 in
 
  
 Age:26
 Gender:
  Joined: Jun 11 2004
 Posts: 1826
 Location: USA
 Offline
 
 | 
			
			  | 
				
					|  Posted: Sun Dec 10, 2006 4:19 pm     Post subject: |  |   |  |  
				| 
 |  
				| doing &(str[0]) might work |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| Cerium Server Help Squatter
 
  
 Age:43
 Gender:
  Joined: Mar 05 2005
 Posts: 807
 Location: I will stab you.
 Offline
 
 | 
			
			  | 
				
					|  Posted: Sun Dec 10, 2006 5:04 pm     Post subject: |  |   |  |  
				| 
 |  
				| Thats probably what I did. I remember it being kinda hackish, but I also remember not caring because the assignment itself was pretty stupid. |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| SamHughes Server Help Squatter
 
  
 Joined: Jun 30 2004
 Posts: 251
 Location: Greenwich
 Offline
 
 | 
			
			  | 
				
					|  Posted: Mon Dec 11, 2006 12:21 am     Post subject: |  |   |  |  
				| 
 |  
				| It still wouldn't necessarily work.  You'd be assuming that the implementation puts a null terminator at the end of its strings, in its internal representation.  There is no reason to rely on it doing this.  Also, you're assuming that the first word of the std::string structure is a pointer to the data array.  There is no reason you can rely on this behavior. 
 What you did, treating a std::string like a char*, would work just fine in gcc, assuming the string doesn't have null characters in the middle.  The implementation was deliberately designed this way, so that std::strings look like char*s and can be passed around that way.
 
 
 	| #include <stdio.h>
#include <string>
 
 /* prints words from [-delta, delta). */
 void disp_mem(void* p, int delta) {
 
 unsigned long* q = (unsigned long*) p;
 
 int i;
 
 for (i = -delta; i < 0; ++i) {
 printf("%08x ", q[i]);
 }
 puts("");
 for (i = 0; i < delta; ++i) {
 printf("%08x ", q[i]);
 }
 puts("");
 
 return;
 }
 
 void draw_str(std::string& s, const char* msg) {
 printf("-- (%s) --\nDRAWING STRING: \"%s\"\n", msg, s.c_str());
 printf("str ptr: 0x%08x\n", *(unsigned int*) &s);
 printf("Memory at [t-7,t+7):\n");
 disp_mem(*(void**) &s, 7);
 }
 
 int main() {
 
 std::string s("Hel");
 std::string t("Foobar foobar foo bar barf ew");
 
 draw_str(t, "before s = t");
 
 s = t;
 
 printf("std::string size: %d\n", sizeof(std::string));
 
 draw_str(s, "s = t");
 draw_str(t, "t initialized");
 
 s[3] = '@';
 draw_str(s, "s, after s[3] = '@'");
 draw_str(t, "t, after s modified");
 
 s += "Okaaaaay.";
 draw_str(s, "s += Okaaaay.");
 s += s; s += s; s += s; s += s;
 s = "foo";
 draw_str(s, "s 16-upled, s = foo");
 s = "";
 draw_str(s, "s = \"\"");
 s.clear();
 draw_str(s, "s.clear()");
 s.resize(0);
 draw_str(s, "s.resize(0)");
 s.reserve(0);
 draw_str(s, "s.reserve(0)");
 s.reserve(1);
 draw_str(s, "s.reserve(1)");
 }
 
 
 | 
 
 You can see from this code (once you run it and look at it) that std::string objects in gcc are just pointers to character array data stored somewhere.
 
 If you subtract 1 word from that location, you get a reference count.
 If you subtract 2, you get the allocated space.
 If you subtract 3, you get the string's length.
 
 The use of reference counts surprised me.  That means that simple string assignment is an O(1) operation.  This isn't guaranteed across all implementations.  And note that once you make any trivial modification to s, it requires O(n) copying.  Which makes the line, s[3] = '@';, an O(n) operation that one time.  The overall running time is not changed, but it can produce unexpected spurts, if you are not careful.
 
 You can also see that as you grow a string with +=, it grows its allocation space by multiplying by 2.  Which is not surprising at all.
 
 And if you shrink a std::string, it will still continue hogging all the space it's allocated for itself until you explicitly use reserve.
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		|  |