-
April 9th, 2012, 02:38 PM
#31
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.
-
April 9th, 2012, 02:55 PM
#32
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.
-
April 9th, 2012, 03:02 PM
#33
Re: Structure Linked Lists: Searching for Words.
Originally Posted by chucker
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.
-
April 9th, 2012, 03:08 PM
#34
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';
}
-
April 9th, 2012, 03:31 PM
#35
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?
-
April 9th, 2012, 03:45 PM
#36
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.
-
April 9th, 2012, 03:46 PM
#37
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.
-
April 9th, 2012, 03:51 PM
#38
Re: Structure Linked Lists: Searching for Words.
Originally Posted by chucker
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?
-
April 9th, 2012, 03:56 PM
#39
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.
-
April 9th, 2012, 04:20 PM
#40
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.
-
April 9th, 2012, 05:33 PM
#41
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.
-
April 9th, 2012, 06:09 PM
#42
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
-
April 9th, 2012, 06:34 PM
#43
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.
-
April 9th, 2012, 06:44 PM
#44
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
-
April 9th, 2012, 06:53 PM
#45
Re: Structure Linked Lists: Searching for Words.
Also I remember needing a free( somewhere, can that cause infiniteness if i dont have it?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|