This may be a stupid question....

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

This may be a stupid question....

 
0
  #1
Jan 7th, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
0
  #2
Jan 7th, 2005
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 *'.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

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

 
0
  #3
Jan 7th, 2005
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');
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

 
0
  #4
Jan 7th, 2005
>umm so can you give me an example of a string?
  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?
  1. char string[4]; // maybe, but not yet
  2. cin.getline(string, 4, '\n'); // yes now, if call was successful
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 68
Reputation: harshchandra is an unknown quantity at this point 
Solved Threads: 1
harshchandra harshchandra is offline Offline
Junior Poster in Training

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

 
0
  #5
Jan 8th, 2005
Aah !! u have to specify the size of the array
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

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

 
0
  #6
Jan 8th, 2005
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
  1. string s;
  2. cin << s;
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

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

 
0
  #7
Jan 8th, 2005
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
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 22
Reputation: geeeez is an unknown quantity at this point 
Solved Threads: 0
geeeez's Avatar
geeeez geeeez is offline Offline
Newbie Poster

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

 
0
  #8
Jan 8th, 2005
for me..i dont think itsa stupid question bro...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC