View Full Version : Character Array input on C++


caffeine43
02-26-02, 11:56 AM
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

Bowser
02-26-02, 12:50 PM
This is how I would write it...





main()
{
char aChar[80];

cout << "Enter your string";

cin >> aChar;
cout << aChar;

return 0;
}



You would need to include your iostream.h file too.

Bowser
02-26-02, 01:25 PM
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.

caffeine43
02-26-02, 01:47 PM
Is there a way to do that without using an array of a specific size?

Bowser
02-26-02, 02:07 PM
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.

kwvanderlinde
02-11-08, 01:41 PM
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