943,879 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 672
  • C++ RSS
Oct 6th, 2008
0

Array of strings

Expand Post »
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;
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sunveer is offline Offline
10 posts
since Sep 2008
Oct 6th, 2008
0

Re: Array of strings

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.
Reputation Points: 57
Solved Threads: 2
Junior Poster in Training
bhoot_jb is offline Offline
89 posts
since Mar 2008
Oct 6th, 2008
0

Re: Array of strings

i want the string for eg: "hello this is c++" like this .
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sunveer is offline Offline
10 posts
since Sep 2008
Oct 6th, 2008
0

Re: Array of strings

you need to use getline() in that case to allows you to read more than just 1 character in at a time

Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Oct 6th, 2008
0

Re: Array of strings

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?
Reputation Points: 57
Solved Threads: 2
Junior Poster in Training
bhoot_jb is offline Offline
89 posts
since Mar 2008

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: multiplying values that are stored in the array
Next Thread in C++ Forum Timeline: Linked List Question





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


Follow us on Twitter


© 2011 DaniWeb® LLC