Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
just a problem with a string

 
Post new topic   Reply to topic Printable version
 View previous topic  my first personal project =) Post :: Post asMaze - The greatest asm based maze g...  View next topic  
Author Message
hellzlaker
Registered Cap Buster
Popping men in the ass since Oct 2005


Gender: NEVER ENOUGH!
Joined: Oct 27 2005
Posts: 34
Offline

PostPosted: Sat Dec 09, 2006 11:21 am    Post subject: just a problem with a string Reply to topic Reply with quote

well when i read tut it said strings hold non numerical values so i tried to make a small program but it didnt compile..

Code: Show/Hide
#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <string>
using namespace std;

int main()
{
    SetConsoleTitle("TESTING BETA PROGRAM");
    SetCursorPos(512,512);
   
    string pass;
   
    cout<<"Enter non numeric password ( type quit and press enter to quit): ";
    cin>>pass;
   
    if (pass==hi)
    {
                 cout<<"nice if you want to quit type quit and then press enter";
    }
   
    else if (pass==quit)
    {
         cout<<"closing..";
         return 0;
    }
   
    else
    {
        cout<<"wrong password type quit to quit this program";
    }
   
    return main();
}
         
         
Back to top
View users profile Send private message Add User to Ignore List Send email AIM Address MSN Messenger
Cyan~Fire
I'll count you!
I'll count you!


Age:36
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Sat Dec 09, 2006 11:47 am    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List Visit posters website
hellzlaker
Registered Cap Buster
Popping men in the ass since Oct 2005


Gender: NEVER ENOUGH!
Joined: Oct 27 2005
Posts: 34
Offline

PostPosted: Sat Dec 09, 2006 11:50 am    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List Send email AIM Address MSN Messenger
Purge
Episode I > Eposide III
Jar-Jar is kool


Age:34
Gender:Gender:Male
Joined: Sep 08 2004
Posts: 2018
Offline

PostPosted: Sat Dec 09, 2006 12:48 pm    Post subject: Reply to topic Reply with quote

Since you're comparing strings you should use strcmp.

So it should be:
if (!strcmp("hi", pass))
{
cout << "hi";
}
Back to top
View users profile Send private message Add User to Ignore List
Muskrat
Server Help Squatter


Age:36
Joined: Aug 24 2004
Posts: 829
Location: Swamp
Offline

PostPosted: Sat Dec 09, 2006 12:53 pm    Post subject: Reply to topic Reply with quote

A single(=) defines or assigns, double(==) compares.
Back to top
View users profile Send private message Add User to Ignore List AIM Address
Bak
?ls -s
0 in


Age:24
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Sat Dec 09, 2006 1:08 pm    Post subject: Reply to topic Reply with quote

Purge wrote:
Since you're comparing strings you should use strcmp.


try again purge
_________________
SubSpace Discretion: A Third Generation SubSpace Client
Back to top
View users profile Send private message Add User to Ignore List AIM Address
Mine GO BOOM
Hunch Hunch
What What
Hunch Hunch<br>What What


Age:40
Gender:Gender:Male
Joined: Aug 01 2002
Posts: 3614
Location: Las Vegas
Offline

PostPosted: Sat Dec 09, 2006 1:11 pm    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List Send email
hellzlaker
Registered Cap Buster
Popping men in the ass since Oct 2005


Gender: NEVER ENOUGH!
Joined: Oct 27 2005
Posts: 34
Offline

PostPosted: Sat Dec 09, 2006 5:10 pm    Post subject: Reply to topic Reply with quote

yea it is sorry im just too used to messangers sa_tongue.gif also is there any advantages between
switch

statement and

if (x==0){}

?
Back to top
View users profile Send private message Add User to Ignore List Send email AIM Address MSN Messenger
Purge
Episode I > Eposide III
Jar-Jar is kool


Age:34
Gender:Gender:Male
Joined: Sep 08 2004
Posts: 2018
Offline

PostPosted: Sat Dec 09, 2006 9:04 pm    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List
Mine GO BOOM
Hunch Hunch
What What
Hunch Hunch<br>What What


Age:40
Gender:Gender:Male
Joined: Aug 01 2002
Posts: 3614
Location: Las Vegas
Offline

PostPosted: Sat Dec 09, 2006 9:33 pm    Post subject: Reply to topic Reply with quote

He is using the class called string. strcmp and its ilk work on character arrays. Two different things.
Back to top
View users profile Send private message Add User to Ignore List Send email
Cerium
Server Help Squatter


Age:41
Gender:Gender:Male
Joined: Mar 05 2005
Posts: 807
Location: I will stab you.
Offline

PostPosted: Sat Dec 09, 2006 11:59 pm    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List AIM Address
Bak
?ls -s
0 in


Age:24
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Sun Dec 10, 2006 1:09 am    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List AIM Address
Cyan~Fire
I'll count you!
I'll count you!


Age:36
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Sun Dec 10, 2006 1:41 pm    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List Visit posters website
Cerium
Server Help Squatter


Age:41
Gender:Gender:Male
Joined: Mar 05 2005
Posts: 807
Location: I will stab you.
Offline

PostPosted: Sun Dec 10, 2006 2:28 pm    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List AIM Address
Bak
?ls -s
0 in


Age:24
Gender:Gender:Male
Joined: Jun 11 2004
Posts: 1826
Location: USA
Offline

PostPosted: Sun Dec 10, 2006 4:19 pm    Post subject: Reply to topic Reply with quote

doing &(str[0]) might work
Back to top
View users profile Send private message Add User to Ignore List AIM Address
Cerium
Server Help Squatter


Age:41
Gender:Gender:Male
Joined: Mar 05 2005
Posts: 807
Location: I will stab you.
Offline

PostPosted: Sun Dec 10, 2006 5:04 pm    Post subject: Reply to topic Reply with quote

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
View users profile Send private message Add User to Ignore List AIM Address
SamHughes
Server Help Squatter


Joined: Jun 30 2004
Posts: 251
Location: Greenwich
Offline

PostPosted: Mon Dec 11, 2006 12:21 am    Post subject: Reply to topic Reply with quote

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.

Code: Show/Hide
#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
View users profile Send private message Add User to Ignore List
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> Non-Subspace Related Coding All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 624 page(s) served in previous 5 minutes.

phpBB Created this page in 0.506441 seconds : 42 queries executed (80.1%): GZIP compression disabled