Array of strings

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

Join Date: Sep 2008
Posts: 10
Reputation: sunveer is an unknown quantity at this point 
Solved Threads: 0
sunveer sunveer is offline Offline
Newbie Poster

Array of strings

 
0
  #1
Oct 6th, 2008
the below code is to allow user to enter many strings of characters until he enters a fullstop (.)
Please tell me how can i calculate the total number of strings the user has entered.

#include <iostream>
void main()
{
char a[80];
int i,c=0;
for(i=0;;i++)
{
cin>>a[i];
if(a[i]=='.')
break;
}
}
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 89
Reputation: bhoot_jb is on a distinguished road 
Solved Threads: 2
bhoot_jb bhoot_jb is offline Offline
Junior Poster in Training

Re: Array of strings

 
0
  #2
Oct 6th, 2008
well, i am quite confused here. You mean to say you want to take input of several strings in a single character array?

infact, here you are asking for a single character everytime :
> cin >> a[i]

if you meant to count the number of characters, then you may do it using some function like strlen(), or you may put a counter along with input.

Also, the array here is certainly unstable, without '\0' null character.
Bhoot
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 10
Reputation: sunveer is an unknown quantity at this point 
Solved Threads: 0
sunveer sunveer is offline Offline
Newbie Poster

Re: Array of strings

 
0
  #3
Oct 6th, 2008
i want the string for eg: "hello this is c++" like this .
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Array of strings

 
0
  #4
Oct 6th, 2008
you need to use getline() in that case to allows you to read more than just 1 character in at a time

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 89
Reputation: bhoot_jb is on a distinguished road 
Solved Threads: 2
bhoot_jb bhoot_jb is offline Offline
Junior Poster in Training

Re: Array of strings

 
0
  #5
Oct 6th, 2008
If you want only a single string, then its getline().

If you want several strings, then a single array certainly wont do. I wonder how will you differentiate between strings. As you said, if you use '.', still wouldnt it be better if you use a 2-D array of characters, or an array of character pointers?
Bhoot
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