Thread: function error

  1. #1
    Registered User jamez's Avatar
    Join Date
    Nov 2005
    Posts
    10

    function error

    I've been fighting with this one for a little bit and I cant figure it out. The void printStudent(studentInfo); i've tried at printStudent(studentInfo *); and i've tried printStudent(studentInfo * stud); now I'm at this point where it compiles up to the fuction of printStudent(studentInfo * stud). I know this is such a simple fix that I'm missing it completely. Any ideas?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define NUMELTS 10
    
    struct student_info {
                   char * firstname;
                   char * lastname;
                   char * major;
                   char * yearinschool;
                   int numcredits;
                   float* gpa;
                        };
    
    struct studentInfo * student_list;
    
    void printStudent(studentInfo);
    void printAllStudents();
    void addStudent();
    void getStudentInfo();
    
    int main()
    {
     char current = 'a';
     printf("Welcome to the student manager.\n");
     while (current != 'Q') 
      {
       printf("Enter a choice:\n");
       printf("[V]iew all students\n");
       printf("[A]dd a student\n");
       printf("[G]et info on a student.\n");
       printf("[Q]uit.\n");
       current = getc(stdin);
    
       getc(stdin);
    
       switch (current) 
       {
        case 'V':
          printAllStudents();
          break;
        case 'A':
          addStudent();
          break;
        case 'G':
          getStudentInfo();
          break;
        case 'Q':
          printf("Thank You!\n");
          getc(stdin);
          break;
        default:
          printf("Error: Unknown command!\n");
       }
      }
     return 0;
    }
    /************************** printstudent() **************************/
    void printStudent(studentInfo * stud)
    {
     printf("First: %s\n", stud->firstname);
     printf("\tLast: %s\n", stud->lastname);
     printf("\tMajor: %s\n", stud->major);
     printf("\tYear: %s\n", stud->yearinschool);
     printf("\tCredits: %d\n", stud->numcredits);
     printf("\tGPA: %f\n", stud->gpa);
    }
    /************************** printallstudents() **************************/
    void printAllStudents()
    {
     struct studentInfo * current = student_list;
      if (current != 0) 
       {
        while (current != 0) 
         {
          printStudent(current);
          current = current->next;
         }
        else 
         {
          printf("Error: No students have been entered yet!\n");
         }
       }
    }
    /************************** addstudent() **************************/
    void addStudent()
    {
     printf("Creating a new student...\n");
     studentInfo * temp = (struct studentInfo*) malloc(sizeof(struct studentInfo));
     printf("\nEnter student first name:");
     gets(temp->firstname);
     printf("\nEnter student last name:");
     gets(temp->lastname);
     printf("\nEnter the student's grade:");
     char buffer[8];
     gets(buffer);
     printf("\nEnter student major:");
     gets(temp->major);
     printf("\nEnter student year in school:");
     gets(temp->major);
     printf("\nEnter student credits:");
     gets(temp->numcredits);
     print("\nEnter student GPA:");
     gets(temp->gpa);
     printf("Adding student %s to the list...", temp->name);
     temp->next = student_list;
     student_list = temp;
     printf("Done!\n");
    }
    /************************** getstudentinfo() **************************/
    void getStudentInfo()
    {
     printf("Enter the name of a student:");
     char search[32];
     int found = 0;
     gets(search);
     printf("Searching the list...");
      struct studentInfo * current = student_list;
       while ((current != 0) && !found) 
        {
         if (!strcmp(current->name, search)) 
          {
           found = 1;
          }
         else
          {
           current = current->next;
          }
         printf("Done!\n");
         if (current != 0) 
          {
           printStudent(current);
          }
         else 
          {
           printf("No student by that name exists in the database\n");
          }
        }
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    On top of the file you have declared the function to take a studentInfo as parameter
    Code:
    void printStudent(studentInfo);
    and defined the function as
    Code:
    void printStudent(studentInfo * stud)
    That takes a pointer to studentInfo as parameter. That are two different functions with the same name => error
    Kurt

    Btw what is studentInfo ?

    guess you wanted to declare
    Code:
    struct studentInfo {
                   char * firstname;
                   char * lastname;
                   char * major;
                   char * yearinschool;
                   int numcredits;
                   float* gpa;
                        };
    and further down in the code you are using studentInfo as a listnode but studentInfo has no next pointer.

    Found some more

    Code:
     studentInfo * temp = (struct studentInfo*) malloc(sizeof(struct studentInfo));
     printf("\nEnter student first name:");
     gets(temp->firstname);
    You have never allocated any space for temp->firstname and any of the other strings in studentInfo.
    Kurt
    Last edited by ZuK; 11-21-2005 at 09:37 AM.

  3. #3
    Registered User jamez's Avatar
    Join Date
    Nov 2005
    Posts
    10

    function error

    I've tried the void as:

    Code:
    void printStudent(studentInfo *);
    and:
    Code:
    void printStudent(studentInfo * stud);
    and I've gotten an error on compile everytime until I changed it and dropped the *. This should point to the print for studentInfo. student_info is actually the name of the struct. the malloc hasn't generated a compile error, I guess, since I haven't gotten past this point.

    I've tried to format the void differently, but I'm not having any luck at that either.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Let me tell you again. The compiler doesn't know what a studentInfo is ( you called the struct student_info ).
    Kurt

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    I hesitate to post this because it is still full of errors, but hopefully will get you closer.

    you must always use the keyword struct since you never typedef your struct.

    You have no space to store any data in firstname, lastname...
    to start I'd change those to char firstname[100] and so on. Then change to dynamic memory allocation later.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define NUMELTS 10
    
    struct studentInfo {
                   char * firstname;
                   char * lastname;
                   char * major;
                   char * yearinschool;
                   struct studentInfo *next;
                   int numcredits;
                   float* gpa;
                };
    
    struct studentInfo * student_list;
    
    void printStudent(struct studentInfo*);
    void printAllStudents();
    void addStudent();
    void getStudentInfo();
    
    int main()
    {
     char current = 'a';
     printf("Welcome to the student manager.\n");
     while (current != 'Q')
      {
       printf("Enter a choice:\n");
       printf("[V]iew all students\n");
       printf("[A]dd a student\n");
       printf("[G]et info on a student.\n");
       printf("[Q]uit.\n");
       current = getc(stdin);
    
       getc(stdin);
    
       switch (current)
       {
        case 'V':
          printAllStudents();
          break;
        case 'A':
          addStudent();
          break;
        case 'G':
          getStudentInfo();
          break;
        case 'Q':
          printf("Thank You!\n");
          getc(stdin);
          break;
        default:
          printf("Error: Unknown command!\n");
       }
      }
     return 0;
    }
    /************************** printstudent() **************************/
    void printStudent(struct studentInfo * stud)
    {
     printf("First: %s\n", stud->firstname);
     printf("\tLast: %s\n", stud->lastname);
     printf("\tMajor: %s\n", stud->major);
     printf("\tYear: %s\n", stud->yearinschool);
     printf("\tCredits: %d\n", stud->numcredits);
     printf("\tGPA: %f\n", stud->gpa);
    }
    /************************** printallstudents() **************************/
    void printAllStudents()
    {
     struct studentInfo * current = student_list;
      if (current != 0)
       {
        while (current != 0)
         {
          printStudent(current);
          current = current->next;
         }
       }
        else
         {
          printf("Error: No students have been entered yet!\n");
         }
    }
    /************************** addstudent() **************************/
    void addStudent()
    {
     printf("Creating a new student...\n");
     struct studentInfo * temp = (struct studentInfo*) malloc(sizeof(struct studentInfo));
     printf("\nEnter student first name:");
     gets(temp->firstname);
     printf("\nEnter student last name:");
     gets(temp->lastname);
     printf("\nEnter the student's grade:");
     char buffer[8];
     gets(buffer);
     printf("\nEnter student major:");
     gets(temp->major);
     printf("\nEnter student year in school:");
     gets(temp->major); // major twice ???
     // Can't use gets with int
     //printf("\nEnter student credits:");
     //gets(temp->numcredits);
     //print("\nEnter student GPA:");
     //gets(temp->gpa);
     printf("Adding student %s to the list...", temp->name);
     temp->next = student_list;
     student_list = temp;
     printf("Done!\n");
    }
    /************************** getstudentinfo() **************************/
    void getStudentInfo()
    {
     printf("Enter the name of a student:");
     char search[32];
     int found = 0;
     gets(search);
     printf("Searching the list...");
      struct studentInfo * current = student_list;
       while ((current != 0) && !found)
        {
         if (!strcmp(current->name, search))
          {
           found = 1;
          }
         else
          {
           current = current->next;
          }
         printf("Done!\n");
         if (current != 0)
          {
           printStudent(current);
          }
         else
          {
           printf("No student by that name exists in the database\n");
          }
        }
    }

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    1. Don't use gets(). It's dangerous. See the FAQ.
    2. You shouldn't cast malloc(). See the FAQ.
    3. This code
      Code:
      getc(stdin);
      is the same as getchar(). Actually, to flush the input buffer, you should use
      Code:
      int c;
      while((c = getchar()) != '\n' && c != EOF);
    4. You know that declaring variables in the middle of blocks
      Code:
       printf("\nEnter the student's grade:");
       char buffer[8];
       gets(buffer);
      is C99/C++?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User jamez's Avatar
    Join Date
    Nov 2005
    Posts
    10

    function error

    Sorry. I was either blind or not understanding. I've got that fixed, but now I'm getting an error in my struct that visual studio points out at:

    Code:
    int numcredits;

  8. #8
    Registered User jamez's Avatar
    Join Date
    Nov 2005
    Posts
    10

    function error

    dwks:

    I'm looking over the FAQ right now. Thanks for the heads up on that and I wasn't aware I put c++ in there. I'll have to take that out to keep from confusing myself. I saw up all night working on it because of other pressing issues. Thanks for all the help from everyone. It's greatly appreciated.

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    Quote Originally Posted by jamez
    Sorry. I was either blind or not understanding. I've got that fixed, but now I'm getting an error in my struct that visual studio points out at:

    Code:
    int numcredits;
    No one is going to be able to help with just that info.
    post at least your entire struct declaration and the error message

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by spydoor
    No one is going to be able to help with just that info.
    post at least your entire struct declaration and the error message
    ... he has the entire program posted. Look in his structure.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM