What is the exact function of cin.getline? What is the major difference while using it for character array and for strings?
Doubt in theory
cin.getline() is used to take whole line as input (including spaces ā ')
In the introductory video to strings, s[i] gives each character in the string while in this video while taking n strings as input s[i] yields the whole of the ith string out of n taken. Can you please clarify this? I find this a bit confusing.
I am not sure if I get you. But please note that the output of s[i] will depend on the type of s[i], if s[i] is char than only one character is the output, if a string then a string is the output.
The s[i] doubt still persists. Is there any alternative method through which I can communicate it to you (like a phone call) ?
If I write
string s=āhelloā and cout s[0], I will get āhā, right?
but in this code(https://codeshare.io/21ejEm) when I write s[i], I get whole string as output.
@somesh.ps the type of the array is string in this case.
if you write string s=āhelloā this is a single string
if you write string s[100] then s becomes an array of string
Did you understand the difference?
If I write s[10], it is an array that contains various strings as element? Can āapple mangoā be such an element in that array of strings?
So, basically if I am getting it right there are three types of arrays: integer , character, strings!
hi @somesh.ps an array can be of any type! they are not limited to any certain datatypes.
So yes if you have an array of strings like
string s[10];
you can say s[0] = āappleā;
and a[1] = āmangoā;
Now if you want to access individual characters then you can do so like
s[0][1]
this will give you the second character of the first string of s
Thanks a lot! It was very confusing. Now it is clear.
Can you please explain where we should use strings and where we should use character arrays?
These two are more or less the same. I donāt get the point using two different things for the same application.
@somesh.ps in C language, there was no support for strings, so character arrays had to be used, but now string datatype is also there so you can use that only. Thereās not much difference between the two, strings are easier to use and are resizeable as well, plus more library functions are there.
Ohhh! Thanks for the clarification. I was wondering the same, why use two different approaches. Really confusingš
.
Thanks again!
