Character Array input on C++

Discussion in 'Computer Science & Culture' started by caffeine43, Feb 26, 2002.

Thread Status:
Not open for further replies.
  1. caffeine43 Registered Member

    Messages:
    7
    How would someone get a character array (ultimately, I want it to be of type String, which I wrote) from user input? I don't think that

    char* a;
    cin >> a;

    works...
    Thanks in advance...

    -Tom
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. Bowser Life is Fatal. Valued Senior Member

    Messages:
    6,075
    This is how I would write it...


    Code:
    
    main()
    {
        char aChar[80];
    
        cout << "Enter your string";
     
        cin >> aChar;
        cout <<  aChar;
    
        return 0;
    }
    
    
    You would need to include your iostream.h file too.
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. Bowser Life is Fatal. Valued Senior Member

    Messages:
    6,075
    caffeine43,

    Keep posting you questions here; however, I want to offer you another useful link.

    http://www.programmersheaven.com/

    Also, "C++, The Complete Reference, Second Edition," by Herbert Schildt, is an excellent book for answering those programming questions.
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. caffeine43 Registered Member

    Messages:
    7
    Is there a way to do that without using an array of a specific size?
     
  8. Bowser Life is Fatal. Valued Senior Member

    Messages:
    6,075
    You need to allocate space for your array. If the string size is unknow, you would then try dynamic allocation (new & delete), which requires an algorithm that checks the size of the string each time a user inputs a character. The algorithm would then allocate the neccessary space in memory for storing the larger string.

    Your looking at some pointer manipulation and possibly a specialized function or enhanced class. There might be something in the C++ I/O that will perform this task for you, but I don't know if there is.
     
  9. kwvanderlinde Registered Member

    Messages:
    1
    The easiest way to do this is



    #include <iostream>
    using namespace std;

    int main(){
    char* string;
    cin >> (string = new char);
    cout << string;
    system("pause");
    }

    This makes an array of char variables.
    The string variable points to the first char in the array
     
Thread Status:
Not open for further replies.

Share This Page