Sunday, August 2, 2009

Why is output wrong in this C++ program?

Here's the source code:





#include %26lt;iostream.h%26gt;


#include %26lt;math.h%26gt;





int main ()


{


int input, i, j, k, bin, oct, counter, power, hex_dig1, hex_dig2;


char ans = 'y', hex_letter;





do


{


bin = 0;


oct = 0;


counter = 1;


i = 1;


j = 0;


power = 0;





system ("cls");





cout %26lt;%26lt; "Enter an integer number [1-50]: ";


cin %26gt;%26gt; input;


cout %26lt;%26lt; endl %26lt;%26lt; "Decimal Binary Octal Hexadecimal" %26lt;%26lt; endl;


cout %26lt;%26lt; "------- ------ ----- -----------" %26lt;%26lt; endl;





while (counter %26lt;= input)


{


cout.width (10);


cout.setf (ios::left);


i = counter;


cout %26lt;%26lt; i;


k = i;


bin = 0;


power = 0;





while (k %26gt; 0)


{


j = k % 2;


k = k / 2;


bin = bin + (pow(10,power) * j);


power = power + 1;


}





cout %26lt;%26lt; bin;





cout.width (10);


cout.setf (ios::left);


k = i;


oct = 0;


power = 0;





while (k %26gt; 0)


{


j = k % 8;


k = k / 8;


oct = oct + (pow(10,power) * j);


power = power + 1;


}





cout %26lt;%26lt; oct;





cout.width (10);

Why is output wrong in this C++ program?
You don't say what the output is supposed to be.





BTW, this looks suspiciously like a homework question. Have you tried anything yourself? If so, what?


No comments:

Post a Comment