Validity Check: Fatty Fingers and stringstreams

Remember how I said I’m studying coding?  Yeah, that’s still been completely true, I’ve just been puttering along without any real issue following my course on  The e-instructor described things quite well through a nice, thick accent, and anything he didn’t describe I was capable of using the twin powers of my logical brain testing things independently and to resolve.

That is, until I ran into a data validation exercise.

The instructor hadn’t gone too far into detail about the actual functions behind a lot of the directions he was giving including the use of cin.rdstate() and cin.clear();.  I, and apparently a large portion of the inexperienced internet, believed that the latter function would clear the stream of console input.  We were wrong.  We were all horribly wrong.  All it does is clear the error tag on cin and allow the stream to continue being used.

What I was told to do - logically this was the equivalent of "did fatty fingers happen? Alright, let's forgive the fatty fingers, ignore half of what they did, wipe the screen to remove evidence of the fatty fingers, go back to listing our menu again, and let the fatty fingers know they were fatty if they didn't even enter a number!  Silly fatty fingers."
What I was told to do: logically this wasn’t a bad idea in isolation and was the equivalent of “Did fatty fingers happen? Alright, let’s ignore that and keep going!”

So, for example, if I had a line of code getting an input for an integer involved in my lovely while loop and instead of an integer my dog crawled on my keyboard and entered COWSARESKINNY, my program would pee itself and then pee some more as it kept trying to convince itself it was using a valid data type per my instructions.

The other thing my e-instructor had suggested doing is using cin.ignore(numeric_limits<streamsize>::max(), ‘/n’);.  This essentially told my data input to ignore everything after any valid data or until a new line.  This could have worked just fine, because if I entered 2ynuhdoaf4reay, I’d have an input of 2 instead, and everyone would be happy fatty fingers didn’t destroy the day.  Option two in my menu said to compute the area of a square.  Fantastic!

Well, this time, anyway.
Fatty fingers will NOT ruin my day!

HOWEVER, my particular program then needed another integer and finally a character that’s a y/n to know whether or not to continue running, so since my stream was still full of the rest of the input that was simply ignored earlier, here is what my computer would do in response to my rabid key mashing:

Take 2 to select to use my square function, then start looking for another numeric value in the stream to use that as the length of a side.  It came across a weird string of characters, which it was told to ignore and give an error message, which in my case said, “You and your silly characters!  I said an integer!”.  It kept pushing through this error message, clearing the screen so it’d look pretty and hide the evidence of previous errors, until finally it found what I had told it to look for.  A number.  The number 4.  My code did a happy dance and giggled excitedly, finally computing the area of a square.  16!  Hooray!  Then I sent it back to my main function where it asked whether or not we wanted to calculate the area of another shape, so it then looked in the input stream for a character.  r, e, a where all invalid being that they weren’t a y or an n, and so the computer spat repeatedly to enter the area of another shape until it decided to.  ALL this without a human being needing to add anything else in!

I had a problem.  A problem my instructor over the internet hadn’t even looked at because he hadn’t produced this case during the lesson, and while I was sure there would be a later point where data validation would be looked at further with a solution that didn’t cause my program to just auto-run itself into failure, I didn’t learn it yet.  Was I particularly attached to perfecting a program that calculated the area of a shape?  No.  But it worked incorrectly for what I had just been supposed to learn, and darn it, I wasn’t going to fail at acquiring the expected knowledge!  So I set to my problem-solving regime: a healthy mix of independent research and soliciting the aid of those I perceive as being better at what I’m trying to do than I am.

My first step was to test a few different cases to determine if my hypothesis about my error was correct.  It was.  Hooray!  My next step was to try to figure out a way I could clear the input stream after getting the data I wanted.  After trying many, many things, I finally enlisted my husband’s assistance in case he happened to know the information I was looking for.

While talking to him did further my understanding of my code produced with the aid of an online instructor, I unfortunately did realize that he didn’t know a command that would solve this problem either.  The next step was to hit up the internet again, and while the internet had a variety of suggestions, none of them met the program’s outlined perimeters – that is, accepting the valid input if they typed correctly the first time but just got fatty fingers afterward.  Not to say I’m against typically forcing people to enter their input correctly in order to get what they want.

My next step was to check in with my friend Michie who is a professional programmer who would like to ride off into the sunset with C++.  She, also, said she typically just refused any data input that wasn’t perfect.  So back to the internet I went.  I finally found a post at‘s forums that addressed my issue.  It all seemed so simple now, but it required the use of a library I had never used before: sstream.

string input, stringstream iStream, and int choice lived together happily ever after.
string input, stringstream iStream, and int choice lived together happily ever after.

