Tuesday, July 14, 2009

Another C++ input question; cin and strings.?

K so if someone has a compiler on them they may be able to tell me or if you know anyway;





Suppose I declare an array of 5 strings.





vector%26lt;string%26gt; word(5);





The I use cin to input values saying;





cin%26gt;%26gt;word(0)%26gt;%26gt;word(1)%26gt;%26gt;word(2)%26gt;%26gt;word(3...





When the program runs and I am prompted for input I write.





" apples bear lemon party "





With all these spaces in there, will C pick out the words and store them in their respective string variables?





Are the vals:





word(0) is "apples"


word(1) is "bear"


word(2) is "lemon"


word(3) is "party"





-What is word(4), is it " " ie a single space, is is a number of spaces, or is it some weird thing without a value or a random value?


Can I use a simple if statement to check for such blanks?





if(word.at(4) == ' ') %26lt; does this actually tell me if it's blank?

Another C++ input question; cin and strings.?
word(4) will be whatever the default value for a vector is. You can't accept whitespace in cin, for that you need to use getline. However comparing word(4) == "" will tell if it is an empty string.





Also be careful with strings and characters:


" - for strings


' - for characters


No comments:

Post a Comment