944,103 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2776
  • C++ RSS
Jan 7th, 2005
0

This may be a stupid question....

Expand Post »
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?
Similar Threads
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Jan 7th, 2005
0

Re: This may be a stupid question....

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 *'.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jan 7th, 2005
0

Re: This may be a stupid question....

Quote originally posted by Dave Sinkula ...
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');
Reputation Points: 12
Solved Threads: 5
Posting Pro
Acidburn is offline Offline
510 posts
since Dec 2004
Jan 7th, 2005
0

Re: This may be a stupid question....

>umm so can you give me an example of a string?
C++ Syntax (Toggle Plain Text)
  1. char string1[10]; // maybe, but not yet
  2. char string2[] = "Hello"; // yes
Do you see how each of these is different from your previous data?

>Or is this a string?
C++ Syntax (Toggle Plain Text)
  1. char string[4]; // maybe, but not yet
  2. cin.getline(string, 4, '\n'); // yes now, if call was successful
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jan 8th, 2005
0

Re: This may be a stupid question....

Aah !! u have to specify the size of the array
Reputation Points: 7
Solved Threads: 1
Junior Poster in Training
harshchandra is offline Offline
68 posts
since Nov 2004
Jan 8th, 2005
0

Re: This may be a stupid question....

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
C++ Syntax (Toggle Plain Text)
  1. string s;
  2. cin << s;
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Jan 8th, 2005
0

Re: This may be a stupid question....

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
Reputation Points: 16
Solved Threads: 6
Posting Pro in Training
1o0oBhP is offline Offline
445 posts
since Dec 2004
Jan 8th, 2005
0

Re: This may be a stupid question....

for me..i dont think itsa stupid question bro...
Reputation Points: 9
Solved Threads: 0
Newbie Poster
geeeez is offline Offline
22 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Keyboard input or "stuck in a loop"
Next Thread in C++ Forum Timeline: queue , memory, 32k please





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC