A newbie

This is a discussion on A newbie within the C Programming forums, part of the General Programming Boards category; Hi guys I need some help with pointers. The code below doesn't work. What am I doing wrong?. Code: #include ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    77

    A newbie

    Hi guys I need some help with pointers. The code below doesn't work. What am I doing wrong?.

    Code:
    #include <stdio.h>
    void ReadNumStudents(int* pt_NumStudents);
    void ReadNumOfGrades(int * pt_NumOfGrades);
    void GetMaxTotalPoints(int NumOfGrades, int* pt_MaxTotalPoints);
    void ReadAddGrades(int NumOfGrades, int* pt_TotalPoints);
    int ReadGrade(int);
    void ClassGrades(int NumStudents, int NumOfGrades,int MaxTotalPoints);
    
    int main()
    {
       int NumStudents;     /* number of students in class       */
       int NumOfGrades;     /* number of grades for each student */
       int MaxTotalPoints;  /* total number of possible points   */
    
       printf("This program finds the average for each student.\n\n");
       ReadNumStudents(&NumStudents);
       ReadNumOfGrades(&NumOfGrades);
       GetMaxTotalPoints(NumOfGrades, &MaxTotalPoints);
       ClassGrades(NumStudents, NumOfGrades, MaxTotalPoints);
       return 0;
    }
    
    /*
     * Function to read and return the number of students
     * Pre:  none
     * Post: The (positive) number of students was returned.
     */
    
    void ReadNumStudents(int *pt_NumStudents)
    {
                    /* number of students in class */
    
       printf("How many students are in the class? ");
       scanf("%d", &pt_NumStudents);
       while (pt_NumStudents <= 0)
       {
          printf("The number of students must be positive.\n");
          printf("How many students are there? ");
          scanf("%d", &pt_NumStudents);
    
       }
    
    }
    /*
     * Function to read and return the number of grades
     * Pre:  none
     * Post: The (positive) number of grades was returned.
     */
    
    void ReadNumOfGrades(int *pt_NumOfGrades)
    {
                    /* number of grades for each student */
    
       printf("How many course grades each student has? ");
       scanf("%d", &pt_NumOfGrades);
       while (pt_NumOfGrades <= 0)
       {
          printf("The number of course grades must be positive.\n");
          printf("How many course grades each student has? ");
          scanf("%d", &pt_NumOfGrades);
       }
    
    
    }
    /*
     * Function to return the maximum total number of points
     * possible for a student to earn.
     * Pre:  NumOfGrades is the (positive) number of grades.
     * Post: The (positive) maximum possible points was returned.
     */
    
    void GetMaxTotalPoints(int NumOfGrades, int *pt_MaxTotalPoints)
    {
                                    /* total number of possible points */
    
       printf("\nEnter the maximum grade for each course.\n");
      pt_MaxTotalPoints = ReadAddGrades(NumOfGrades);
       while (*pt_MaxTotalPoints <= 0)
       {
          printf("The total maximum grade must be positive.\n");
          printf("Please enter maximum grade for each course again.\n\n");
          pt_MaxTotalPoints = ReadAddGrades(NumOfGrades);
       }
    
    }
    /*
     * Function to compute and print the average of each student
     * Pre:  NumStudents is the (positive) number of students.
     *       NumOfGrades is the (positive) number of grades.
     *       MaxTotalPoints is the (positive) total worth of grades.
     * Post: Each student's average was displayed.
     */
    
    void ClassGrades(int NumStudents, int NumOfGrades, int MaxTotalPoints)
    {
       int student;         /* index - which student */
       int TotalPoints;     /* total number of points */
       float average;       /* student's average */
    
       for (student = 1; student <= NumStudents; student++)
       {
          printf("\nEnter the grades for student %d.\n", student);
          TotalPoints = ReadAddGrades(NumOfGrades);
    
          average = (float)100 * TotalPoints / MaxTotalPoints;
    
          printf("\nAverage for student %d = %.1f%%\n",student, average);
       }
    }
    
    /*
     * Function to read and total grade and to return total
     * Pre:  NumOfGrades is the (positive) number of grades.
     * Post: The nonnegative total grade points was returned.
     */
    
    void ReadAddGrades(int NumOfGrades, int *pt_TotalPoints)
    {
    
       int grade;                   /* index - which grade    */
    
       pt_TotalPoints = 0;
    
       for (grade = 1; grade <= NumOfGrades; grade++)
            pt_TotalPoints += ReadGrade(grade);
    
    
    }
    /*
     * Function to read and return an integer grade
     * Pre:  grade is the number of the grade.
     * Post: A nonnegative grade was returned.
     */
    
    int ReadGrade(int grade)
    {
       int points;                  /* points for one test*/
    
       do
       {
          printf("Enter the nonnegative value of grade %d: ", grade);
          scanf("%d", &points);
       }while (points < 0);
    
       return points;
    }
    And this is the errors I am getting when I compile it.

    Code:
    test.c: In function `GetMaxTotalPoints':
    test.c:78: too few arguments to function `ReadAddGrades'
    test.c:78: void value not ignored as it ought to be
    test.c:83: too few arguments to function `ReadAddGrades'
    test.c:83: void value not ignored as it ought to be
    test.c: In function `ClassGrades':
    test.c:105: too few arguments to function `ReadAddGrades'
    test.c:105: void value not ignored as it ought to be
    thanks

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    pt_MaxTotalPoints = ReadAddGrades(NumOfGrades);
    ReadAddGrades takes two parameters, and you're only giving it one; that accounts for 3 errors.
    Also, by setting pt_MaxTotalPoints to the result of ReadAddGrades it's expecting ReadAddGrades to return something, and your function is void (doesn't return anything) which accounts for the other 3 errors.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    On an unrelated note that's the best formatted code I've seen on these forums in a while.

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Well hy not fix up the errors the compiler gives you and go from there.

    Code:
    pt_MaxTotalPoints = ReadAddGrades(NumOfGrades);
    Your function ReadAddGrades is declared as having a void return type. So why are you trying to assign a return value from it? Either make it a proper return type (depending on what type you wish to return, be it int, float or whatever) or else don't try to return something from it. In any case, your function prototype has to match your usage of the function.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    Thanks guys..will try that and will post back the results.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  2. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 06:49 PM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 11:53 AM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM

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