Ryan Fisher
Ryan Fisher

Reputation: 35

Cant find my logical error in a function

For some reason, my code is not iterating past checking for "and". Why would it not pick up the rest of my literals?

NOTE: I am calling this function for another function. This is working fine only posting this for reference:

int readFile(FILE *file, struct assem *item) 
{
size_t i =0;
//this is sort of the constructor, if you will. 
item->counter = 0;
//breaks down text file into line by line 
size_t maxLines = sizeof item->myLines / sizeof *item->myLines;
//and stores them into "myLines array"
while(i < maxLines && fgets(item->myString, sizeof item->myString, file))
    {
        item->myLines[i] = strdup(item->myString);
        item->myLines2[i] = strdup(item->myString);
        i++;
        item->counter++;
    }   
return 0;
}

int secondCheck(struct assem item)
{
for (int s = 0; s < item.counter; s++)
    {
        //Rformat check
        if((strcmp(item.myWord[s],"add")==0)||(strcmp(item.myWord[s],"srl")==0)||
           (strcmp(item.myWord[s],"and")==0)||(strcmp(item.myWord[s],"sub")==0)||
           (strcmp(item.myWord[s],"slt")==0)||(strcmp(item.myWord[s],"nor")==0)||
           (strcmp(item.myWord[s],"sll")==0)||(strcmp(item.myWord[s],"subu")==0)||
           (strcmp(item.myWord[s],"sltu")==0)||(strcmp(item.myWord[s],"addu")==0)||
           (strcmp(item.myWord[s],"or")==0))
           {
               checkRformat(item.myLines2[s], &item);
               printf("%s%s%s%s%s%s\n",item.opcode,item.rs,item.rt,item.rd,item.shamt,item.funct);
           }
           else{printf("Womp");}

    }

}

Here is my output:

Splitting string:
add
$t1
$t1
$s6
000000NULLNULLNULL0000010100

This is the part im having issues with.

void checkRformat(char *LineArray, struct assem *item)
{
    int i = 0;
    char *str;
    char *temp;
    item->opcode = "NULL";
    item->shamt = "NULL";
    item->funct = "NULL";
    item->rs = "NULL";
    item->rt = "NULL";
    item->rd = "NULL";

            printf("Splitting string:\n");
            temp = strdup(LineArray);
            str = strtok(temp, " ,.-#\n\t");
    //this helper function gets called by another.It primarily serves to convert the MIPS code
    //instructions or labels into their binary values. These global variables will then concatenated
    // and be printed onto a .txt file. 
        while(str != NULL)
    {
        printf("%s\n", str);
        str = strtok(NULL, " ,.-#\n\t");
        if (!str) 
        {
            break;
        }
        if(strcmp(str, "add"))
        {
            item->opcode = "000000";
            item->shamt = "00000";
            item->funct = "10100";
        }
        else if(strcmp(str, "$t0"))
        {
            if(strcmp(item->rs, "NULL")){item->rs = "01000";}
            else if(item->rt, "NULL"){item->rt = "01000";}
            else if(item->rd,"NULL"){item->rd = "01000";}
        }
        else if(strcmp(str, "$t1"))
        {
            printf("\nSuccess\n");
            if(strcmp(item->rs,"NULL")){item->rs = "01001";}
            else if(item->rt,"NULL"){item->rt = "01001";}
            else if(item->rd,"NULL"){item->rd = "01001";}
        }
        else if(strcmp(str, "$s6"))
        {
            if(strcmp(item->rs,"NULL")){item->rs = "11110";}
            else if(item->rt,"NULL"){item->rt = "11110";}
            else if(item->rd,"NULL"){item->rd = "11110";}
        }
    }
}

Upvotes: 0

Views: 66

Answers (1)

merridius
merridius

Reputation: 61

strcmp returns 0 if the strings match.

Thus all your conditionals do not evaluate.

Upvotes: 1

Related Questions