Thread: How to store a user input string into dynamic array?

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    3

    How to store a user input string into dynamic array?

    Hi I'm having trouble figuring out how to store something the user inputs into a dynamic character array? This is all I have so far:

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    
    string userString;
    
    cout << "Enter a string value: ";
    cin >> userString;
    cout << endl << endl;
    cout << "You entered: " << userString << ". *NOTE* We are not counting anything after white spaces!  Count: " << userString.length();
    cout << endl << endl;
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First what do you mean by "a dynamic character array"? Your posted code is using a std::string which is dynamic and the code should compile and run. Are you having some problem with this code?

    Jim

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Sorry for not being very clear about that. I need to dynamically create a character array, this is the challenge of the assignment, but I am having trouble getting my string input to count any white spaces. When I type in a sentence the output shows only the first word. Do you know what would cause this?

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Ok I finally found a solution for this problem. I'm hoping this i the best method for this:

    Using cin to get user input. - C++ Forum

    So basically what I have done is used getline:

    getline(cin, userString);

    Then I copy it's contents into a dynamic character array of length = userString.length() using a for loop.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To do the copy, you can do:
    std::copy(array, UserString.begin(), UserString.end());
    Make sure there is enough room in the array first!
    This is unsafe programming, but since it is an exercise, I suppose it is acceptable.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-08-2011, 11:22 AM
  2. Replies: 3
    Last Post: 11-01-2010, 08:22 PM
  3. Dynamic string input
    By CaptainQcumber in forum C Programming
    Replies: 7
    Last Post: 07-23-2010, 10:18 PM
  4. Replies: 2
    Last Post: 10-08-2005, 04:09 PM
  5. user string input
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-13-2001, 07:47 AM