CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 3 of 4 FirstFirst 1234 LastLast
Results 31 to 45 of 52
  1. #31
    Join Date
    Feb 2012
    Posts
    68

    Re: Structure Linked Lists: Searching for Words.

    yeah i figured initialize the node and then return that pointer to the insert function which I was going to have called elsewhere. the current title "findorinsert" is kind of misleading buttttt im going keep there to not get docked hah.

  2. #32
    Join Date
    Feb 2012
    Posts
    68

    Re: Structure Linked Lists: Searching for Words.

    the sPtr = find(name); is causing the warning: passing arguement 1 of 'find' from incompatible pointer type.is there a work around? .

    Also there is too few arguements in my find function. should i take the ptr out of the brackets to fix it?
    Last edited by chucker; April 9th, 2012 at 03:01 PM.

  3. #33
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Structure Linked Lists: Searching for Words.

    Quote Originally Posted by chucker View Post
    the sPtr = find(name); is causing the warning: passing arguement 1 of 'find' from incompatible pointer type.is there a work around? .

    Also there is too few arguements in my find function. should i take the ptr out of the brackets to fix it?
    We don't have your current code. I don't get that error when I try to compile the code in your first post. But, look at what you're passing and what type of argument your function takes and ask yourself which, if either, is correct.
    Last edited by GCDEF; April 9th, 2012 at 03:05 PM.

  4. #34
    Join Date
    Feb 2012
    Posts
    68

    Re: Structure Linked Lists: Searching for Words.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define LINEBUFFERSIZE 256
    
    /* Definitions for data structure nodes (artist, disc, track)  */
    
    struct song
    {
        char *songName_p;
        int trackNumber;
        struct song *nextSong_p;
    };
    
    
    struct disc
    {
        char *discName_p;
        int year;
        struct song *song_p;
        struct disc *nextDisc_p;
    
    };
    
    struct artist
    {
        char name[20];
        char *artistName_p;
        struct disc *disc_p;
        struct artist *nextArtist_p;
    
    };
    struct artist *end = (struct artist *) NULL;   //NEW
    struct artist *startPtr = (struct artist *) NULL; //NEW
    struct artist *find(struct artist *, char * );//NEW
    struct artist *initializenode(char *);//NEW
    
    typedef struct artist artist_t;
    typedef struct disc disc_t;
    typedef struct song song_t;
    
    typedef struct artist *artistNodePtr;
    typedef struct disc *discNodePtr;
    typedef struct song *songNodePtr;
    
    /* function  prototypes */
    
    void InsertArtist(struct artist *New);
    
    artistNodePtr findOrInsertArtist( artistNodePtr *sPtr, char * );
    
    discNodePtr  findOrInsertDisc(discNodePtr *sPtr, char *discID, int releaseYear);
    
    void  findOrInsertSong(songNodePtr *sPtr, char *songID, int trackID);
    
    void getNextLine(char buffer[], int bufferSize, FILE *fptr);
    
    
    
    int main(int argc, char *argv[]) {
        char name[20];
        struct artist *ptr;
        char lineBuffer[LINEBUFFERSIZE];
        artistNodePtr startPtr = NULL; /* initially the artist list is empty */
        FILE *musicFile;
        char *artistTemp, *discTemp, *yearTemp, *trackTemp, *songTemp;
        int year, track, menu = 1;
        artistNodePtr theArtist;
        discNodePtr theDisc;
    
        if (argc==1)
        {
            printf(" Must supply a file name as command line argument/n");
            return 0;
        }
    
        if ((musicFile = fopen(argv[1], "r")) == NULL)
        {
            printf ("Error opening music file.  Program terminated/n");
            return 0;
        }
    
        getNextLine(lineBuffer, LINEBUFFERSIZE, musicFile);
        while (!feof(musicFile))
        {
    
            artistTemp = strtok(lineBuffer,";");
            if (artistTemp == NULL)
            {
                printf("Error parsing input file; Program is terminated\n");
                return 0;
            }
            discTemp = strtok(NULL ,";");
            if (discTemp == NULL)
            {
                printf("Error parsing input file; Program is terminated\n");
                return 0;
            }
            yearTemp  = strtok(NULL ,";");
            if (yearTemp == NULL)
            {
                printf("Error parsing input file; Program is terminated\n");
                return 0;
            }
            trackTemp = strtok(NULL ,";");
            if (trackTemp == NULL)
            {
                printf("Error parsing input file; Program is terminated\n");
                return 0;
            }
            songTemp = strtok(NULL ,"\n");
            if (songTemp == NULL)
            {
                printf("Error parsing input file; Program is terminated\n");
                return 0;
            }
    
            year = atoi(yearTemp);
            track = atoi(trackTemp);
    
            theArtist = findOrInsertArtist(&startPtr, artistTemp);
            theDisc = findOrInsertDisc(&(theArtist->disc_p), discTemp, year);
            findOrInsertSong(&(theDisc->song_p), songTemp, track);
    
            getNextLine(lineBuffer, LINEBUFFERSIZE, musicFile);
        }  /* end of while loop */
    
        while (menu != 0)
        {
            printf("1 to display entire catalog \n");
            printf("2 to display alls songs by a given artist\n");
            printf("3 to display all songs on a given disc\n");
            printf("0 to exit the library\n ");
            scanf("%d", &menu);
            switch (menu)
            {
            case 2: printf("enter an artist name");
                scanf("%s", name);
                ptr = find(startPtr, name);
                if (ptr==NULL)
                {
                    ptr = initializenode(name);
                    InsertArtist(ptr);
                }
            }
        } /* end main */
    }
    artistNodePtr findOrInsertArtist( artistNodePtr *sPtr, char *name ) {
      sPtr = NULL;
      sPtr = find(name);
       if(!sPtr)
          sPtr = initializenode(name);
      return sPtr;
    }
    
    
    struct artist *initializenode(char *name)
    {
      artistNodePtr newNodePtr = (artistNodePtr)malloc(sizeof(artist_t));
      if (newNodePtr != NULL)
      {
         newNodePtr->artistName_p = (char*)malloc((strlen(name)+1)*sizeof(char));
         if (newNodePtr->artistName_p != NULL)
         {
            strcpy(newNodePtr->artistName_p, name);
            newNodePtr->nextArtist_p = NULL;
            newNodePtr->disc_p = NULL;
            return newNodePtr;
         }
      }
    }
    
    void InsertArtist(struct artist *New)
    { //NEW
        struct artist *temp, *prev;
    
        if(startPtr == NULL)
        {
            startPtr = New;
            end = New;
            startPtr->nextArtist_p = NULL;
            return;
        }
        temp = startPtr;
    
        while(strcmp(temp->name, New->name) < 0)
        {
            temp = temp->nextArtist_p;
            if(temp == NULL)
            break;
        }
        if(temp == startPtr)
        {
            New->nextArtist_p = startPtr;
            startPtr = New;
        }
        else
        {
            prev = startPtr;
            while(prev->nextArtist_p != temp)
            {
                prev = prev->nextArtist_p;
            }
            prev->nextArtist_p = New;
            New-> nextArtist_p = temp;
            if(end == prev)
            end = New;
        }
    }
    
    struct artist *find(struct artist *sPtr, char *name)
    { //NEW
    
        while (strcmp(name, sPtr->name )!=0)
        {
            sPtr = sPtr->nextArtist_p;
            if (sPtr == NULL)
                break;
        }
        return sPtr;
    }
    
    
    discNodePtr findOrInsertDisc( discNodePtr *sPtr, char *discID, int releaseYear)
    {
    }
    
    void findOrInsertSong(songNodePtr *sPtr, char *songID, int trackID)
    {
    }
    
    
    void getNextLine(char buffer[], int bufferSize, FILE *fptr)
    {
        char temp;
        int i = 0;
    
        buffer[0] = fgetc(fptr);
        while ( (!feof(fptr)) && (buffer[i] != '\n') &&  i<(bufferSize-1))
        {
            i = i +1;
            buffer[i]=fgetc(fptr);
        }
    
        if ((i == (bufferSize-1)) && (buffer[i] != '\n'))
        {
            temp = fgetc(fptr);
            while (temp != '\n')
            {
                temp = fgetc(fptr);
            }
        }
    
        buffer[i] = '\0';
    }

  5. #35
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Structure Linked Lists: Searching for Words.

    What do you think? Your function takes an artist* and a char*, but you pass a char*. Which do you think is correct?

  6. #36
    Join Date
    Feb 2012
    Posts
    68

    Re: Structure Linked Lists: Searching for Words.

    compiled! Just had to follow more closely to what paul was doing. Im just worried about straying from the original code.

  7. #37
    Join Date
    Feb 2012
    Posts
    68

    Re: Structure Linked Lists: Searching for Words.

    weird tho, when i run absolutely nothing happens :/ wait thats probably because i havent let it access the file yet? **** i know nothing about reading in txt files.

  8. #38
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Structure Linked Lists: Searching for Words.

    Quote Originally Posted by chucker View Post
    compiled! Just had to follow more closely to what paul was doing. Im just worried about straying from the original code.
    Which did you change, the function or the call to it?

  9. #39
    Join Date
    Feb 2012
    Posts
    68

    Re: Structure Linked Lists: Searching for Words.

    took the artistNodePtr *sPtr out of findOrInsertArtist() and instead put it in the body and got rid of the *. The i realized i forgot to put startPtr in the find(name) function, there was supposed to be two things there. Then cleaned up everywhere else that needed adjustments because of the changes.

  10. #40
    Join Date
    Feb 2012
    Posts
    68

    Re: Structure Linked Lists: Searching for Words.

    the code is already there for the file reading stuff, but how do i do the command line arguement in code blocks?
    Last edited by chucker; April 9th, 2012 at 04:45 PM.

  11. #41
    Join Date
    Aug 2009
    Posts
    440

    Re: Structure Linked Lists: Searching for Words.

    To add command line argument to your project with Code::Blocks go to Project > Set Program Arguments. There should be a spot to list them.

  12. #42
    Join Date
    Feb 2012
    Posts
    68

    Re: Structure Linked Lists: Searching for Words.

    so just type in HW4Data.txt in the arguement box? I did it and instead of say error opening input my program just stopped responding. infinte loop somewhere? Or do i need to type something before HW4Data.txt

  13. #43
    Join Date
    Aug 2009
    Posts
    440

    Re: Structure Linked Lists: Searching for Words.

    So, are you actually trying to redirect a file into your program like:
    Code:
    ./prog < infile.txt
    If so, you might just want to add code to read in the file...then you can pass the name of the file line so, and your code can use that to open the file, etc.

  14. #44
    Join Date
    Feb 2012
    Posts
    68

    Re: Structure Linked Lists: Searching for Words.

    Well everything i need is already there to input the data file. And no, in the box i just type in HW4Data.txt and that is it. so far everything else like ./hw4.exe HW4Data.txt has resulted in an error in file input

  15. #45
    Join Date
    Feb 2012
    Posts
    68

    Re: Structure Linked Lists: Searching for Words.

    Also I remember needing a free( somewhere, can that cause infiniteness if i dont have it?

Page 3 of 4 FirstFirst 1234 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured