Better way maybe... but need assistance

This is a discussion on Better way maybe... but need assistance within the C Programming forums, part of the General Programming Boards category; I have a function to determine a letter grade for each individual grades however I am not sure I have ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    40

    Better way maybe... but need assistance

    I have a function to determine a letter grade for each individual grades however I am not sure I have it set up the best possible way.. the errors I am getting are at the bottom.. There is three test grades but that would be alot of if elses... could someone help me on this... I am so new at this and am about to pull my hair out...



    Code:
       #include <stdio.h>
       #include <string.h>
       #include <stdlib.h>
    
    
    
       void readfile(char name, int grade1,int grade2,int grade3);
       int meangrade(int grade1,int grade2,int grade3);
       void display(char name, int grade1,int grade2,int grade3);
       int qsort(char name, int grade1, int grade2, int grade3, int meangrade);
    
         struct grades
        {
         char students[30];
         int grade1;
         int grade2;
         int grade3;
        };
    
          struct grades name[30], g1, g2, g3,*gptr;
    
          FILE *infile;
          FILE *outfile;
    
         int main(int argc, char **argv) {
        {
          char filename;
          int choice, exam;
         
          FILE *infile = fopen("grade.txt", "r");
          FILE *outfile = fopen("grades.txt","w");
    
    
          printf ("Please enter the filename needed:  \n");
          scanf("%c", &filename);
          printf("Please enter 1 for a specific exam to be displayed or 0 to exit. \n");
          scanf("%d", &choice);
             if (choice == 0)
                 display;
             else
                printf("Goodbye....\n");
    
          fclose(infile);
          fclose(outfile);
    
          return 0;
       }
          void readfile(char filename,char name[30],int grade1,int grade2,int grade3){
          fscanf(infile,"%c %d%d%d", name[30], &grade1, &grade2,&grade3);
    }
          int meangrade(int grade1,int grade2,int grade3,int meangrade){
          meangrade = (grade1 +grade2 +grade3)/3;
          return meangrade;
         }
    
          void display(char filename,char name[30], int grade1,int grade2,int grade3){
          int exam;
           printf("Which exam number would you like to see?    ");
           scanf("%d", &exam);
               if (exam > 3){
               printf("Please enter a valid exam number:  ");
               scanf("%d", &exam);
              }else
               fprintf(infile,"%s%d%d%d", name[30], &grade1,&grade2, &grade3);
             }
    
    
           void lettergrade(char letter, int grade1, int grade2, int grade3){
               if (grades > 85){
                   printf("A");
               }else if  (grades > 70 || grades< 84){
                   printf("B");
               } else if (grades >55 || grades < 69){
                   printf("C");
                }else if (grades>40 || grades<54){
                   printf("D");
                }else{
                   printf("F");
               }
    }
    
    
               int sorting(struct grades students[30] grade1 grade2, grade3,){
               int j, max;
               for(j = 0; j < 100; ++j) {
                  if (students[j].grade1 > students[j].grade2)
                       max = students[j].grade1;
                  else
                       max = students[j].grade2;
                  if (students[j].grade3 > max)
                       max = students[j].grade3;
               return max;
            }
       }
    Code:
    grades.c: In function `lettergrade':
    grades.c:77: `grades' undeclared (first use in this function)
    grades.c:77: (Each undeclared identifier is reported only once
    grades.c:77: for each function it appears in.)
    grades.c: In function `main':
    grades.c:93: parse error before "grade1"
    grades.c:106: parse error at end of input

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Code:
          int meangrade(int grade1,int grade2,int grade3,int meangrade){
          meangrade = (grade1 +grade2 +grade3)/3;
          return meangrade;
         }
    meangrade is not a declared variable. Other languages, like VB, force you to return values like this, but in C you do not. You have to declare an int variable to return, and it cannot be meangrade, since that is the function name.

    same thing in lettergrade(), grades was never declared. Don't worry about a lot of if else's. It gets the job done, and sometimes it's the only way to do what you want.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    getting this error message. Don't understand why?

    grades.c:89: conflicting types for `sorting'
    grades.c:18: previous declaration of `sorting'

    Code:
    int sorting(struct grades students[30], int grade1, int grade2, int grade3) {
    
               int j, max;
               for(j = 0; j < 100; ++j) {
                  if (students[j].grade1 > students[j].grade2){
                       max = students[j].grade1;
                  }else{
                       max = students[j].grade2;
                  }if (students[j].grade3 > max){
                      max = students[j].grade3;
                 }
               return max;

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You mind showing us line 18 and line 89 please.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    Code:
     
    
    Line 18: int sorting(char students[30], int grade1, int grade2, int grade3);
    
    Line 89 int sorting(struct grades students[30], int grade1, int grade2, int grade3)
    Line 18 is the def of the function and line 89 is the function itself... Sorry about that...

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    In the prototype you say the first parameter should be an array of characters, in the function header you say it should be an array of struct grades. Have to choose one and use it in both spots

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    So the fact that the two don't match isn't in the least bit odd to you?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    If I make them both the same it gives me a new error..

    grades.c:18: warning: `struct grades' declared inside parameter list
    grades.c:18: warning: its scope is only this definition or declaration, which is probably not what you want.
    grades.c:93: conflicting types for `sorting'
    grades.c:18: previous declaration of `sorting'


    and if I put it the other way...

    Code:
    int sorting(char students[30], int grade1, int grade2, int grade3);
    int sorting(char students[30], int grade1, int grade2, int grade3) {
               int j, max;
               for(j = 0; j < 100; ++j) {
                  if (students[j].grade1 > students[j].grade2){
                       max = students[j].grade1;
                  }else{
                       max = students[j].grade2;
                  }if (students[j].grade3 > max){
                       max = students[j].grade3;
                 }
               return max;
            }
       }
    grades.c: In function `sorting':
    grades.c:96: request for member `grade1' in something not a structure or union
    grades.c:96: request for member `grade2' in something not a structure or union
    grades.c:97: request for member `grade1' in something not a structure or union
    grades.c:99: request for member `grade2' in something not a structure or union
    grades.c:100: request for member `grade3' in something not a structure or union
    grades.c:101: request for member `grade3' in something not a structure or union

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    It's an order thing - if you prototype a function using a struct before the struct itself is in scope, C thinks you're declaring the struct in the prototype itself

    Code:
         struct grades
        {
         char students[30];
         int grade1;
         int grade2;
         int grade3;
        };
    
    // now do
    int sorting(struct grades students[30], int grade1, int grade2, int grade3);
    
    // and make your function definition match that prototype
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    Thank you that worked great now I have another issue.. I am trying to call a function and I am getting the following errors.

    grades.c: In function `main':
    grades.c:43: incompatible type for argument 1 of `readfile'
    grades.c:43: incompatible type for argument 2 of `readfile'
    grades.c:43: incompatible type for argument 3 of `readfile'
    grades.c:43: incompatible type for argument 4 of `readfile'

    Here is the code
    Code:
     readfile(students[i], grade1, grade2,grade3); /* here is where I am trying to call it.*/
    
    
    /* Here is the function itself...*/
    void readfile(char students[30], int grade1, int grade2, int grade3){
            int choice;
            printf("Please enter 1 for a specific exam to be displayed or 0 to exit. \n");
             scanf("%d", &choice);
             if (choice == 1){
                 display;
             }else{
                 printf("Goodbye....\n");}
              fscanf(infile,"%s %d%d%d", students[30], &grade1, &grade2,&grade3);
    
      }

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Are you passing it an array of 30 characters for the first argument?
    Are you passing it an integer for the second, third, and fourth arguments?

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    yes I am passing the array for the first argument and then integers for the rest... Now it can be no more then 30 students... ..

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I'm willing to bet you aren't passing an array of characters for the first parameter. Of course it'll help if you actually show the variable declartions for the function that you call from.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I am also.
    Code:
    readfile(students[i], grade1, grade2,grade3); /* here is where I am trying to call it.*/
    So 'students' is a two dimensional array? And you're passing one of those arrays, located at i to the function?

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Sep 2004
    Posts
    40
    Maybe this is more help....


    Code:
    
    void readfile(char students[30], int grade1,int grade2,int grade3);/*function declaration*/
    
    
    readfile(students[30], grade1, grade2,grade3); /*calling the function*/
    
    void readfile(char students[30], int grade1, int grade2, int grade3)
    /*Function itself.*/
    {
            int choice;
            printf("Please enter 1 for a specific exam to be displayed or 0 to exit. \n");
             scanf("%d", &choice);
             if (choice == 1){
                 display;
             }else{
                 printf("Goodbye....\n");}
              fscanf(infile,"%s %d%d%d", students[30], &grade1, &grade2,&grade3);
    
      }

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

Similar Threads

  1. Hello,i need assistance in completing the code
    By toader in forum C++ Programming
    Replies: 1
    Last Post: 06-22-2009, 03:32 AM
  2. Variable Timer assistance
    By rich2852 in forum C Programming
    Replies: 2
    Last Post: 01-21-2009, 04:43 AM
  3. Any assistance would be greatly appreciated
    By iiwhitexb0iii in forum C Programming
    Replies: 18
    Last Post: 02-26-2006, 11:06 AM
  4. Need some more assistance
    By Thantos in forum Windows Programming
    Replies: 6
    Last Post: 08-14-2003, 12:13 PM
  5. Need assistance for major text base Arena RPG project
    By Ruflano in forum C++ Programming
    Replies: 0
    Last Post: 04-04-2002, 10:11 AM

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