Hello just had a m8 round and he reckons that this is a string:

char data;

I told him no its not to start a string it must be

char data[];
cin << data;

with the inclusion of the header files, is he correct or am i?

Recommended Answers

All 7 Replies

Neither. An array of char that ends with a terminating null byte is a c-style string. A single char thus can never be a string.

And an uninitialized char data[]; is an incomplete type.

And 'operator<<' not implemented in type 'istream' for arguments of type 'char *'.

Neither. An array of char that ends with a terminating null byte is a c-style string. A single char thus can never be a string.

And an uninitialized char data[]; is an incomplete type.

And 'operator<<' not implemented in type 'istream' for arguments of type 'char *'.

umm so can you give me an example of a string?


i found these in my text book under strings:

char string1[10];
char string2[] = "Hello";

cout << string2; // displays Hello....


-------------------------------------------------------------------------
Or is this a string?

char string[4];
cin.getline(string, 4, '\n');

>umm so can you give me an example of a string?

char string1[10]; // maybe, but not yet
char string2[] = "Hello"; // yes

Do you see how each of these is different from your previous data?

>Or is this a string?

char string[4]; // maybe, but not yet
cin.getline(string, 4, '\n'); // yes now, if call was successful

Aah !! u have to specify the size of the array

String handling in C++ is NOT generally to be done using char[] or char*, but using the class string which is available in <string>.
Far easier and more safe.

So instead of the code you have you'd simply do

string s;
cin << s;

and for your m8:char data; --> is a character variable NOT a string. When Dave Sinkula says "maybe not yet" he is reffering to the fact that

char data[10] is an ARRAY of characters. if and ONLY if the last is '\0' then it is a string

I agree jwenting that the ANSI string class is MUCH easier to use and works with stream operators >> and <<.

Heres one though: that char data;

if data was '\0' that could be an EMPTY string :)

for me..i dont think itsa stupid question bro...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.