Doubt in theory

What is the exact function of cin.getline? What is the major difference while using it for character array and for strings?

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) ?

@somesh.ps can you share the code that you are referring to?

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!

@somesh.ps please mark your doubt as resolved if I have solved all your queries :slight_smile:

1 Like