Character Array input on C++

Status
Not open for further replies.

caffeine43

Registered Member
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
 
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.
 
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.
 
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.
 
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
 
Status
Not open for further replies.
Back
Top