Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Sep 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    C program...very frustrated

    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:

    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!

  • #2
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You are reading in the number into the variable "d":
    Code:
    sscanf(q, "%d", &d);
    but doing calculations based on the variable "i":
    Code:
    if(i >= 128)
    "i" never gets any input so it's always 0.

    shmoove

  • #3
    New Coder
    Join Date
    Sep 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thanks!

    Thanks! I knew it was something stupid that I wasn't catching. You know how it is when you keep looking at the same lines of code over and over and over again.....Sometimes you just need a fresh pair of eyes to figure out what's wrong. It works fine now and I'm very happy because that is the hard part of the project.

  • #4
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    While you're at it, this piece of code:
    Code:
    	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; 
    	}
    is just screaming out for a loop...

    shmoove

  • #5
    New Coder
    Join Date
    Sep 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks. I know the code is ugly, but I don't think I want to mess it up at this point as long as it works. I do have another problem, however. Everything works fine, except for some reason in the first part of the program (changing mnemonic Little Man code to numeric op codes) it's not printing a 0 for the "COB" (coffee break or halt) instruction at the end of the program. I swear its the stupid Solaris text editor voodo at work again! I wrote a program to convert a number from octal, decimal, or hex to one of the other number systems and somehow the editor put some invisible characters in the program and made it not work. My instructor did something goofy and fixed it up. But maybe I'm wrong and I screwed up somewhere. Anyway, here's the code. I probably didn't need to include the whole program, but I wanted to make sure you had all the info you needed. The line of interest is this one:

    if(0==strcmp(out, "COB"))...

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    
    FILE *infile;      
    FILE *outfile;     
    int j;             
    char a[20], out[20];
    	
    infile = fopen("LMC.dat", "r");    
    outfile = fopen("LMCdec.dat", "w");
    
    while (NULL != fgets(a, 20, infile))
    	{
    	a[strlen(a)-1]='\0';
    	
    	sscanf(a, "%s %d", out, &j);
    	
    	if(0==strcmp(out, "LDA"))
    		{
    		fprintf(outfile, "%s\n%d\n", "5", j);
    		}
    	if(0==strcmp(out, "STO"))
    		{
    		fprintf(outfile, "%s\n%d\n", "3", j);
    		}
    	if(0==strcmp(out, "ADD"))
    		{
    		fprintf(outfile, "%s\n%d\n", "1", j);
    		}
    	if(0==strcmp(out, "SUB"))
    		{
    		fprintf(outfile, "%s\n%d\n", "2", j);
    		}
    	if(0==strcmp(out, "IN"))
    		{
    		fprintf(outfile, "%s\n", "9\n01", j);
    		}
    	if(0==strcmp(out, "OUT"))
    		{
    		fprintf(outfile, "%s\n", "9\n02", j);
    		}
    	if(0==strcmp(out, "BRZ"))
    		{
    		fprintf(outfile, "%s\n%d\n", "7", j);
    		}
    	if(0==strcmp(out, "BRP"))
    		{
    		fprintf(outfile, "%s\n%d\n", "8", j);
    		}
    	if(0==strcmp(out, "BR"))
    		{
    		fprintf(outfile, "%s\n%d\n", "6", j);
    		}
    	if(0==strcmp(out, "COB"))
    		{
    		fprintf(outfile, "%s\n%d\n", "0", j);
    		}
    	if(0==strcmp(out, "MUL"))
    		{
    		fprintf(outfile, "%s\n%d\n", "11", j);
    		}
    	if(0==strcmp(out, "EXP")) 
    		{
    		fprintf(outfile, "%s\n%d\n", "12", j);
    		}
    		
    	} /*end while*/
    	
    fclose(infile);
    fclose(outfile);
    
    return 0;
    
    FILE *infile1;      
    FILE *outfile1;             
    char q[20], r[20];
    int i;
    i=0;
    	
    infile1 = fopen("LMCdec.dat", "r");    
    outfile1 = fopen("LMC.obj", "w");
    
    
    
    while (NULL != fgets(q, 20, infile1))
        {
        q[strlen(q)-1]='\0';
        sscanf(q, "%d", &i);
        
        	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;
    		}
    	else  r[8]='0';
    		
    	fprintf(outfile1, "%s\n", r);
    		
    } /*end while*/
    
    fclose(infile1);
    fclose(outfile1);
    
    return 0;
    
    } /*end main*/
    Last edited by reneeccski; 12-20-2004 at 04:17 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •