CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Oct 2008
    Posts
    65

    New to C++ but familiar with Programming

    Ok im trying to figure out how to hold a string variable, how do i declare a variable as the string type.

    like this example in PHP:

    $myString = "This is my string";

    regards thank you

  2. #2
    Join Date
    May 2007
    Posts
    811

    Re: New to C++ but familiar with Programming

    Code:
    #include <string>
    int main()
    {
       std::string myString = "This is my string";
    }

    std::string info.
    Last edited by STLDude; May 7th, 2009 at 04:22 PM. Reason: Added link

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: New to C++ but familiar with Programming

    Just "plain" C++ doesn't have a string type. String management is the same as in C, using arrays of char's (or widechars for unicode).

    The Standard Template Library which comes with most recent C++ compilers does have one, as STLDude mentioned.

    Most other libraries offer their own version of string classes. MFC/ATL has CString.


    At the low level. A string in most C++ libraries ends up being a pointer to a character array and ending with a zero terminator. The memory this pointer points to is usually allocated and disposed of via new[]/delete[].

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: New to C++ but familiar with Programming

    "plain" C++ doesn't have a string type.
    It depends on what you mean by "plain" C++. If by "plain", you mean just C++ syntax, then there is no "string" type.

    However, the language library of C++ (and C) are considered parts of the language. "Plain" C++ could mean what is defined by ANSI, which includes syntax and libraries. If this is what it means to be "plain", then C++ does have a string type (std::string).

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; May 7th, 2009 at 07:50 PM.

  5. #5
    Join Date
    Oct 2008
    Posts
    65

    Re: New to C++ but familiar with Programming

    thank you guys for the help.
    im using visual C++ from Microsoft's site. and im making a form app. isnt the "std::string" only used in console apps?

    thankyou

  6. #6
    Join Date
    May 2007
    Posts
    811

    Re: New to C++ but familiar with Programming

    isnt the "std::string" only used in console apps?
    No.

    So, what would you use in GUI/Form app if you needed to deal with strings? (20 points for answering).

  7. #7
    Join Date
    Oct 2008
    Posts
    65

    Re: New to C++ but familiar with Programming

    say i wanted to hold the path to a file in the string. i want to use the string in a few places and not have to rewrite it.
    but simply for usability and ease.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: New to C++ but familiar with Programming

    Quote Originally Posted by sacarias40 View Post
    say i wanted to hold the path to a file in the string. i want to use the string in a few places and not have to rewrite it.
    but simply for usability and ease.
    Let's change some words:
    "say I wanted to hold the student average in the double, I want to use the double in a few places and not have to rewrite it."
    Now what is the difference between the above, and what you stated? What difference does it make if the variable type is an int, double, char, or string? None. A string is just another data type. Except for a couple of cases, it doesn't need to be treated differently than any other built-in type.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Oct 2008
    Posts
    65

    Re: New to C++ but familiar with Programming

    Well im not exactly sure on this but i suppose you cannot do math with a string only with an int or double or something of that sort.
    if im right, dont doubles and ints only hold numbers. please correct me if im wrong.

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: New to C++ but familiar with Programming

    Quote Originally Posted by sacarias40 View Post
    Well im not exactly sure on this but i suppose you cannot do math with a string only with an int or double or something of that sort.
    if im right, dont doubles and ints only hold numbers. please correct me if im wrong.
    You're missing the point.

    A string is nothing but another data type -- a type that can hold "strings". Most languages have string types, numeric types, boolean types, etc. C++ is no different than those languages.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; May 8th, 2009 at 08:43 PM.

  11. #11
    Join Date
    Oct 2008
    Posts
    65

    Re: New to C++ but familiar with Programming

    i understand this but i just need to have the equivalent to this:

    var filePath = "C:\users\name\desktop";

    C++ style

    or this vb style:

    dim filePath as string = "C:\users\name\desktop"

  12. #12
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: New to C++ but familiar with Programming

    Did you bother to read any of the example code in the reference link above?

  13. #13
    Join Date
    Oct 2008
    Posts
    65

    Re: New to C++ but familiar with Programming

    yes, and the above is confusing. they are telling me two things. std::string and char str[]

    but i dont know what will work for me. just some help is all im looking for. lol

  14. #14
    Join Date
    May 2007
    Posts
    811

    Re: New to C++ but familiar with Programming

    So, is this example (listed in my previous reply also) was not enough to show how to initialize a string?
    Code:
    #include <string>
    int main()
    {
       std::string myString = "This is my string";
    }

  15. #15
    Join Date
    Jan 2009
    Posts
    12

    Re: New to C++ but familiar with Programming

    Go play around with this.

    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	string Path = "C:\\users\\name\\desktop";
    
    	cout << Path << endl;
    
    	cin.get();
    
    	return 0;
    }

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

Featured