reneeccski
12-16-2004, 02:33 AM
I'm having some trouble with the C program I'm writing for my intro CS course. The assignment has two parts and this is the second part. I am to take some decimal op codes from a "Little Man" computer program and convert them to binary. The op codes look like this (I have them stored so the op code and the storage spaces are on separate lines):
5 (op code)
90 (storage space)
6
27
The code I have is below. The problem is that my output file (LMC.obj) is printing only this:
00000000
00000000
etc.
The solution is supposed to be in 8 bit binary, so I think I'm close at least. Now all I need are some 1's! One of my classmates did his assignment exactly the same way as me and his works. Neither of us could figure out where mine is messed up.
Here is my code:
#include <stdio.h>
#include <string.h>
int main() {
FILE *infile;
FILE *outfile;
int i;
int d;
char q[20], r[20];
i=0;
infile = fopen("LMCdec.dat", "r");
outfile = fopen("LMC.obj", "w");
while (NULL != fgets(q, 20, infile))
{
q[strlen(q)-1]='\0';
sscanf(q, "%d", &d);
if(i >= 128)
{
r[0]='1';
i=i-128;
}
else r[0]='0';
if(i >= 64)
{
r[1]='1';
i=i-64;
}
else r[1]='0';
if(i >= 32)
{
r[2]='1';
i=i-32;
}
else r[2]='0';
if(i >= 16)
{
r[3]='1';
i=i-16;
}
else r[3]='0';
if(i >= 8)
{
r[4]='1';
i=i-8;
}
else r[4]='0';
if(i >= 4)
{
r[5]='1';
i=i-4;
}
else r[5]='0';
if(i >= 2)
{
r[6]='1';
i=i-2;
}
else r[6]='0';
if(i >= 1)
{
r[7]='1';
i=i-1;
}
else r[7]='0';
if(i >= 0)
{
r[8]='\0';
i=i-0;
}
fprintf(outfile, "%s\n", r);
} /*end while*/
fclose(infile); fclose(outfile);
return 0;
} /*end main*/
The program compiles fine and runs fine, just with incorrect output. Can someone please take a look? Thanks!
5 (op code)
90 (storage space)
6
27
The code I have is below. The problem is that my output file (LMC.obj) is printing only this:
00000000
00000000
etc.
The solution is supposed to be in 8 bit binary, so I think I'm close at least. Now all I need are some 1's! One of my classmates did his assignment exactly the same way as me and his works. Neither of us could figure out where mine is messed up.
Here is my code:
#include <stdio.h>
#include <string.h>
int main() {
FILE *infile;
FILE *outfile;
int i;
int d;
char q[20], r[20];
i=0;
infile = fopen("LMCdec.dat", "r");
outfile = fopen("LMC.obj", "w");
while (NULL != fgets(q, 20, infile))
{
q[strlen(q)-1]='\0';
sscanf(q, "%d", &d);
if(i >= 128)
{
r[0]='1';
i=i-128;
}
else r[0]='0';
if(i >= 64)
{
r[1]='1';
i=i-64;
}
else r[1]='0';
if(i >= 32)
{
r[2]='1';
i=i-32;
}
else r[2]='0';
if(i >= 16)
{
r[3]='1';
i=i-16;
}
else r[3]='0';
if(i >= 8)
{
r[4]='1';
i=i-8;
}
else r[4]='0';
if(i >= 4)
{
r[5]='1';
i=i-4;
}
else r[5]='0';
if(i >= 2)
{
r[6]='1';
i=i-2;
}
else r[6]='0';
if(i >= 1)
{
r[7]='1';
i=i-1;
}
else r[7]='0';
if(i >= 0)
{
r[8]='\0';
i=i-0;
}
fprintf(outfile, "%s\n", r);
} /*end while*/
fclose(infile); fclose(outfile);
return 0;
} /*end main*/
The program compiles fine and runs fine, just with incorrect output. Can someone please take a look? Thanks!