samedi 19 janvier 2019

If Statement not working correctly (DFA) C program

I am trying to make a DFA that takes states as a linked list. My if statement if(answerStepTwo == 'z') is not working correctly. I have added a debugging printf("Should have been inserted"); When I run my program it does print should have been inserted but the insertStates(answerStepTwo); right after never happens. If I take out the if statement if(answerStepTwo == 'z') the insertStates(answerStepTwo); works like it is supposed to.

This is with the if statement. This linked list is empty but should not be because the printf statement ran saying "should have been inserted." enter image description here This is with the if statement commented out. Runs like it is supposed to but I do not want the z character added to the linked list. enter image description here

#include <stdio.h>
#include <stdlib.h>

struct node *head = NULL;
struct node *current = NULL;

//structure for the linked list
typedef struct node 
{ 
    int data;
    struct node * next;
}NODE_T; 

void insertStates(char data)
{
    struct node *connect = (struct node*)malloc(sizeof(struct node));
    connect->data = data;
    connect->next = head;
    head = connect; 
}

void viewList()
{
    struct node *pointer;
    printf("\n[head] =>");

while(pointer != NULL)
{
    printf("%c =>", pointer->data);
    pointer = pointer->next;
}
printf(" [null]\n");
};

int main ()
{
    int answerStepOne;
    char answerStepTwo;
    char userContinue;
    int i;
    int statesTot;

    printf("***Pick an option.*** \n1. Create a new DFA. \n2. Load DFA from file.");
    scanf("%d", &answerStepOne);
    if (answerStepOne == 1)
    {
        //Create new DFA    
        printf("Enter single alphabetical states excluding 'z'. (Max states 25)(z to quit)\n");
        i=1;
        while(answerStepTwo != 'z')
        {
            printf("Enter State[%d]: ",i);
            scanf("%s", &answerStepTwo);

        if(answerStepTwo == 'z')
        {
            break;
        }
        else
        {
            printf("Should have been inserted\n");
            insertStates(answerStepTwo);
            i++;
        }
    }
}
if(answerStepOne == 2)
{
    //Load DFA from file
}

viewList();
system ("PAUSE");
return 0;

}

Aucun commentaire:

Enregistrer un commentaire