0

What I am trying to do is, create a program which converts Decimal values into other Number Systems(Binary, Octal and Hexa-Decimal) and shows the original Decimal value as-well-as the converted value.

It runs fine in first run but when the outer for loop runs the program again, the previous values which were printed by inner for loop are also printed right after its output in the next run. Any Suggestions?

Try running it, and see it yourself... source code:-

#include <iostream>
using namespace std;
int main()
{
        cout << "This program will help in Decimal to other radix conversion :-"  << endl;
        const int i = 10;
        //Declaring array to store remainder values...
        int RemArray [i] = {0};
        //Declaring variable to use it as index of RemArray[]...
        short int R=0;
        char Exit = '\0';
    //To iterate the whole program...
        for (;;)
        {
                cout << "Enter Decimal digit: ";
                long long int Decimal = 0;
                cin >> Decimal;
                long long int TempDecimal = Decimal;
                cout << "Enter Desired Radix: ";
                int Radix = 0;
                cin >> Radix;
                long long int Quotient = 1;
                long long int Remainder = 0;
                cout << endl;
                while (Quotient != 0)
                {
                        //Formula for conversion...
                        Quotient = Decimal / Radix;
                        Remainder = Decimal % Radix;
                        cout << "Quotient: " << Quotient << "  <->  " << "Remainder: ";
                        //Using switch case so that hexa-decimal values could also be printed...
                        switch (Remainder)
                        {
                                case 10:
                                        cout << "A" << endl;
                                        break;
                                case 11:
                                        cout << "B" << endl;
                                        break;
                                case 12:
                                        cout << "C" << endl;
                                        break;
                                case 13:
                                        cout << "D" << endl;
                                        break;
                                case 14:
                                        cout << "E" << endl;
                                        break;
                                case 15:
                                        cout << "F" << endl;
                                        break;
                                default:
                                        cout << Remainder << endl;
                                        break;
                        }
                        Decimal = Quotient;
                        //Using array to store remainder values...to print them all together
                        RemArray [R] = Remainder;
                        R++;
                }
                cout << endl;
                cout << "Therefore, " << TempDecimal << " = ";
        /*Output the result...But When the program runs again, values obtained here remains
        on the screen and prints them too on the next run of this loop...*/
                for (int NewR = R-1, z=0; (NewR >= 0) && (z < R); NewR--,z++)
                {
                        switch (RemArray [NewR])
                        {
                                case 10:
                                        cout << "A";
                                        break;
                                case 11:
                                        cout << "B";
                                        break;
                                case 12:
                                        cout << "C";
                                        break;
                                case 13:
                                        cout << "D";
                                        break;
                                case 14:
                                        cout << "E";
                                        break;
                                case 15:
                                        cout << "F";
                                        break;
                                default:
                                        cout << RemArray [NewR];
                                        break;
                        }
                }
                //Asking user to iterate the program again...
                cout << endl << "Want to convert another digit (y/n): ";
                cin >> Exit;
                cout << endl;
                if (Exit == 'n')
                {
                        break;
                }
        }
        cout << "GoodBye !!!" << endl;
        return 0;
}
Jester
  • 1
  • 3
  • Please read [How To Debug Small Programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – alter_igel Mar 07 '21 at 04:56
  • 1
    What you're doing is silly. A number is a number. "Octal" vs. "Hex" vs. "Decimal" is a matter of *PRESENTATION*. The binary *VALUE* is always the same. So why just use the standard ios format specifiers, e.g. [ios::hex](http://www.cplusplus.com/reference/ios/hex/) and friends? – paulsm4 Mar 07 '21 at 05:02
  • @paulsm4 well that's helpfull....But still want the code to run perfectly as matter of college assignments I made it using loops and switch-case and now I can't find the bug. – Jester Mar 07 '21 at 05:21
  • 1
    As usual in these questions, the only decimal to binary conversion happens here: `cin >> Decimal;`; which means *inter alia* that the `Decimal` variable is misnamed. It is already binary at this point. – user207421 Mar 07 '21 at 08:47
  • Somebody out there seems to be mis-teaching this topic. This confusion arises all the time. There are hundreds of questions here exhibiting this confusion. – user207421 Mar 09 '21 at 03:36

0 Answers0