User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 427,851 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,729 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1878 | Replies: 7
Reply
Join Date: Dec 2004
Posts: 465
Reputation: Acidburn is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

This may be a stupid question....

  #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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,649
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 144
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

  #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  
Join Date: Dec 2004
Posts: 465
Reputation: Acidburn is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

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

  #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  
Join Date: Apr 2004
Posts: 3,649
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 144
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

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

  #4  
Jan 7th, 2005
>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
Reply With Quote  
Join Date: Nov 2004
Location: India
Posts: 57
Reputation: harshchandra is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
harshchandra harshchandra is offline Offline
Junior Poster in Training

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

  #5  
Jan 8th, 2005
Aah !! u have to specify the size of the array
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 199
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

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

  #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
string s;
cin << s;
Reply With Quote  
Join Date: Dec 2004
Location: Devon - UK
Posts: 420
Reputation: 1o0oBhP is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

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

  #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  
Join Date: Dec 2004
Posts: 22
Reputation: geeeez is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
geeeez's Avatar
geeeez geeeez is offline Offline
Newbie Poster

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

  #8  
Jan 8th, 2005
for me..i dont think itsa stupid question bro...
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 3:23 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC