Results 1 to 12 of 12

Thread: Strings SIMPLE !

  1. #1

    Thread Starter
    Hyperactive Member WP's Avatar
    Join Date
    Aug 2000
    Location
    Belgium
    Posts
    278

    Question

    I want to put some text in a string, I did the following:

    Code:
    char part1[1];
    char part2[1];
    char total[1];
    
    int main()
    {
       part1 = "this is";
       part2 = "a test";
       total = part1 + " " + part2 //So total would be 'this is a test'
       return 0;
    }
    But he gives errors on this!
    How could it?

    thanks,

    WP

    Visual Basic 6.0 EE SP5 / .Net
    Windows XP

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    char is a datatype representing one character. A string is an array of characters, you have that bit right, but you need one array element for each character in the string. You need to declare your char arrays to be bigger.

    Secondly, I think you have the wrong idea about how C handles strings. They are quite complicated compared to VB when you use char arrays and not some of the special string classes which simplify them. You can't just concatenate strings using the + operator. You need to use a function to do it, I have forgotten which, but it may be using strcopy().

    part1 written just like that will return a pointer to the first element in the array, the same goes for part2, and similarly the string " " is just a pointer to that string literal within your executable. So you are assigning a pointer plus a pointer plus another pointer to the address of the array total, and you can't do that.

    Also, you forgot a semicolon just before your comment.
    Harry.

    "From one thing, know ten thousand things."

  3. #3

    Thread Starter
    Hyperactive Member WP's Avatar
    Join Date
    Aug 2000
    Location
    Belgium
    Posts
    278

    Cool

    WOW, did you see that! Thanks.

    WP

    Visual Basic 6.0 EE SP5 / .Net
    Windows XP

  4. #4
    Guest
    I recomend using the STL string library....

    Code:
    #include <string>
    
    using namespace std;
    
    //do not include .h for the stl string library
    int main()
    {
    string p1, p2, p3;
    p1 = "this is";
    p2 = "a test";
    p3 = p1 + " " + p2;
    cout << p3;
    return 0;
    }

    there are quite a bit of functions for the string datatype..:

    Code:
    stringname.length(); //returns the length
    stringname.c_str(); //returns the c_style string, with the null char at the end.
    stringname.substr(s,f); //gets a substring from point s with 
    //the length of f
    those are the most common ones..

    and here is the header for the strcpy() function, you only use it for character arrays, or pointers to character arrays(which are essentially the same thing).

    Code:
    char *strcpy( char *strDestination, const char *strSource );

    PS:
    the reason + works with the string datatype, is because it is overloaded, so it can add strings.....

    <edited: I made a little mistake when explaining the substr() function... but it's fixed now>

    [Edited by denniswrenn on 12-21-2000 at 06:09 AM]

  5. #5

    Thread Starter
    Hyperactive Member WP's Avatar
    Join Date
    Aug 2000
    Location
    Belgium
    Posts
    278

    Talking thanks

    Thanks Denniswrenn, it worked!

    WP

    Visual Basic 6.0 EE SP5 / .Net
    Windows XP

  6. #6

    Thread Starter
    Hyperactive Member WP's Avatar
    Join Date
    Aug 2000
    Location
    Belgium
    Posts
    278

    Angry BUT

    How can I output it in a console window?
    cout<<p3; wouldn't work

    WP

    Visual Basic 6.0 EE SP5 / .Net
    Windows XP

  7. #7
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    stringname.c_str(); //returns the c_style string, with the null char at the end.

    So you would use:
    Code:
    cout<<p3.c_str();
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    WP - You can use
    Code:
    cout << string;
    I expect you included <iostream.h>. If you use ANY of the STL then you have to use the STL iostream header, which is in <iostream> (without the .h). Then it'll work.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9

    Thread Starter
    Hyperactive Member WP's Avatar
    Join Date
    Aug 2000
    Location
    Belgium
    Posts
    278

    Exclamation Thanks

    It worked thank you, but I have 2 questions for you.

    1) What's the difference between 'cout' and 'printf' ?
    2) I received this error: 'First-chance exception in Tafels.exe: 0xC0000005: Access Violation.' how handle with this ? (not on your line, but in the begin of the prog)

    WP

    Visual Basic 6.0 EE SP5 / .Net
    Windows XP

  10. #10
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    I think cout is the new way you should use to write to a console and printf was used in C. They alsi have different header files.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  11. #11
    Guest
    printf is in stdio.h, and cout is in iostream.h and I like cout a LOT better......... printf is a little too complicated to use for simple output.....

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    printf came from C. The format string was the only practical non-object-oriented method. In C++ the insertion operators can be used .

    WP - What source line is the error on?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width