Server Help

Non-Subspace Related Coding - just a problem with a string

hellzlaker - Sat Dec 09, 2006 11:21 am
Post subject: just a problem with a string
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();
}
         
         

Cyan~Fire - 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.
hellzlaker - 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 "=="
Purge - 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";
}
Muskrat - Sat Dec 09, 2006 12:53 pm
Post subject:
A single(=) defines or assigns, double(==) compares.
Bak - Sat Dec 09, 2006 1:08 pm
Post subject:
Purge wrote:
Since you're comparing strings you should use strcmp.


try again purge
Mine GO BOOM - 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?
hellzlaker - Sat Dec 09, 2006 5:10 pm
Post subject:
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){}

?
Purge - 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?
Mine GO BOOM - 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.
Cerium - 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.
Bak - 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.
Cyan~Fire - 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*.
Cerium - 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.
Bak - Sun Dec 10, 2006 4:19 pm
Post subject:
doing &(str[0]) might work
Cerium - 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.
SamHughes - 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.

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.
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group