Error-correcting loop in C++, find specific chars in a string and flag as bad input
<p>Here is v1.0 of the binary_to_decimal converter I wrote.  I want to make several changes as I keep improving the spec.  Classes and pointers will be added as well in the future.  Just to keep me fresh and well practiced.  </p>

<p>Well, I now want to implement an error-correcting loop that will flag any character that is not a 0 or a 1 and ask for input again.</p>

<p>I have been trying something along the line of this code block that worked with an array.
It might be way off but I think I can tweak it.  I am still learning 0_0</p>

<p>I want to add something like this:</p>

<pre><code>    while ((cin &gt;&gt; strint).get())
        {
            cin.clear();  //reset the input
            while (cin.get() != '\n')  //clear all the way to the newline char
                continue; //
            cout &lt;&lt; "Enter zeroes and/or ones only! \n";
        }
</code></pre>

<p>Here is the final code without the error-correcting loop:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;cmath&gt;
#include &lt;string&gt;
using namespace std;
const int MAX = 100;

int conv(int z[MAX], int l[MAX], int a);

int main()
{
    int zelda[MAX];
    int link[MAX];
    string strint;
    int am;

    cout &lt;&lt; "Enter a binary number: \n";
    (cin &gt;&gt; strint).get();  //add error-correction to only read 0s and 1s.
    am = strint.size();
    cout &lt;&lt; am &lt;&lt; " digits entered." &lt;&lt; endl;

    int i = 0;
    int p = 0;
    while (i &lt; am)
    {
        zelda[i] = strint[p] - '0';     //copies the string array elements into the int array; essentially STRING TO INT (the minus FORCES a conversion because it is arithmetic) &lt;---- EXTREMELY CLEVER!
        ++i;
        ++p;
    }   

    cout &lt;&lt; conv(zelda, link, am);

    cin.get();
    return  0;
}


int conv(int zelda[MAX], int link[MAX], int length)
{   
    int sum = 0;
    for (int t = 0; t &lt; length; t++)
    {
        long int h, i;
        for (int h = length - 1, i = 0; h &gt;= 0; --h, ++i)
            if (zelda[t] == 1)
                link[h] = pow(2.0, i);
            else
                link[h] = 0;
            sum += link[t]; 
    }
    return sum;
}
</code></pre>

<p>thanks guys.</p>
