Thread: String question, and hello from a newcomer

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    29

    String question, and hello from a newcomer

    hi there everyone

    i've been a php and vb amateur-programmer for a while, and i've started recently learning C/C++, for my growing interest in game development.

    i'm making a simple chat server, that can be accessible via telnet. i tried for about 2 hours, to find an easy way, to get the first n characters in char variable "buffer3" from the char variabile "buffer4". i was only able to do it this way:

    Code:
    if (SDLNet_TCP_Recv(clnt[j], buffer4, 512)) {
            buffer3[0]=buffer4[0];
            buffer3[1]=buffer4[1];
            buffer3[2]=buffer4[2];
            buffer3[3]=buffer4[3];
            buffer3[4]=buffer4[4];
            buffer3[5]=buffer4[5];
    
          MessageBox(NULL, buffer3, "Message", MB_ICONWARNING);       
    
            }
    it's supposed to get data from a sdl socket, and (for the moment) just display it. isn't there an easier way to do this? like somthing buffer3=substr(buffer4,0,5) ?

    i'm on a windows box, working under dev-c++.

    thanks,
    izuael.

    edit: um, yeah. this is a very n00bish question. i've googled for some help, but they referenced on a header called string.h, and most of the functions gave an error compiling, that's why i am asking
    Last edited by izuael; 10-13-2006 at 03:54 AM.

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    If you use std::string, you can use the = operator.

    Code:
    std::string thing1 = "whatever";
    std::string thing2 = "revetahw"
    
    if ( something ) thing1 = thing2
    don't forget to include <string>, not <string.h>

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    29
    i'm sorry, but i'm really new to c/c++

    what is std:string ?

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Strncpy() does exactly what you want.
    It does something like this:
    Code:
    char *mystrncpy(char* buffer,char* buffer2,int amount){
    	int i;
    	for(i=0;i<amount&&buffer[i]!='\0';i++){
    		buffer2[i]=buffer[i];
    	}
    	buffer2[i]='\0';
    	return buffer2;
    }
    http://www.cppreference.com/cppstring/index.html
    http://www.cprogramming.com/tutorial/string.html
    Last edited by maxorator; 10-13-2006 at 06:34 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    29
    i see, but i want to get only the first 5 characters, strating from position 3 (example). how can i do that?

    also, is there a way to concatenate or copy strings the "usual" way (like other languages work, say vb or php) ? with = and + ?

    thanks

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    also, is there a way to concatenate or copy strings the "usual" way (like other languages work, say vb or php) ? with = and + ?
    Yes, by using std::string objects.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    The "usual" ways are actually very complicated systems, although they seem to be "easy" and "logical".
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    29
    Quote Originally Posted by maxorator
    The "usual" ways are actually very complicated systems, although they seem to be "easy" and "logical".
    uh, thought so. so, um, how are ::string objects used?

    can i also pass ::string variables where a char is required, like in a function?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Actually, I suggest that you take a look at the C++ Book Recommendations thread. Since you already have programming experience, Accelerated C++: Practical Programming by Example is a good book for you to start learning C++.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    29
    i do not want to be rude, and i do appreciate the suggestion, but can you recommend me some online book? i do realise that a hardcover book is better, since i can keep it around my desk, and look up much easier, but it's a real pain where i live (romania) to find books published abroad, and i won't get it for a decent price, either.

    thanks.

  11. #11
    Registered User
    Join Date
    Oct 2005
    Posts
    271

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >isn't there an easier way to do this? like somthing buffer3=substr(buffer4,0,5) ?
    As Laserlight suggested, if you change buffer3 and buffer4 to be a std::string, then you can simply do:
    Code:
    buffer3 = buffer4.substr(0,5);
    And you can copy a char array to a std::string by simply doing:
    Code:
    char array[100];
    std::string buf = array;

  13. #13
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Google is your friend. It's organic, healthy, safe and free of all harmful side effects. Try it today!

    On a more serious note, you should try to appreciate the low-level-ness of C++, as opposed to the PHP (scripted) and vB (managed). Strings are actually nothing more than arrays of characters in RAM, referenced by a memory pointer. Code is fast, but the process of writing the code can be slow and hard, unless you are well grounded in the basics, know all the pitfalls (read the CProg FAQ to have an idea of what to expect) and have a fundemental understanding of how your machine works inside.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Not necessarily appreciate, but at least understand
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed