Need Help w/ Structures and Pointers

This is a discussion on Need Help w/ Structures and Pointers within the C Programming forums, part of the General Programming Boards category; First I want to say this is a HOMEWORK assignment. I am looking for help but I don't want to ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    9

    Need Help w/ Structures and Pointers

    First I want to say this is a HOMEWORK assignment. I am looking for help but I don't want to cheat. With that said, I'm looking to understand the CONCEPTS that I have wrong, and the DIRECTION I need to go in order to fix this. Please help in any way you can without the possibility of cheating. I will do my best to make this post as clear and to the point as possible. Thanks!

    The code is at the end of this post. Please note that it is rather lengthy.

    /**************************************************
    The Assignment:
    Create a structure to hold student names and averages. The structure should contain first name, last name, and an integer grade average.


    Write a program that will do the following:
    1) Create an array of pointers to these student structures.
    2) Prompt the user for names and averages
    3) After you get the student's info, use malloc to provide the memory to store the info
    4)Place the address of the student, returned by malloc, into the pointer array.
    5)AFTER the user indicates there are no more students:
    -Search data for the highest and lowest grade
    -Print name and grade for highest grade
    -Print name and grade for lowest grade
    -Print average of all grades entered


    Program should allow storage of up to 25 students.
    Use proper modularization to divide your program into logical functions.
    *************************************************/
    If it is needed I can post the error messages I get when I try to compile the program.

    In summary, I beleive my errors to be the following:
    1. Incorrect syntax with pointers
    2. Incorrect use of malloc
    3. Incorrect syntax with structures
    The Code:
    Code:
    #include "stdafx.h"
    #include <string.h>
    
    
    #define SIZE 25
    
    
    int enterStudents (int);
    void searchData (int, int, int);
    int calculateAvg (int, int);
    void printData (int, int, int);
    
    
    struct student
    {
        char firstName;
        char lastName;
        int average;
    };
    
    
    int main()
    {
        int pointArray[SIZE], high[3], low[3];
        int forCounter = 0, studentCounter, avgGrade; 
    
    
        for (forCounter = 0; forCounter < 25; forCounter++)
            pointArray[forCounter] = 0;
    
    
        studentCounter = enterStudents(pointArray);
    
    
        searchData(pointArray, high, low);
    
    
        avgGrade = calculateAvg(studentCounter, pointArray);
    
    
        printData(avgGrade, high, low);
    
    
    
    
        return 0;
    }
    
    
    
    
    int enterStudents (int ePointArray[SIZE])
    {
        char tempFname[20], tempLname[20], yesNo[2] = "y";
        int tempAvg, counter = 0;
        int *studPtr;
        
        struct student aStud={"\0", "\0", 0};
    
    
    
    
        while( counter < SIZE && strcmp(yesNo, "y")==0)
        {
            printf(" Enter first name: ");
            scanf("%s", tempFname);
    
    
            printf(" Enter last name: ");
            scanf("%s", tempLname);
    
    
            printf(" Enter grad average:");
            scanf("%d", tempAvg);
    
    
            strcpy(aStud.firstName, tempFname);
            strcpy(aStud.lastName, tempLname);
            aStud.average = tempAvg;
    
    
            studPtr = malloc(sizeof(struct aStud));
    
    
            ePointArray[counter] = &studPtr;
    
    
            counter++;
    
    
            printf("/n");
            printf(" Do you have more students? yes or no:");
            scanf("%s", yesNo);
        }
    
    
        return counter;
    
    
    }
    
    void searchData (int sPointArray[SIZE], int sHigh[3], int sLow[3])
    {
        int searchCounter = 0;
    
    
        while( searchCounter = 0)
        {
            if( *sPointArray[searchCounter].average > *sPointArray[searchCounter+1].average)
            {
                sHigh[0] = &sPointArray[searchCounter].firstName;
                sHigh[1] = &sPointArray[searchCounter].lastName;
                sHigh[2] = &sPointArray[searchCounter].average;
            }
            
            if( *sPointArray[searchCounter].average < *sPointArray[searchCounter+1].average)
            {
                sLow[0] = &sPointArray[searchCounter].firstName;
                sLow[1] = &sPointArray[searchCounter].lastName;
                sLow[3] = &sPointArray[searchCounter].average;
            }
    
    
            searchCounter++;
        }
    
    
    }
    
    
    int calculateAvg( int totalStudents, int avgPointArray[SIZE])
    {
        int sum = 0;
        int avgCounter;
        double overallAvg;
    
    
        for( avgCounter = 0; avgCounter < totalStudents; avgCounter++)
            sum = sum + *avgPointArray[avgCounter].average;
    
    
        overallAvg = sum/totalStudents;
    
    
        return overallAvg;
    }
    
    
    
    
    void printData (int pAverage, int pHigh[3], int pLow[3])
    {
        printf(" Highest Grade: %s %s %d", *pHigh[0], *pHigh[1], *pHigh[3]);
        printf("/n");
        printf(" Lowest Grade: %s %s %d", *pLow[0], *pLow[2], *pLow[3]);
        printf("/n");
        printf(" Average Grade: %d",pAverage);
    
    
    }

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    If it is needed I can post the error messages I get when I try to compile the program.
    When posting looking for help on forums you should always, always do this. Don't expect people to visually inspect your program or download and compile it themselves.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Gonna guess it has something to do with pointers of type int rather than of type struct student

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    9
    Here are the errors. The numbers at the end of each description are the line numbers. (i.e. The first error is on line 32.)

    Error 1 error C2664: 'enterStudents' : cannot convert parameter 1 from 'int [25]' to 'int' 32
    Error 2 error C2664: 'searchData' : cannot convert parameter 1 from 'int [25]' to 'int' 35
    Error 3 error C2664: 'calculateAvg' : cannot convert parameter 2 from 'int [25]' to 'int' 38
    Error 4 error C2664: 'printData' : cannot convert parameter 2 from 'int [3]' to 'int' 41
    Error 5 error C2440: 'initializing' : cannot convert from 'const char [2]' to 'char' 58
    Error 6 error C2440: 'initializing' : cannot convert from 'const char [2]' to 'char' 58
    Error 7 error C2664: 'strcpy' : cannot convert parameter 1 from 'char' to 'char *' 77
    Error 8 error C2664: 'strcpy' : cannot convert parameter 1 from 'char' to 'char *' 78
    Error 9 error C2027: use of undefined type 'enterStudents::aStud' 82
    Error 10 error C3861: 'malloc': identifier not found 82
    Error 11 error C2440: '=' : cannot convert from 'int **' to 'int' 85
    Error 12 error C2228: left of '.average' must have class/struct/union 109
    Error 13 error C2228: left of '.average' must have class/struct/union 109
    Error 14 error C2228: left of '.firstName' must have class/struct/union 111
    Error 15 error C2228: left of '.lastName' must have class/struct/union 112
    Error 16 error C2228: left of '.average' must have class/struct/union 113
    Error 17 error C2228: left of '.average' must have class/struct/union 116
    Error 18 error C2228: left of '.average' must have class/struct/union 116
    Error 19 error C2228: left of '.firstName' must have class/struct/union 118
    Error 20 error C2228: left of '.lastName' must have class/struct/union 119
    Error 21 error C2228: left of '.average' must have class/struct/union 120
    Error 22 error C2228: left of '.average' must have class/struct/union 139
    Error 24 error C2100: illegal indirection 153
    Error 25 error C2100: illegal indirection 153
    Error 26 error C2100: illegal indirection 153
    Error 27 error C2100: illegal indirection 155
    Error 28 error C2100: illegal indirection 155
    Error 29 error C2100: illegal indirection 155

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Oh, so many problems...

    Code:
    struct student
    {
        char firstName;
        char lastName;
        int average;
    };
    You do realize that char means a single character, not an array of characters -- otherwise known as a C-string -- right? And that a name cannot fit into a single character?

    First actual error:
    Code:
    Error 1 error C2664: 'enterStudents' : cannot convert parameter 1 from 'int [25]' to 'int' 32
    Your prototype
    Code:
    int enterStudents (int);
    Your definition
    Code:
    int enterStudents (int ePointArray[SIZE])
    See the difference?

    Same with the other similar errors.

    Get those fixed first and try again.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    9
    Thanks!. I forgot that if I was passing an array into a function I had to specify that in the prototype as well. That did indeed clear up the first set of errors. I have also made the name variables in the structure arrays as well.

    Still lots of errors to go. However, I'm pretty sure that all of them are the same 3 or 4 errors - just repeated more than once.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers to structures
    By breaka in forum C Programming
    Replies: 15
    Last Post: 08-15-2006, 12:20 PM
  2. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 05:31 PM
  3. structures with pointers to structures
    By kzar in forum C Programming
    Replies: 3
    Last Post: 11-20-2004, 08:32 AM
  4. Pointers to Classes || pointers to structures
    By C++Child in forum C++ Programming
    Replies: 24
    Last Post: 07-30-2004, 06:14 PM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM

Tags for this Thread


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