Sunday, June 30, 2013

C++ (beginner) different ways of reading string type

As we all do we write cin>>s; (s is our string type)
what if we need to read a whole line which can include spaces;
cin will read the string until it reaches a space bar or enter. For that we can use a special function called getline..
The getline function can be called in 2 ways
getline(cin,s);
cin.getline(s);

getline function, as the name suggests, this will get everything on that line.

another way is to read a set of characters.
suppose we have char a[10000];
string s;
we can use scanf function to read the characters
scanf("%c",a);
this will read all he text in an array of characters. now we can write this
s=a;
though a is an array and s is a string but you can write thath . NOTE that you can't write the reverse version
a=s; this one is not legal.

No comments:

Post a Comment