Thread: Massive Menu Monster

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    73

    Massive Menu Monster

    I love alliteration. Anyways, to the problem at hand. This is still very much a work in progress (no need to look at the clearly unfinished parts).

    The part that I am having trouble with is the functions labeled enter_grade, find_average, and find_letter. The problem lies with that ave bit. It calculates the average of the grades that are found in the array. The problem is i get a cast type error when setting ave=sum/5; This doesn't make any sense to, so I was wondering if it dealt with the sum and pointers which it is adding. If this is the case, please help me fix that part. It is driving me mad.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int studentgrades [10][5]={0};
    int a;
    
    /* Enter grades for a student */
    int enter_grades(){
          int j;
    
          printf ("Enter student number you want to enter grades for: ");
          scanf ("%d", &a);
    
          printf ("Enter grades for student number %d: ", a);
          for (j = 0; j<5; j++)
              scanf ("%d", *((studentgrades+a)+j));
    
          printf ("Thank you!\n");
    
          return 0;
    }
    
    /* Find average grades for a particular student */
    int find_average(){
          int ave, sum, s;
          printf ("Enter student number you want to get an average for: ");
          scanf ("%d", &a);
    
          for (s = 0; s<5; s++)
              sum += *((studentgrades+a)+s)
          ave=(sum/5);
    
          printf ("Average grade for student number %d is:\n", a, ave);
    
          printf ("Thank you!\n");
    }
    
    /* Find letter grade for a particular student */
    int find_letter(){
          int ave, sum, s;
    
          printf ("Enter student number you want to get an average for: ");
          scanf ("%d", &a);
    
          for (s = 0; s<5; s++)
              sum += *((studentgrades+a)+s)
          ave=(sum/5);
    
          if (ave<=100 && ave>=90)
              printf ("Letter grade for student number %d: A", a);
          else if (ave<90 && ave>=80)
              printf ("Letter grade for student number %d: B", a);
          else if (ave<80 && ave>=70)
              printf ("Letter grade for student number %d: C", a);
          else if (ave<70 && ave>=60)
              printf ("Letter grade for student number %d: D", a);
          else if (ave<60 && ave>=0)
              printf ("Letter grade for student number %d: E", a);
    
          printf ("Thank you!\n");
    }
    
    /*  Find average grade and letter grade for the whole class */
    int avegrade_class()
    
    /*  Find how much would a student need to increase his letter grade */
    int increase()
    
    /*displays the menu,select choice*/
    int menu()  {
    int a;
    
    printf("1.	Enter grades for a student
    2.	Find average grades for a particular student
    3.	Find letter grade for a particular student
    4.	Find average grade and letter grade for the whole class
    5.	Find how much would a student need to increase his letter grade
    6.	Exit\n
    Enter your choice: ");
    
    scanf ("%d", &a);
    
    switch (a)
      {
        case 1: enter_grades(); break;
        case 2: find_average(); break;
        case 3: find_letter(); break;
        case 4: avegrade_class(); break;
        case 5: increase(); break;
        case 6: exit;
      }
    }
    
    int main()
    {
    menu();
    
          system("PAUSE");
          return 0;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You need to pull out your book on C and do a little reading. Here is code that compiles, but I haven't run it:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int studentgrades [10][5]={0};
    int a;
    
    /* Enter grades for a student */
    int enter_grades(void){
      int j;
    
      printf ("Enter student number you want to enter grades for: ");
      scanf ("%d", &a);
    
      printf ("Enter grades for student number %d: ", a);
      for (j = 0; j<5; j++)
        scanf ("%d", *((studentgrades+a)+j));
    
      printf ("Thank you!\n");
    
      return 0;
    }
    
    /* Find average grades for a particular student */
    int find_average(void){
      int ave, sum = 0, s;
      printf ("Enter student number you want to get an average for: ");
      scanf ("%d", &a);
    
      for (s = 0; s<5; s++)
        sum += studentgrades[a][s];
      ave=(sum/5);
    
      printf ("Average grade for student number %d is:\n", a, ave);
    
      printf ("Thank you!\n");
      return ave;
    }
    
    /* Find letter grade for a particular student */
    void find_letter(void){
      int ave, sum = 0, s;
    
      printf ("Enter student number you want to get an average for: ");
      scanf ("%d", &a);
    
      for (s = 0; s<5; s++)
        sum += studentgrades[a][s];
      ave=(sum/5);
    
      if (ave<=100 && ave>=90)
        printf ("Letter grade for student number %d: A", a);
      else if (ave<90 && ave>=80)
        printf ("Letter grade for student number %d: B", a);
      else if (ave<80 && ave>=70)
        printf ("Letter grade for student number %d: C", a);
      else if (ave<70 && ave>=60)
        printf ("Letter grade for student number %d: D", a);
      else if (ave<60 && ave>=0)
        printf ("Letter grade for student number %d: E", a);
    
      printf ("Thank you!\n");
    }
    
    /*  Find average grade and letter grade for the whole class */
    int avegrade_class(void);
    
    /*  Find how much would a student need to increase his letter grade */
    int increase(void);
    
    /*displays the menu,select choice*/
    void menu(void)  {
      int a;
    
      printf("1.	Enter grades for a student"
        "2.	Find average grades for a particular student"
        "3.	Find letter grade for a particular student"
        "4.	Find average grade and letter grade for the whole class"
        "5.	Find how much would a student need to increase his letter grade"
        "6.	Exit\n"
        "Enter your choice: ");
    
      scanf ("%d", &a);
    
      switch (a)
      {
      case 1: enter_grades(); break;
      case 2: find_average(); break;
      case 3: find_letter(); break;
      case 4: avegrade_class(); break;
      case 5: increase(); break;
      case 6: exit(0);
      }
    }
    
    int main(void)
    {
      menu();
    
      system("PAUSE");
      return 0;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    Besides declaring the array, I am not allowed to use [] when accessing elements of an array. That is why I am using pointers (or at least trying). That is why I am getting confusd/messing up.

    EDIT: Also, I have been doing reading. Ive been going through 2 books and online help. I dont post a problem unless I cant figure it out. The restrictions on what I am supposed to do is what messes me up. Besides setting sum=0, and putting the [] in (which i am not allowed to use), not much is changed. I just want to know what concept I am looking over for those functions I was referring to. It would be much appreciated.
    Last edited by pxleyes; 03-25-2004 at 06:02 PM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >not much is changed
    Except for declaring functions to return int when they actually return nothing, forgetting a whole slew of semicolons, trying to span string literals across multiple lines and trying to call exit without a parameter list.

    >I am not allowed to use [] when accessing elements of an array
    What a silly restriction. Here's how you would do it with a two dimensional array:
    Code:
    *(*(studentgrades + a) + s)
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    Originally posted by Prelude
    >not much is changed
    Except for declaring functions to return int when they actually return nothing, forgetting a whole slew of semicolons, trying to span string literals across multiple lines and trying to call exit without a parameter list.

    >I am not allowed to use [] when accessing elements of an array
    What a silly restriction. Here's how you would do it with a two dimensional array:
    Code:
    *(*(studentgrades + a) + s)
    I'm aware its a silly restriction. trust me, i know. Sorry I snapped, but I know a LOT of it isn't done. Thats why I said in the first post to JUST look at the 3 functions I mentioned and for the problems I was adressing at that point.

  6. #6
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    prelude, I am sitll getting a parse error before the = in the line ave=(sum/5);. I dont understand why ave would be causing a problem?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I dont understand why ave would be causing a problem?
    It probably isn't. Post your code. I would guess that you forgot a semicolon on the line before that statement.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    Woah, didn't even see that. Its always the little things. Thanks.

  9. #9
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    This is what I have now. It compiles fine, but there are some procedural problems I noticed. THe major one is that when you run option 1, the grades dont actually input like they should. Ill look that over now. The other problem I know I am going to have is that in the avegrade_class function, I have to return a "Not all grades entered" if not all the grades are entered. I think this specifically means that they want it to return that text if a particular student's grades are ALL = 0. I dont know how I would do that without a big if, else statement.

    woops, here is the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int studentgrades [10][5]={0};
    int a;
    
    /* Enter grades for a student */
    int enter_grades(){
          int j;
    
          printf ("Enter student number you want to enter grades for: ");
          scanf ("%d", &a);
    
          printf ("Enter grades for student number %d: ", a);
          for (j = 0; j<5; j++)
              scanf ("%d", *(studentgrades+a)+j);
    
          printf ("Thank you!\n\n");
    
          return 0;
    }
    
    /* Find average grades for a particular student */
    int find_average(){
          int ave=0, sum=0, s;
          printf ("Enter student number you want to get an average for: ");
          scanf ("%d", &a);
    
          for (s = 0; s<5; s++){
              sum += *(*(studentgrades+a)+s);}
          ave=(sum/5);
    
          printf ("Average grade for student number %d is: %d\n", a, ave);
    
          printf ("Thank you!\n\n");
    
          return ave;
    }
    
    /* Find letter grade for a particular student */
    void find_letter(){
          int ave=0, sum=0, s;
    
          printf ("Enter student number you want to get an average for: ");
          scanf ("%d", &a);
    
          for (s = 0; s<5; s++){
              sum += *(*(studentgrades+a)+s);}
          ave=(sum/5);
    
          if (ave<=100 && ave>=90)
              printf ("Letter grade for student number %d: A", a);
          else if (ave<90 && ave>=80)
              printf ("Letter grade for student number %d: B", a);
          else if (ave<80 && ave>=70)
              printf ("Letter grade for student number %d: C", a);
          else if (ave<70 && ave>=60)
              printf ("Letter grade for student number %d: D", a);
          else if (ave<60 && ave>=0)
              printf ("Letter grade for student number %d: E", a);
    
          printf ("Thank you!\n\n");
    }
    
    /*  Find average grade and letter grade for the whole class */
    void avegrade_class() {
    
          printf ("Thank you!\n\n");
    }
    /*  Find how much would a student need to increase his letter grade */
    void increase() {
    
          printf ("Thank you!\n\n");
    }
    
    /*displays the menu,select choice*/
    int menu()  {
    int a;
    
    printf("1.	Enter grades for a student\n"
           "2.	Find average grades for a particular student\n"
           "3.	Find letter grade for a particular student\n"
           "4.	Find average grade and letter grade for the whole class\n"
           "5.	Find how much would a student need to increase his letter grade\n"
           "6.	Exit\n"
           "Enter your choice: ");
    
    scanf ("%d", &a);
    
    switch (a)
      {
        case 1: enter_grades(); break;
        case 2: find_average(); break;
        case 3: find_letter(); break;
        case 4: avegrade_class(); break;
        case 5: increase(); break;
        case 6: exit(0);
      }
    }
    
    int main()
    {
    while(1) {
          menu();
    }
          system("PAUSE");
          return 0;
    }
    EDIT: Ok, I got the values to input. Thats a start. However, no number is being shown for the average function. Gotta work on that considering the letter grade function works properly (and it includes the average equation).

    EDIT 2: Scratch that error too. I forgot a %d. Stupid mistake. The problem with the "not all grades entered" is still a big one for me though.
    Last edited by pxleyes; 03-25-2004 at 06:54 PM.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I dont know how I would do that without a big if, else statement.
    A loop would be better:
    Code:
    int empty = 0;
    for ( i = 0; i < 5; i++ ) {
      if ( studentgrades[student][i] == 0 ) {
        empty = 1;
        break;
      }
    }
    if ( empty )
      return "Not all grades entered";
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Feb 2004
    Posts
    73
    This is the final code for those who are interested:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int studentgrades [10][5]={0};
    int a;
    
    /* Enter grades for a student */
    int enter_grades(){
          int j;
    
          printf ("\nEnter student number you want to enter grades for: ");
          scanf ("%d", &a);
    
          printf ("Enter grades for student number %d: ", a);
          for (j = 0; j<5; j++)
              scanf ("%d", *(studentgrades+(a-1))+j);
    
          printf ("Thank you!\n\n");
    
          return 0;
    }
    
    /* Find average grades for a particular student */
    int find_average(){
          int ave=0, sum=0, s;
          printf ("\nEnter student number you want to get an average for: ");
          scanf ("%d", &a);
    
          for (s = 0; s<5; s++){
              sum += *(*(studentgrades+(a-1))+s);}
          ave=(sum/5);
    
          printf ("Average grade for student number %d is: %d\n", a, ave);
    
          printf ("Thank you!\n\n");
    
          return ave;
    }
    
    /* Find letter grade for a particular student */
    void find_letter(){
          int ave=0, sum=0, s;
    
          printf ("\nEnter student number you want to get an average for: ");
          scanf ("%d", &a);
    
          for (s = 0; s<5; s++){
              sum += *(*(studentgrades+(a-1))+s);}
          ave=(sum/5);
    
          if (ave<=100 && ave>=90)
              printf ("Letter grade for student number %d: A\n", a);
          else if (ave<90 && ave>=80)
              printf ("Letter grade for student number %d: B\n", a);
          else if (ave<80 && ave>=70)
              printf ("Letter grade for student number %d: C\n", a);
          else if (ave<70 && ave>=60)
              printf ("Letter grade for student number %d: D\n", a);
          else if (ave<60 && ave>=0)
              printf ("Letter grade for student number %d: E\n", a);
    
          printf ("Thank you!\n\n");
    }
    
    /*  Find average grade and letter grade for the whole class */
    int avegrade_class() {
         int s,t,i,ave=0, sum=0, empty=0;
    
         for ( i = 0; i < 10; i++ ) {
             if ( *(*(studentgrades+i)+0) == 0 ) {
                empty = 1;
                break;
                }
         }
          if ( empty )
               return printf("Not all grades entered\n\n");
          else{
    
          for (s = 0; s<5; s++){
             for (t=0; t<10; t++){
              sum += *(*(studentgrades+t)+s);}
          }
    
          ave=(sum/50);
    
          printf ("Average grade for the whole class: %d", ave);
    
          if (ave<=100 && ave>=90)
              printf ("\nLetter grade for the whole class: A\n");
          else if (ave<90 && ave>=80)
              printf ("\nLetter grade for the whole class: B\n");
          else if (ave<80 && ave>=70)
              printf ("\nLetter grade for the whole class: C\n");
          else if (ave<70 && ave>=60)
              printf ("\nLetter grade for the whole class: D\n");
          else if (ave<60 && ave>=0)
              printf ("\nLetter grade for the whole class: E\n");
    
          printf ("Thank you!\n\n");
          }
    
    }
    /*  Find how much would a student need to increase his letter grade */
    void increase() {
          int sum=0, s, grade=0;
    
          printf ("Enter student number: ");
          scanf ("%d", &a);
    
          for (s = 0; s<5; s++){
              sum += *(*(studentgrades+(a-1))+s);}
    
          if (sum>=450)
            printf ("Letter grade can not be increased, already an A.\n");
          else if (sum<450 && sum>=400){
            grade=450-sum;
            printf ("Number of points needed to increase letter grade: %d\n", grade);}
          else if (sum<400 && sum>=350){
            grade=400-sum;
            printf ("Number of points needed to increase letter grade: %d\n", grade);}
          else if (sum<350 && sum>=300){
            grade=350-sum;
            printf ("Number of points needed to increase letter grade: %d\n", grade);}
          else if (sum<300){
            grade=300-sum;
            printf ("Number of points needed to increase letter grade: %d\n", grade);}
    
          printf ("Thank you!\n\n");
    }
    
    /*displays the menu,select choice*/
    int menu()  {
    int a;
    
    printf("1.	Enter grades for a student\n"
           "2.	Find average grades for a particular student\n"
           "3.	Find letter grade for a particular student\n"
           "4.	Find average grade and letter grade for the whole class\n"
           "5.	Find how much would a student need to increase his letter grade\n"
           "6.	Exit\n"
           "Enter your choice: ");
    
    scanf ("%d", &a);
    
    switch (a)
      {
        case 1: enter_grades(); break;
        case 2: find_average(); break;
        case 3: find_letter(); break;
        case 4: avegrade_class(); break;
        case 5: increase(); break;
        case 6: exit(0);
      }
    }
    
    int main()
    {
    while(1) {
          menu();
    }
          system("PAUSE");
          return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM