-3

My program will compile, but when I run the exe, there is no output and the program does not terminate.

I've tried to find the line of code that might be causing the problem by printing "flags;" however, none of my flags are printed, even the flag that is the first statement in main. The program will execute fine if the function populateLinkedList is not called.

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

//Declare Node
struct TransactionNode{
    char id[11];
    char accountDebit[11];
    char accountCredit[11];
    char debit[11];
    char credit[11];
    char date[11];
    char message[101];

    struct TransactionNode *nextPtr; // pointer to next node
};

//Rename Node and Node Pointer
typedef struct TransactionNode Node; //ListNode is synonym for struct listNode
typedef Node *NodePtr; //ListNodePtr is a pointer to ListNode

//Declare starting transaction node
NodePtr tSPtr = NULL;

//prototypes
void writeLinkedList(NodePtr sPtr);
void populateLinkedList(void);

int main(){
    printf("hello");
    writeLinkedList(tSPtr);
    populateLinkedList(); 
    writeLinkedList(tSPtr);
    return 0;
}

void writeLinkedList(NodePtr sPtr){
    FILE *fPtr;
    NodePtr currentPtr;
    currentPtr = sPtr; //assign currentPtr to startingPtr
    fPtr = fopen("records.dat", "w");

    while(currentPtr != NULL){//while currentPtr exists
        //write structure
        fprintf(fPtr, "%s;%s;%s;%s;%s;%s;%s;", currentPtr -> id, currentPtr -> accountDebit, 
        currentPtr -> accountCredit, currentPtr -> debit, currentPtr -> credit, currentPtr -> date, currentPtr ->message);
        //walk... 
        currentPtr = currentPtr -> nextPtr;
    }//end while

    fclose(fPtr);
}

void populateLinkedList(void){
    FILE *fPtr;
    NodePtr currentPtr;
    NodePtr previousPtr;
    char temp[101];
    //check to see if file can be opened
    if (( fPtr = fopen("records.dat", "r")) == NULL){
        printf("populateLinkedList error: Cannot open file"); //error
        return;
    } //end if

    //create first node
    currentPtr = malloc(sizeof(Node));
    if(currentPtr == NULL){
        fclose(fPtr);
        printf("populateLinkedList error: Not enough memory");
        return;
    }//end if
    else tSPtr = currentPtr; //end else: assign first node to sPtr

    //FOR FIRST TRANSACTION:
    //flush and scan and assign id
    fflush(stdin);
    fscanf(fPtr, "%10[^;]", temp);
    printf("%s", temp);
    if(temp[0] == EOF){
        fflush(stdin);
        fclose(fPtr);
        return;
    }; //end if: if EOF is found break function
    strcpy(currentPtr -> id, temp);
    //flush and scan and assign debit account id
    fflush(stdin);
    fscanf(fPtr, "%10[^;]", temp);
    strcpy(currentPtr -> accountDebit, temp);
    //flush and scan and assign credit account id
    fflush(stdin);
    fscanf(fPtr, "%10[^;]", temp);
    strcpy(currentPtr -> accountCredit, temp);
    //flush and scan and assign debit amount
    fflush(stdin);
    fscanf(fPtr, "%10[^;]", temp);
    strcpy(currentPtr -> debit, temp);
    //flush and scan and assign credit amount
    fflush(stdin);
    fscanf(fPtr, "%10[^;]", temp);
    strcpy(currentPtr -> credit, temp);
    //flush and scan and assign date
    fflush(stdin);
    fscanf(fPtr, "%10[^;]", temp);
    strcpy(currentPtr -> date, temp);
    //flush and scan and assign message
    fflush(stdin);
    fscanf(fPtr, "%100[^;]", temp);
    strcpy(currentPtr -> credit, temp);
    //assign nextPtr
    currentPtr -> nextPtr = NULL;

    //assign currentPtr to previousPtr
    previousPtr = currentPtr;

    while(temp[0]!= EOF){

        //create first node
        currentPtr = malloc(sizeof(Node));
        if(currentPtr == NULL) break;

        //flush and scan and assign id
        fflush(stdin);
        fscanf(fPtr, "%10[^;]", temp);
        if(temp[0] == EOF) break; //end if: if EOF is found break function
        strcpy(currentPtr -> id, temp);
        //flush and scan and assign debit account id
        fflush(stdin);
        fscanf(fPtr, "%10[^;]", temp);
        strcpy(currentPtr -> accountDebit, temp);
        //flush and scan and assign credit account id
        fflush(stdin);
        fscanf(fPtr, "%10[^;]", temp);
        strcpy(currentPtr -> accountCredit, temp);
        //flush and scan and assign debit amount
        fflush(stdin);
        fscanf(fPtr, "%10[^;]", temp);
        strcpy(currentPtr -> debit, temp);
        //flush and scan and assign credit amount
        fflush(stdin);
        fscanf(fPtr, "%10[^;]", temp);
        strcpy(currentPtr -> credit, temp);
        //flush and scan and assign date
        fflush(stdin);
        fscanf(fPtr, "%10[^;]", temp);
        strcpy(currentPtr -> date, temp);
        //flush and scan and assign message
        fflush(stdin);
        fscanf(fPtr, "%100[^;]", temp);
        strcpy(currentPtr -> credit, temp);
        //assign nextPtr
        currentPtr -> nextPtr = NULL;

        //assign currentPtr to previousPtr
        previousPtr = currentPtr;

        //assign currentPtr to previousPtr -> nextPtr
        previousPtr -> nextPtr = currentPtr;
    }//end while

    fflush(stdin);
    fclose(fPtr);
    return;
}



I expect the file to remain the same, the flags be printed, and the program to terminate; however, the flags are not printed and the program does not terminate.
Justin
  • 24,288
  • 12
  • 92
  • 142

1 Answers1

0

in populateLinkedList you create a circle in memory :

previousPtr = currentPtr;

//assign currentPtr to previousPtr -> nextPtr
previousPtr -> nextPtr = currentPtr;

after that currentPtr-> nextPtr == currentPtr, if you try to go through your list your program loops indefinitely, a circle has no end

The lines must be exchanged :

//assign currentPtr to previousPtr -> nextPtr
previousPtr -> nextPtr = currentPtr;

previousPtr = currentPtr;
bruno
  • 32,421
  • 7
  • 25
  • 37