How to limit cin to string?
-
char str[8];
cin >> str;When run, if inputed string size more than eight byte, result run time error. So, how to limit cin >> size or only get eight byte when inputed more than eight? thanks!
-
char str[8];
cin >> str;When run, if inputed string size more than eight byte, result run time error. So, how to limit cin >> size or only get eight byte when inputed more than eight? thanks!
allocate memory dynamically based on the size of the string You can use a loop (upto 8) along with getch()/getchar() to get the user input
Some things seem HARD to do, until we know how to do them. ;-)_AnShUmAn_
-
char str[8];
cin >> str;When run, if inputed string size more than eight byte, result run time error. So, how to limit cin >> size or only get eight byte when inputed more than eight? thanks!
cin statement doesn't provide you with limited input support. since it wait for RETRUN/ENTER key for updating the input character in buffer supplied to it.!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/xml>
-
char str[8];
cin >> str;When run, if inputed string size more than eight byte, result run time error. So, how to limit cin >> size or only get eight byte when inputed more than eight? thanks!
std::cin.get(str, sizeof(str), '\n'); or std::cin >> std::setw(sizeof(str)) >>str;
-
char str[8];
cin >> str;When run, if inputed string size more than eight byte, result run time error. So, how to limit cin >> size or only get eight byte when inputed more than eight? thanks!
hanlei0000000009 wrote:
When run, if inputed string size more than eight byte, result run time error.
Were you expecting a different result? Why not use a
string
object instead?"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch