LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Blogs > peonuser
User Name
Password

Notices


Rate this Entry

C conversion.

Posted 12-22-2014 at 01:44 AM by peonuser

The main thing we did was to add a substitution for get(ch) and clrscr();

Original code:
#include<stdio.h>
void main()
{
int n,d=0,j,a[9];
clrscr();
printf("Enter the Integer which u want to Convert Decimal to Binary : ");
scanf("%d",&n);
while(n>0)
{
a[d]=n%2;
n=n/2;
d++;
}
printf("After Converting Decimal to Binary is : ");
for(j=d-1;j>=0;j--)
printf("%d",a[j]);
getch();
}

New code:
#include<stdio.h>
#include <unistd.h>
#include <termios.h>
char getch(){
char buf=0;
struct termios old={0};
fflush(stdout);
if(tcgetattr(0, &old)<0)
perror("tcsetattr()");
old.c_lflag&=~ICANON;
old.c_lflag&=~ECHO;
old.c_cc[VMIN]=1;
old.c_cc[VTIME]=0;
if(tcsetattr(0, TCSANOW, &old)<0)
perror("tcsetattr ICANON");
if(read(0,&buf,1)<0)
perror("read()");
old.c_lflag|=ICANON;
old.c_lflag|=ECHO;
if(tcsetattr(0, TCSADRAIN, &old)<0)
perror ("tcsetattr ~ICANON");
printf("%c\n",buf);
return buf;
}
void main()
{
int n,d=0,j,a[9];
// clrscr();
system("clear");
printf("Enter the Integer which u want to Convert Decimal to Binary : ");
scanf("%d",&n);
while(n>0)
{
a[d]=n%2;
n=n/2;
d++;
}
printf("After Converting Decimal to Binary is : ");
for(j=d-1;j>=0;j--)
printf("%d",a[j]);
getch();
}
Posted in Uncategorized
Views 950 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 03:22 AM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration