0

I'm trying to make this program slightly better in variations. I have this program that converts from binary to decimal. How to I change it so that it continually asks the user for the correct code

This is the code:

while (nextDigit != '\n') {
    while (nextDigit != '0' && nextDigit != '1') 
    {
        cout << "Invalid Binary Entry. Enter again: " << endl;
        cin >> nextDigit;
    }
    decimalValue = ((decimalValue * 2) + nextDigit - 48);
    cin >> nextDigit;
}

cout << "Decimal value is: " << decimalValue << endl;

The problem is, it displays the error message multiple times, not once. It even displays more error messages the longer the incorrect input string is. How can I fix this?

Thank you.

4
  • I'm pressing enter after I write the digits right? Sorry it is not clear to me the hint. Jan 31, 2015 at 22:53
  • Yes that is exactly what you're doing. I take it if someone enters a string of digits that contain anything besides 1 or 0 you want to ignore the whole input and recycle, correct ?
    – WhozCraig
    Jan 31, 2015 at 22:54
  • Yes. I've seen now a previous thread talking about something known as cin.clear() and cin.ignore(10000). If this is where I must be focusing on I should find a different way, it looks to me that this is too advanced for now. From what I understood in another thread, I should implement those two "functions" inside my loop, so it will clear what cin previous had. Is it possible for another hint? I think I cannot use this though, as I cannot understand what it means. Ah well. I will keep at it until it works. Jan 31, 2015 at 22:58
  • Its not that bad. honestly. If I get a chance I'll whip something up, but more than likely someone else already has a solution in the works.
    – WhozCraig
    Jan 31, 2015 at 22:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.