C++ different output

Why do I get a different output in C++?


[code]
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{ clrscr();
int m,i=0,a,b[100],c,d[100];
char e='y';
cout<<"\nProgram to convert decimal number into binary number";
loop:if(e=='y'||e=='Y')
{
cout<<"\nEnter a number:";
cin>>a;
do
{
b[i]=a%2;
c=a/2;
a=c;
b[i];
m=i;
i++;
}while(a>=2);
cout<<"\nAnswer:";
cout<<c;
for(i=m;i>=0;i--)
{ cout<<b[i];
}
cout<<"\nDo you want to continue:y/Y or n/N:";
cin>>e;
goto loop;
}
else
{ cout<<"\nThank you for using the decimal to binary converter in C++";
}
getch();
}
[\code]

I did this program on my own to find the binary number. When I give input as 9, the program runs as intended. But, when I enter 09, the program doesn't seem to go well. Please Help me.Thanks.
Last edited on
the problem occurs only after/at the second input.
if your first input is 09 it's running just as expected.
I think you forget to print the last digit everytime after the first input.
closed account (E0p9LyTq)
The variable i needs to be set/reset to 0 (zero) if continuing to convert numbers.
In C++, An integer literal that begins with 0 is considered as an Octal integer literal

Edit:
// Your code looks a bit messy and off standard, i will advice you:
* Use <string> instead of <string.h>
* use <iostream> instead of <iostream.h>
* Main function returns int NOT void
* Use loops instead of goto
* Make your variable names more descriptive
* Learn proper code Formatting.
Last edited on
In C++, An integer literal that begins with 0 is considered as an Octal integer literal
yeah but that is not the problem.
If you put the string 09 in a stream and read it in an integer the standard operation is to read it as if the number was in base 10.
(you could set it to be of base 2, 8 or 16 if you really want)
Last edited on
Topic archived. No new replies allowed.