Hello. I'm an c++ beginner, and I want to make a little program, but i am suck here... i don't know how can I do with char, to input more that 1 character.
What I need to use, and how?
Thanks!
(enter == 'yes') and (enter == 'no') should be (enter == "no") and (enter == "no")
Single quotes are used only for single characters.
As for your question, you can simple do
1 2
char enter[10];
cin >> enter;
Only problem with this is that if the user enters more than 9 characters then your program will write to indexes out of memory. Which is bad. So you should use std::cin.getline() like FurryGuy said. Note that cin.getline() also reads whitespaces.
But like FurryGuy said if you know about the string datatype, you should use that instead. And you seemed to have included <string> already. == is supported by string so you don't need to use strcmp in that case.
if you put s on the end of it you can use == because this forces the string literal into a string object.
eg
string enter;
if(enter == "yes"s) //I may have messed up the syntax here, but if you need/want this you can look it up, my apologies as I don't do a lot of string processing.
Ooo jonnin's right. Didn't know you could do that! But what exactly happens when you change "yes" to "yes"s? And why does the comparison work only when it's specified as string as in what's the difference in how it's read by the compiler?
its just making a string instead of a char* for the literal behind the scenes. Its probably a mashup for efficiency behind the scenes, I don't know... but the user can treat it like a string.
I missed many generations of the language, I basically skipped from 99 to 2017 c++ versions... so I am no good at telling you when a feature was added. Im doing good just to know some of whats out there!
It works because the undelying type. String supports overload for ==, char* does not.
no, "yes"s is treated like a string.
"yes" without the s is treated like a char*
"" is a pointer because strings were added to the language later. When I learned c++, string did not exist. There were borland strings, microsoft strings, your company strings, bob's string class, joe's string class.... everyone took a crack at it and they were all different and most of them were terrible. Microsoft to this day has a lot of clunk in its tools because of its approach to strings (known by the wonderful names like slznptr or something like that to tell what kind of string it was by the letter codes in the name)