storing an string in C dynamically

This is a discussion on storing an string in C dynamically within the C++ Programming forums, part of the General Programming Boards category; is there anyway to store a string (which is imputted from the user) into an array?...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    1

    storing an string in C dynamically

    is there anyway to store a string (which is imputted from the user)
    into an array?

  2. #2
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Code:
    char* cstyle = new char[50];
    
    cin.getline(cstyle, 50);
    For next time, please read a tutorial on the C/C++ language before posting questions like this (this is pretty basic stuff).
    Last edited by homeyg; 03-10-2006 at 07:43 PM.

  3. #3
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    This is c++ anyways, so why not use string?

    If for whatever reason you needed a c array you can use the c_str() method

  4. #4
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    He said put a string into an array, not put a string into a string..

    Plus the string class is for morons who can't properly handle real C style strings.

  5. #5
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Maybe he wanted something like
    Code:
    string array[5];
    
    for (int i = 0; i < 5; i++)
      getline(cin, array[i], '\n');
    Not sure if he wanted the array dynamical though.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    is there anyway to store a string (which is imputted from the user)
    into an array?
    This sounds like "how do I get a string from the user?". FAQ: FAQ > How do I... (Level 1) > Obtain a string from the user (C++)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by homeyg
    He said put a string into an array, not put a string into a string..

    Plus the string class is for morons who can't properly handle real C style strings.
    I really hope you're joking. I'm not even gonna go into why that's stupid.

    He also said store a string in C dynamically not C++, so no std::string and no new.

    Code:
    #incude <stdio.h>
    
    int main()
    {
       int len = 0;
       char* pString = NULL;
       printf("How long is a piece of string?");
    
       scanf("%d", &len);
    
       pString = malloc(len);
    
       printf("enter the string");
       scanf("%s", pString);
    }
    man that takes me back! That's the C way to do it.

    if you ARE using C++, that's about the worst way to do it.
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string str;
        cout << "enter a string: ";
        cin >> str
    }
    Last edited by ChaosEngine; 03-11-2006 at 04:57 PM. Reason: bloody C variable declaration rules!!
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    He said put a string into an array, not put a string into a string..
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    
    	string myStr;
    	cout<<"Enter a string of any length: ";
    	getline(cin, myStr);
    
    	cout<<"The characters you entered were:\n";
    	
    	for(int i = 0; i < myStr.length(); i++)
    	{
    		cout<<myStr[i]<<endl;
    	}
    
    	return 0;
    }
    Well, looky there: a string is an array. Imagine that.

    He also said store a string in C dynamically not C++, so no std::string and no new.
    ...but it was posted in a C++ forum. std::string and new until the sun don't shine!

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by ChaosEngine
    Code:
    #incude <stdio.h>
    
    int main()
    {
       int len = 0;
       char* pString = NULL;
       printf("How long is a piece of string?");
    
       scanf("%d", &len);
    
       pString = malloc(len);
    
       printf("enter the string");
       scanf("%s", pString);
    }
    man that takes me back! That's the C way to do it.

    if you ARE using C++, that's about the worst way to do it.
    If you're using C, that's STILL the worst way to do it. What happens if I claim my string is shorter than what I actually enter?
    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

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    54
    Quote Originally Posted by 7stud
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    
    	string myStr;
    	cout<<"Enter a string of any length: ";
    	getline(cin, myStr);
    
    	cout<<"The characters you entered were:\n";
    	
    	for(int i = 0; i < myStr.length(); i++)
    	{
    		cout<<myStr[i]<<endl;
    	}
    
    	return 0;
    }
    Well, looky there: a string is an array. Imagine that.


    ...but it was posted in a C++ forum. std::string and new until the sun don't shine!

    strings are for the lazy >.>

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,068
    Quote Originally Posted by homeyg
    He said put a string into an array, not put a string into a string..

    Plus the string class is for morons who can't properly handle real C style strings.
    Quote Originally Posted by JeremyCAFE
    strings are for the lazy >.>
    *........es himself*

    Rebels without a cause... I suppose you shouldn't use the ANSI strlen(), strcpy(), strcmp(), or any other predefined functions for strings either. They're just for lazy morons.
    Last edited by SlyMaelstrom; 03-11-2006 at 08:42 PM.
    Sent from my iPad«

  12. #12
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by CornedBee
    If you're using C, that's STILL the worst way to do it. What happens if I claim my string is shorter than what I actually enter?
    true. to be honest it's been a long time since I've done any C (actually had to look up scanf and malloc ),
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    strings are for the lazy >.>
    Crazy lazy:
    Code:
    #include <iostream>
    #include <string>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
    
    	string myStr;
    	cout<<"Enter a string of any length: ";
    	getline(cin, myStr);
    	
    	cout<<"The characters you entered were:\n";
    	
    	copy(myStr.begin(), myStr.end(), ostream_iterator<char>(cout, "\n"));
    
    	return 0;
    }

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Laziness is a virtue.
    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

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

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