String C /C++

This is a discussion on String C /C++ within the C++ Programming forums, part of the General Programming Boards category; When i want to use a string in C I tell my compiler to make an array of chars (e.g. ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    67

    String C /C++

    When i want to use a string in C I tell my compiler to make an array of chars (e.g. char myString[15]), but in C++ theres a special datatype (string) to declare a string.. can i still use the C way in C++ or what would you recommend,
    In my opinion the array-approach is the best but then again i started C++ 10 minutes ago.. can anyone tell me a bit more about it?

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    17
    Actually I beg to differ, the string class is much more flexible than an array. The method to use it is below.

    Code:
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    
    int main(void)
    
    {
    
         string str;
    
         cout<<"Enter your name.\n";
    
         cin>>str;
    
         cout<<"Hello "<<str<<"!"<<endl;
    
         system("pause");
    
         return 0;
    
    }
    The string class method allows you to input as many characters as your memory can handle! Whereas to come close you would have to dynamically allocate memory in a string. Or possibly use a the vector array.

    I'm about an intermediate programmer to be honest. I use the string method to read from files, it's my prefered method. Although, I do use strings I dynamically allocate its more work doing so.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    67

    aa

    I found a solution to my problem.. I use vectors now

  4. #4
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    To get the above code to work you'll need to add the line
    Code:
    using namespace std;
    below the includes. Also, stdlib.h is a deprecated header, it's preffered to use cstdlib.
    Detailed understanding of language features - even of all features of a language - cannot compensate for lack of an overall view of the language and the fundamental techniques for using it. - Bjarne Stroustrup

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    67
    yes i knew that but thanks anyway

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21