Here’s how it worked to the best of my knowledge.  I’d get an input from cin and dump it all into a string named input.  I’d then make a stringstream, which to my understanding is essentially a stream that can handle both input and output instead of just one or the other.  Since I get the input initially as a string, I dump everything in my cin stream directly to my input without having to worry about some extra stuff floating around, and since stringstream can input and output, I input the stream into my integer data until it can no longer input.  If it can’t add any of the stream to my integer at all, it breaks out, clears the screen, and lets me know I have another chance to try again.  If I can enter a number but then have some insanity afterward, it takes the valid input and then moves on.  this removed my issue of invalid information still being stored in the string afterward, because with a new input, the content of my stream is changed to the new input.  Unlike just cin, I can use the stream for input and output, so I can convert the data safely from my string input to the stream to my data type.  I no longer had to ignore the rest of my stream, only for my cin stream to vomit them up later when I least expected.

Thank you, stranger on the internet at the C++ forums, for helping me solve this problem!  My code, while still not completely elegant, is much more functional now and forgiving to fatty fingers everywhere.

Validity Check: Fatty Fingers and stringstreams

From Special Ed Teacher to Technophile

This is a blog dedicated to my attempts to studying computer science and code by myself and recovering from an injury.  Many people have done it before me, and many people will do it after me.  So why blog about it?  In September, I was rammed into a wall while blocking a student from punching me, and he kept swinging.  I’m a special ed teacher who works with emotionally and behaviorally disturbed children, so I’m aware that getting physically assaulted sometimes happens when the best interventions and motivators weren’t successful for a multitude of reasons.  In this particular case, I had decided to intervene when the student was strangling himself after weeks of asking district administrators for advice on how to help him since I felt out of my specialty.  I was repeatedly told they believed in my professional judgment after I had told them that due to his self-injurious behaviors either I would be allowing him to harm himself or intervene and be harmed.  Neither of these scenarios were particularly appealing to me, and while we used a plethora of positive interventions that worked in some cases, a history of mental illness and abuse was not something that I could resolve within a few short weeks by modeling, coaching, and reinforcement.  This was indicated by a number of scars he had left on me prior to that day when he continuously banged his head on the wall, and I decided to intervene.

I ended up with a shoulder injury that limited my mobility in my right arm.  Thank goodness I’m left-handed.  The doctor pulled me out for 2 weeks, which as a fairly active person was very dull for me, and then once I was placed back I kept getting injured more severely and being pulled off work again.  After a few months of this, human resources decided to pull me out of the classroom occupied by a variety of sometimes physically aggressive children until my shoulder was up to snuff longer term and the district offered me light duty work reviewing student IEPs.  It’s not the most mentally engaging work, but I’m pretty good at it and it’s way better than being off work.  I’m just grateful to be working again.

Most people would be pretty thrilled to have had time off from work partially paid.  I was not.  My brain on boredom is very obnoxious, and I wasn’t capable of doing a majority of my previous hobbies anymore.  Prior to September, I had the following hobbies: swimming, rock climbing, paddle boarding, knitting, gaming, cooking, hiking, reading non-fiction, sci-fi, and fantasy books, attending all day concerts, board games and running with the dog.  After September, my hobbies included gaming for 15 minute intervals, attending short concerts, board games, reading, and walking the dog.  You’ll notice that list includes very, very few things physical and even decreased intensity of non-physical activities.  With rock climbing and gaming out, I needed something to keep my brain busy while my husband played League of Legends for hours without me, and making yarn replicas of Poros was getting a little awkward since my dexterity, as you can imagine, is fairly limited in the right hand in comparison to what it once was.

The solution?  Well, I’d spent the beginning of the year teaching my students basic programming as a way to bridge their disinterest in math and their struggle with accuracy and truth in math (read: they got pissed off if they solved a problem incorrectly, and pissed off for some of my students means very violent.  Long term, we’re working on realizing that learning comes from hard work and if mistakes are made it’s more important to realize why to grow from them, but short term safety is a priority.).  There’s something pretty amazing to just about anyone about making your own multiplication table for the first time using a nested while loop.  I had never really gone beyond the basics myself, however, and so I decided to take the time I once spent gaming, making winter hats, feeding myself, and exercising to learn a new skill.  My physical therapist, 3 months after the incident, put me at another 8-12 weeks away from being in reasonable shape, so that should give me plenty of time to put all my extra energy into healing and coding.  This blog is to keep a record of my progress in both.  So!  Without further ado…

12/19/2014 – I can make a multiplication chart in C++, and when the physical therapist moves my shoulder into the right position I can hold it where it belongs for about 10 seconds without using compensatory muscles.  I’m still pretty certain I’m doing many of my home exercises with my shoulder in the incorrect location, but I am trying my best to get it there!

To start, I’m using a few resources from to learn C++ to remember the basics of coding again, and then I’ll start accessing web design since I have an old domain I’ve owned since high school that could use some serious TLC and purpose again.

For anyone curious, here’s how to make a multiplication chart with C++ (or at least how I’ve been doing it!):

for (int i = 1; i <= 12; i++)
for (int j = 1; j <= 12; j++)
cout << i * j;

cout << endl;


Any recommendations are much appreciated!  I would love any suggestions about books about algorithms.


Lady Liu

From Special Ed Teacher to Technophile