Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Strings

Strings

Scheduled Pinned Locked Moved The Lounge
questionc++javahelplearning
13 Posts 11 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    marouane miftah el kheir
    wrote on last edited by
    #1

    i just started learning C++ here is the first problem i encountered. how do you declare String data types. like if i want to have some variables like firstName, lastName; how do i store those variables??? i know it is not like java where you can just declare something as a String. thanks in advance. Marouane

    S P M 3 Replies Last reply
    0
    • M marouane miftah el kheir

      i just started learning C++ here is the first problem i encountered. how do you declare String data types. like if i want to have some variables like firstName, lastName; how do i store those variables??? i know it is not like java where you can just declare something as a String. thanks in advance. Marouane

      S Offline
      S Offline
      Simon Walton
      wrote on last edited by
      #2

      Try the c++ forums in the future. But since you asked;

      char varname[x];

      Does the trick for standard c, where x is the number of character you wish to store. It's basically an array of 1 byte characters. If you're using MFC, you can use the CString class, which offers a host of handy string manipulation functions. Simon Nobody does chicken like MFC Sonork ID 100.10024

      1 Reply Last reply
      0
      • M marouane miftah el kheir

        i just started learning C++ here is the first problem i encountered. how do you declare String data types. like if i want to have some variables like firstName, lastName; how do i store those variables??? i know it is not like java where you can just declare something as a String. thanks in advance. Marouane

        P Offline
        P Offline
        Paul Watson
        wrote on last edited by
        #3

        marouane miftah el kheir wrote: i just started learning C++ here is the first problem i encountered. how do you declare String data types. like if i want to have some variables like firstName, lastName; how do i store those variables??? i know it is not like java where you can just declare something as a String. thanks in advance. Can someone please explain to me why even a simple thing like declaring a variable is such a task to do in C++? Do you really think that by making the programmer declare the variable, it's type AND it's length you are "breeding" better programmers? Just curious, as always :) regards, Paul Watson Bluegrass Cape Town, South Africa "The greatest thing you will ever learn is to love, and be loved in return" - Moulin Rouge Martin Marvinski wrote: Unfortunatly Deep Throat isn't my cup of tea Do you Sonork? I do! 100.9903 Stormfront

        C L T C M 5 Replies Last reply
        0
        • P Paul Watson

          marouane miftah el kheir wrote: i just started learning C++ here is the first problem i encountered. how do you declare String data types. like if i want to have some variables like firstName, lastName; how do i store those variables??? i know it is not like java where you can just declare something as a String. thanks in advance. Can someone please explain to me why even a simple thing like declaring a variable is such a task to do in C++? Do you really think that by making the programmer declare the variable, it's type AND it's length you are "breeding" better programmers? Just curious, as always :) regards, Paul Watson Bluegrass Cape Town, South Africa "The greatest thing you will ever learn is to love, and be loved in return" - Moulin Rouge Martin Marvinski wrote: Unfortunatly Deep Throat isn't my cup of tea Do you Sonork? I do! 100.9903 Stormfront

          C Offline
          C Offline
          Christopher Lord
          wrote on last edited by
          #4

          Indeed. Lately, in my experiments with c#, Once in a while I feel relieved I dont have to worry about the horrors of low-level strings (and the mish-mash of different string classes) constantly. // Rock

          C 1 Reply Last reply
          0
          • M marouane miftah el kheir

            i just started learning C++ here is the first problem i encountered. how do you declare String data types. like if i want to have some variables like firstName, lastName; how do i store those variables??? i know it is not like java where you can just declare something as a String. thanks in advance. Marouane

            M Offline
            M Offline
            markkuk
            wrote on last edited by
            #5

            #include <string> std::string firstName; std::string lastName;

            M 1 Reply Last reply
            0
            • P Paul Watson

              marouane miftah el kheir wrote: i just started learning C++ here is the first problem i encountered. how do you declare String data types. like if i want to have some variables like firstName, lastName; how do i store those variables??? i know it is not like java where you can just declare something as a String. thanks in advance. Can someone please explain to me why even a simple thing like declaring a variable is such a task to do in C++? Do you really think that by making the programmer declare the variable, it's type AND it's length you are "breeding" better programmers? Just curious, as always :) regards, Paul Watson Bluegrass Cape Town, South Africa "The greatest thing you will ever learn is to love, and be loved in return" - Moulin Rouge Martin Marvinski wrote: Unfortunatly Deep Throat isn't my cup of tea Do you Sonork? I do! 100.9903 Stormfront

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Just a question of using the standard library std::string or MFC CString. As I understand it, in other languages also, strings are implemented as libraries and get included with a header. What is the difference? Is it a fault of the language that programmers still prefer to use character arrays and use string manipulation functions on them? Thomas

              1 Reply Last reply
              0
              • M markkuk

                #include <string> std::string firstName; std::string lastName;

                M Offline
                M Offline
                Michael A Barnhart
                wrote on last edited by
                #7

                Since the original post was I am new ... std::string is part of STL Standard Template Library. You may want to look that up. Also depending on which compiler/version you have there are a number of string classes. A fair number of people use MFC Microsoft Foundation Class just for the CString class. The last vc release this has been moved to ATL Active Template Library. I hope this is not to much info. To answer your original post however the original responce was correct. A string is an array of chars. It is also standard practice to ask this kind of question in the Message Board area since it is specific to one langauge. Man were things simpler when I started:) Take care,

                1 Reply Last reply
                0
                • P Paul Watson

                  marouane miftah el kheir wrote: i just started learning C++ here is the first problem i encountered. how do you declare String data types. like if i want to have some variables like firstName, lastName; how do i store those variables??? i know it is not like java where you can just declare something as a String. thanks in advance. Can someone please explain to me why even a simple thing like declaring a variable is such a task to do in C++? Do you really think that by making the programmer declare the variable, it's type AND it's length you are "breeding" better programmers? Just curious, as always :) regards, Paul Watson Bluegrass Cape Town, South Africa "The greatest thing you will ever learn is to love, and be loved in return" - Moulin Rouge Martin Marvinski wrote: Unfortunatly Deep Throat isn't my cup of tea Do you Sonork? I do! 100.9903 Stormfront

                  T Offline
                  T Offline
                  Tim Smith
                  wrote on last edited by
                  #8

                  Many times it is much easier to work with a standard C string than these string classes. Not to mention it is MUCH faster. Tim Smith Descartes Systems Sciences, Inc.

                  D 1 Reply Last reply
                  0
                  • P Paul Watson

                    marouane miftah el kheir wrote: i just started learning C++ here is the first problem i encountered. how do you declare String data types. like if i want to have some variables like firstName, lastName; how do i store those variables??? i know it is not like java where you can just declare something as a String. thanks in advance. Can someone please explain to me why even a simple thing like declaring a variable is such a task to do in C++? Do you really think that by making the programmer declare the variable, it's type AND it's length you are "breeding" better programmers? Just curious, as always :) regards, Paul Watson Bluegrass Cape Town, South Africa "The greatest thing you will ever learn is to love, and be loved in return" - Moulin Rouge Martin Marvinski wrote: Unfortunatly Deep Throat isn't my cup of tea Do you Sonork? I do! 100.9903 Stormfront

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #9

                    Paul Watson wrote: Can someone please explain to me why even a simple thing like declaring a variable is such a task to do in C++? Because this person is a beginner and didn't read any docs. std::string and (under mfc) CString, b_str, char * will all do the job. Christian Christian I have come to clean zee pooollll. - Michael Martin Dec 30, 2001 Picture the daffodil. And while you do that, I'll be over here going through your stuff. Picture a world without war, without hate. And I can picture us attacking that world, because they would never expect it.

                    Sonork ID 100.10002:MeanManOz

                    I live in Bob's HungOut now

                    1 Reply Last reply
                    0
                    • C Christopher Lord

                      Indeed. Lately, in my experiments with c#, Once in a while I feel relieved I dont have to worry about the horrors of low-level strings (and the mish-mash of different string classes) constantly. // Rock

                      C Offline
                      C Offline
                      Christian Graus
                      wrote on last edited by
                      #10

                      std::string works everywhere. Christian I have come to clean zee pooollll. - Michael Martin Dec 30, 2001 Picture the daffodil. And while you do that, I'll be over here going through your stuff. Picture a world without war, without hate. And I can picture us attacking that world, because they would never expect it.

                      Sonork ID 100.10002:MeanManOz

                      I live in Bob's HungOut now

                      1 Reply Last reply
                      0
                      • T Tim Smith

                        Many times it is much easier to work with a standard C string than these string classes. Not to mention it is MUCH faster. Tim Smith Descartes Systems Sciences, Inc.

                        D Offline
                        D Offline
                        Daniel Turini
                        wrote on last edited by
                        #11

                        Tim Smith wrote: Many times it is much easier to work with a standard C string than these string classes. Not to mention it is MUCH faster. Hmm... Not to C/C++ newcomers... Think about a Java or VB programmer being exposed for the first time to pointers (although in VB you can convert freely a string into a byte array) It's hard for them to understand that char * == char[] and this is a string zero terminated. It's hard for them having to allocate memory before strcpying. It's hard for them understanding why they can't return a pointer to a stack allocated char[]. STL is actually simple, although the docs are not, but MS implementation gives so many warnings that scare a beginner to his bones. I would recomend to a Visual C++ beginner using MFC's CString until he becomes more comfortable with pointers and arays. THEN he can start Crivo Automated Credit Assessment

                        T 1 Reply Last reply
                        0
                        • D Daniel Turini

                          Tim Smith wrote: Many times it is much easier to work with a standard C string than these string classes. Not to mention it is MUCH faster. Hmm... Not to C/C++ newcomers... Think about a Java or VB programmer being exposed for the first time to pointers (although in VB you can convert freely a string into a byte array) It's hard for them to understand that char * == char[] and this is a string zero terminated. It's hard for them having to allocate memory before strcpying. It's hard for them understanding why they can't return a pointer to a stack allocated char[]. STL is actually simple, although the docs are not, but MS implementation gives so many warnings that scare a beginner to his bones. I would recomend to a Visual C++ beginner using MFC's CString until he becomes more comfortable with pointers and arays. THEN he can start Crivo Automated Credit Assessment

                          T Offline
                          T Offline
                          Tim Smith
                          wrote on last edited by
                          #12

                          STL is simple. That doesn't mean it is right for every task. Many times you need to modify the string in place and operatate with portions of the string. STL just doesn't handle this well. Then there is the allocation of the memory every time you do an assign. That is just SLOWWWWWWWW. If all I need to do is string processing, I still use standard C strings. There is no point in using STL strings when all the do is slow the software down and complicate the code. Like I have always said, use the proper tool for the job. Sometimes it is STL strings, sometimes it is C strings. Tim Smith Descartes Systems Sciences, Inc.

                          1 Reply Last reply
                          0
                          • P Paul Watson

                            marouane miftah el kheir wrote: i just started learning C++ here is the first problem i encountered. how do you declare String data types. like if i want to have some variables like firstName, lastName; how do i store those variables??? i know it is not like java where you can just declare something as a String. thanks in advance. Can someone please explain to me why even a simple thing like declaring a variable is such a task to do in C++? Do you really think that by making the programmer declare the variable, it's type AND it's length you are "breeding" better programmers? Just curious, as always :) regards, Paul Watson Bluegrass Cape Town, South Africa "The greatest thing you will ever learn is to love, and be loved in return" - Moulin Rouge Martin Marvinski wrote: Unfortunatly Deep Throat isn't my cup of tea Do you Sonork? I do! 100.9903 Stormfront

                            M Offline
                            M Offline
                            Michael P Butler
                            wrote on last edited by
                            #13

                            It's a C thing not a C++ thing. C++ has the wonderful STL where we can using strings, instead of declaring character arrays - heck even MFC provides a CString class. Most of the crap in C++ can be blamed on C programmers :-D Michael :-)

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups