Where's Waldo?

Discussion in 'Software' started by longbowrocks, Mar 25, 2010.

  1. longbowrocks

    longbowrocks Private E-2

    Alright, I know what my problem is, I just need to find the source of it: on the final line of check() (the bottom function), I call addVal(). Upon stepping into addVal(), I find that *root = fp. Here's my code, I hope someone can help me find my error.
    P.S. before the function call to addVal(), root is fine.

    PRIMER.C

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

    void main(){
    struct node** primes;
    int current;

    FILE* fp;
    fp = fopen("primes.txt", "r+");
    primes = NULL;
    current = 3;

    if(fp == NULL){
    printf("No saved progress found.\nTime to kick *** and take primes.\n");
    fp = fopen("primes.txt", "w+");
    primes = addVal(fp, 2, primes);
    }
    else{
    printf("Saved progress found.\nLoading file to continue previous progress.\n");
    initList(primes, fp);
    }

    while(1){
    primes = check(current, primes, fp);
    current += 2;
    }

    fclose(fp);
    return;
    }

    //add a value to both the array and the file
    struct node** addVal(FILE* fp, int value, struct node** root){
    fprintf(fp, "%d\n", value);
    return insert(value, root);
    }

    //insert a value into the list
    struct node** insert(int value, struct node** root){
    struct node* newNode;
    newNode = (struct node *)malloc(sizeof(struct node));
    newNode->val = value;
    newNode->next = NULL;
    if(root == NULL){
    root = &newNode;
    }else if(*root == NULL){
    *root = newNode;
    }else{
    newNode->next = *root;
    *root = newNode;
    }
    return root;
    }

    //set up the prime list and read all primes from file to the list
    struct node** initList(struct node** root, FILE* fp){
    int next;
    int i;

    while(!EOF){
    fscanf(fp, "%d", &next);
    root = insert(next, root);
    }
    return root;
    }

    //check to see if the integer is a prime. If so, add it to the file and list.
    struct node** check(int value, struct node** root, FILE* fp){
    struct node* currentNode;
    currentNode = NULL;
    if(root == NULL || *root == NULL){
    printf("error: NULL pointer where there can't be one. in check method\n");
    //exit(0);
    }else{
    currentNode = *root;
    }

    while(currentNode != NULL){
    if(value%(currentNode->val) == 0){
    return root;
    }
    currentNode = currentNode->next;
    }

    return addVal(fp, value, root);
    }



    PRIMER.H

    struct node** addVal(FILE* fp, int value, struct node** root);
    struct node** insert(int value, struct node** root);
    struct node** initList(struct node** root, FILE* fp);
    /*function: check()
    *precondition: root is nonempty
    */
    struct node** check(int value, struct node** root, FILE* fp);
    struct node{
    int val;
    struct node* next;
    };
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds