New Programmer, new Error

This is a discussion on New Programmer, new Error within the C Programming forums, part of the General Programming Boards category; Hey guys I'm new to C and relatively new to programming, but I'm getting an error I've never seen before. ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    14

    New Programmer, new Error

    Hey guys I'm new to C and relatively new to programming, but I'm getting an error I've never seen before. All my research on this board and online has to do with something called SDL but I dont know what that is, therefore I dont use it....ha.

    Anyways my code is below, its a simple linked list(Which i probably botched) and it reads from a file, adds information, and writes to the file again. I havent done the writing part yet, should be cake though.

    Yikes I forgot to post the error. lol.
    ERROR:
    1>Linking...
    1>LINK : fatal error LNK1561: entry point must be defined

    Also im looking at the fputs function to write, and im thinking that it isnt right because p is a FILE pointer but its set to read. I think I need to initialize and use q for that.

    Also does fputs() let me use "" to add \t for formatted file writing?

    Example:
    John Williams 99.80 Average.



    Code is below:
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    struct Student{
    	float ave;
    	char name[128];
    	struct Student* next;
    }*head = (Student *)malloc(sizeof(Student));
    
    int main()
    {
    struct Student* curr =(Student *)malloc(sizeof(Student));
    float i = 0.0;
    int count = 0, x, k;
    FILE* p = fopen("studentListInFile.txt","r");
    FILE* q = fopen("studentListInFile2.txt", "w");
    if (curr == 0){
    	printf("out of memory\n"); return -1;}
    head->next = NULL;
    curr=head;
    curr->next = NULL;
    while(!feof(p))
    {		
    		fgets(curr->name, 40, p);
    		curr->ave = 0.0;
    		curr->next = (Student *)malloc(sizeof(Student));
    		curr = curr->next;
    		curr->next=NULL;
    		count++;
    
    	
    }
    curr = head;
    for (x = 0; x<count; x++)
    {		printf("Enter the final average for the student: ");
    		scanf("&#37;f", i);
    		curr->ave = i;
    		curr = curr->next;
    }
    curr = head;
    for(k = 0; k< count; k++)
    {
    	fputs(curr->name, q);
    	fprintf(q, "\t%f\0", curr->ave);
    	curr = curr->next;
    }
    curr=head;
    for(x=0;x<count;x++)
    {
    	free(curr);
    	curr = curr->next;
    }
    free(head);
    fclose(p);
    fclose(q);
    return 0;
    }
    Last edited by ASURugger; 11-05-2008 at 08:16 PM. Reason: New code

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    struct Student{
    	double ave;
    	char name[128];
    	struct Student* next;
    }*head = NULL;
    
    int main()
    {
    	struct Student* curr = (struct Student*)malloc(sizeof(struct Student));
    	float i = 0.0f;
    	char c;
    	int count = 0, x, k;
    	FILE* p = fopen("studentListInFile.txt","r");
    	FILE* q = NULL;
    
    	if (curr == 0){
    		printf("out of memory\n");
    		return -1;
    	}
    
    	head->next = NULL;
    	curr=head;
    	while(fgets(a->name, 40, p))
    	{		
    		
    		curr->ave = 0.0f;
    		curr = curr->next;
    		count++;
    
    	
    	}
    	curr = head;
    	for (x = 0; x<count; x++)
    	{
    		printf("Enter the final average for the student: ");
    		scanf("&#37;f", i);
    		curr->ave =i;
    		curr = curr->next;
    	}
    	curr = head;
    	for(k = 0; k< count; k++)
    	{
    		fputs(curr->name, p);
    		curr = curr->next;
    	}
    	free(curr);
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    Ok, now Im confused, is it the struct part? Shouldn't I cast the pointer returned by malloc as the type I would want?

    Took out struct and Im still getting an error, so it has to be the whole casting. Im still confused though. Thanks for the help in advance

    Ok I took out both struct keywords in the memory allocation and rebuilt the problem in another solution. If I stay in my current solution in VS 2008 i keep getting the same errors even though I had fixed them, can anyone help me with that too?
    Last edited by ASURugger; 11-05-2008 at 06:54 PM. Reason: checked answer

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So:
    Code:
    $ gcc -o temp -Wall -Wextra temp.c
    temp.c: In function ‘main’:
    temp.c:24: error: ‘a’ undeclared (first use in this function)
    temp.c:24: error: (Each undeclared identifier is reported only once
    temp.c:24: error: for each function it appears in.)
    temp.c:34: warning: format ‘&#37;f’ expects type ‘float *’, but argument 2 has type ‘double’
    temp.c:17: warning: unused variable ‘q’
    temp.c:14: warning: unused variable ‘c’
    That format warning is sneaky. And I don't know what a is supposed to be either. Do you mean curr? And you're going to have to malloc one time for every student,

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    Ive fixed a LOT of errors in the program, and now I get an error after inputting the first value from the user, it takes me to line 1287 in input.c

    #else /* _UNICODE */
    _FASSIGN( longone-1, (char*)pointer , pFloatStr, (char)decimal, _loc_update.GetLocaleT());

    When i hit the break key in debug mode. Any suggestions?
    Last edited by ASURugger; 11-05-2008 at 07:17 PM.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    From what I can make out the probable cause is
    Code:
    head->next = NULL;  /* head is a NULL pointer; its not been initialized nor does it point to an object of type struct Student */
    If you were on UNIX it would abend with a SIGBUS.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Which still means you need to malloc one object for each student and hook them together in a list.

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    Ok I think I get what youre saying. I need to append the list. This is where I get confused. Since curr is going to go through the list, wont I need to create a specific named struct for each student in order for the linked list to stay intact?

    Im deleting what was stored in curr when I reassign it, so Im defeating myself in the loop. Am I on the right track?


    Also i think everytime the while loop goes through Ill be starting at the beginning of the file again with fgets? Am I wrong in that?
    Last edited by ASURugger; 11-05-2008 at 07:31 PM.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    Can anyone let me know if I'm on the right track?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    curr can't go through the list, because you don't have a list yet. You have to make a list, by building nodes and attaching, building nodes and attaching, building nodes and attaching, etc.

    And of course fgets doesn't go back to the start of the file.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    it would be a good idea to post your updated code so we can all be on the same page.

  12. #12
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    {EDIT} Updated code is at the top, thanks for the suggestion itCbitC.


    Ok....but shouldnt the list be created as I initialize the next pointer in each struct?

    for example
    Code:
    struct Node{
    int data;
    Node* next;
    }
    Should be enough to make a node member of a linked list I create in my main function right? All I would need to do is initialize a head variable, a temp(curr in my case) variable, and then when a new member is to be added on, i assign it to curr, head->next = curr, curr-> next = 0?

    Im afraid this boils down to an ignorance of linked lists, and I couldnt find a tutorial for one in your guys' tutorials.

    For some reason I keep thinking I have to name each member in the list to solve this problem, even though thatd be ludicrous as the list could be potentially HUGE.
    Last edited by ASURugger; 11-05-2008 at 08:18 PM.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by ASURugger View Post
    {EDIT} Updated code is at the top, thanks for the suggestion itCbitC.


    Ok....but shouldnt the list be created as I initialize the next pointer in each struct?

    for example
    Code:
    struct Node{
    int data;
    Node* next;
    }
    Should be enough to make a node member of a linked list I create in my main function right? All I would need to do is initialize a head variable, a temp(curr in my case) variable, and then when a new member is to be added on, i assign it to curr, head->next = curr, curr-> next = 0?

    Im afraid this boils down to an ignorance of linked lists, and I couldnt find a tutorial for one in your guys' tutorials.

    For some reason I keep thinking I have to name each member in the list to solve this problem, even though thatd be ludicrous as the list could be potentially HUGE.
    You must not be very good at looking.

  14. #14
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    Evidently I'm a terrible looker >.<, really sorry about that, i stopped after Data Structures.

  15. #15
    Registered User
    Join Date
    Nov 2008
    Posts
    14
    So after reading the linked list tutorial, I think that my program does implement a linked list properly, with a few minor tweaks. It builds fine, but when i run the program in debug mode i get the error previously said.

    Does that still have to do with my linked list even though every time I take in a new string I creat a new struct and allocate the memory for it?

    Ok, in that initial while loop I'm wrong somewhere. I put break points at the beginning of the for loop to look at what was stored in the variables.

    Something is wrong with my condition in the while loop and something is wrong with the way im going through the linked list. Im completely dumbfounded on both accounts.

    The name variable in curr has 128 T looking characters and its point to ?? it says. For the double, im getting a memory location as its value, im assuming this is because curr as a whole is a pointer, therefore the variables inside act as such?

    Im sorry for my ignorance on this and if theres a place where I can read this info, please, point me there, but currently youre the best source of information on the web.
    Last edited by ASURugger; 11-05-2008 at 08:43 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21