I am writing a text-based Scrabble implementation for a college project.
The specification states that the user’s position input must be read from single line, like this:
Coordinates of the word's first letter and orientation (<A – P> <1 – 15> <H ou V>): G 5 H
G 5 H is the user’s input for that particular example. The order, as shown, must be char int char.
What is the best way to read the user’s input?
cin >> row >> column >> orientation will cause crashes if the user screws up.
A getline and a subsequent string parser are a valid solution, but represent a bit of work.
Is there another, better, way to do this, that I am missing?
Thanks for your time!
getlineand parsing doesn’t necessarily have to add much work. Since you already know how to read (correct) data from a stream, just read a line withgetline, then create anistringstreamfrom the line and read from there.The one thing I’d add would be that it might very well make sense to create a class to hold the data for a particular move, and overload
operator>>for that class to read data for a move. A rough sketch would be something like this:At least for the moment, I haven’t included any error handling code. Depending on how picky you want to be, this can get a little tricky (e.g., if input from the
stringstreamfails, setting the fail bit in the original input stream).