Thread: bare bones grades program

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    29

    bare bones grades program

    Code:
          1 /*****************************************************
          2 *          A Program To Manage Grades         *
          3 *                 written by: stormy                  *
          4 *                    9/1/05                             *
          5 ******************************************************/
          6 #include <stdio.h>
          7 #include <stdlib.h>
          8 #include <string.h>
          9 #include <math.h>
         10
         11 #define BUFFER 100
         12
         13 char name[BUFFER];           /* students name */
         14 char assignment[BUFFER];     /* assignment name */
         15 char *Nassignment[BUFFER][10];    /* array to hold assignment names */
         16 int num_grades;              /* number of grades */
         17 int grade;                   /* actuall grade number */
         18 int count = 0, i = 0;        /* counters */
         19 int ngrade[BUFFER];          /* array to hold grades */
         20
         21 int main()
         22 {
         23
         24         //////// TAKE IN INITIAL INFO ////////
         25         printf("Student name:");
         26         scanf("%s", name);
         27
         28         //////// BEGIN LOOP FOR GRADES ////////
         29
         30         printf("How many grades?:");
         31         scanf("%d", &num_grades);
         32
         33         while (i < num_grades) {
         34
         35         printf("Assignment:");
         36         scanf("%s", assignment);
         37         Nassignment[count][count] = assignment;
         38         printf("Grade:");
         39         scanf("%d", &grade);
         40         ngrade[count] = grade;
         41         ++count;
         42         ++i;
         43         }
         44
         45         //////// DO THE MATH ////////
         46         int i = 0;
         47         int k = 1;
         48         int result = 0;
         49
         50         while (i < count) {
         51         result = ngrade[i] + ngrade[k];
         52         ++i;
         53         ++k;
         54         }
         55
         56         //////// OUTPUT FORMATTED RESULTS ////////
         57         i = 0;
         58         printf("\t\tStudent Name: %s\n", name);
         59     printf("=====================================================\n");
         60         while (i < count) {
         61         printf("%s\t\t%d\n", *Nassignment[i], ngrade[i]);
         62         i++;
         63         }
         64         printf("Final Average:%d\n", result / count);
         65         printf("=====================================================\n");
         66
         67 return(0);
         68 }
    The *main* problem I'm having is getting the result correct. I really just need some good eyes and suggestions, maybe a couple of links. Also assignment is borked.

  2. #2
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Quote Originally Posted by stormy
    Code:
    18 int count = 0, i = 0;
    /* counters */


    Code:
     46         int i = 0;
         47         int k = 1;
         48         int result = 0;
    The *main* problem I'm having is getting the result correct. I really just need some good eyes and suggestions, maybe a couple of links. Also assignment is borked.
    >>the global variables need not to be initialized to 0.anyway thats not your problem.
    >>Is this your original code??then you cannot define variables anywhere you want in a C program.
    >>you are declaring 2 variables with same name global and auto this may also confuse you as global int i is therefore not valid in main.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
          1 /**************************************************  ***
          2 *          A Program To Manage Grades         *
          3 *                 written by: stormy                  *
          4 *                    9/1/05                             *
          5 **************************************************  ****/
          6 #include <stdio.h>
          7 #include <stdlib.h>
          8 #include <string.h>
          9 #include <math.h>
         10
         11 #define BUFFER 100
         12
         13 char name[BUFFER];           /* students name */
         14 //char assignment[BUFFER];     // don't need that
         15 //char *Nassignment[BUFFER][10];   would be a pointer to an array of 100 strings
         15 char Nassignment[BUFFER][10];   // 100 grades a 10 chars
         17 // int grade;                   don't need that
         18 int count = 0, i = 0;        /* counters */
         19 int ngrade[BUFFER];          /* array to hold grades */
         20
         21 int main()
         22 {
         23
         24         //////// TAKE IN INITIAL INFO ////////
         25         printf("Student name:");
         26         scanf("%s", name);
         27
         28         //////// BEGIN LOOP FOR GRADES ////////
         29
         30         printf("How many grades?:");
         31         scanf("%d", &num_grades);  // should be tested for <= 100
         32
         33         while (i < num_grades) {
         34
         35         printf("Assignment:");
         36         scanf("%s", Nassignment[i]);  // read directly
         37  //       Nassignment[count][count] = assignment; cant' assign this way
         38         printf("Grade:");
         39         scanf("%d", &ngrade[i]);          // read directly
         40         // ngrade[i] = grade;
         41         // ++count;                      don't need that
         42         ++i;
         43         }
         44
         45         //////// DO THE MATH ////////
         46    //     int i = 0;                // allready have one
         46         i = 0;
         47       //  int k = 1;                    // not needed
         48         int result = 0;
         49
         50         while (i < count) {
         51    //     result = ngrade[i] + ngrade[k];  // would overflow
         51           result += ngrade[i];
         52         ++i;
         53    //      ++k;   don't need that
         54         }
         55
         56         //////// OUTPUT FORMATTED RESULTS ////////
         57         i = 0;
         58         printf("\t\tStudent Name: %s\n", name);
         59     printf("==================================================  ===\n");
         60         while (i < count) {
         61    //     printf("%s\t\t%d\n", *Nassignment[i], ngrade[i]);   wrong indirection
         61         printf("%s\t\t%d\n", Nassignment[i], ngrade[i]);
         62         i++;
         63         }
         64         printf("Final Average:%d\n", result / count);
         65         printf("==================================================  ===\n");
         66
         67 return(0);
         68 }
    Hope I have not overlooked anything.
    Kurt
    Last edited by ZuK; 09-13-2005 at 12:59 PM.

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    29
    Sorry to be a pesk but I get the following:
    Code:
    node@titan:~/code/c/goodc$ ./grades3
    Student name:stormy
    How many grades?:2
    Assignment:blah
    Grade:56
    Assignment:blahalso
    Grade:67
                    Student Name: stormy
    =====================================================
    Floating point exception
    node@titan:~/code/c/goodc$
    But just to let you know you have helped me so much.

  5. #5
    FOX
    Join Date
    May 2005
    Posts
    188
    Code:
    scanf("%s", name);
    You've got yourself a buffer overflow there. Use fgets or add a lenght modifier like %99s or something.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    Zuk removed ++count
    so you will be dividing by zero here

    Code:
    printf("Final Average:%d\n", result / count);
    also make result a float and use %f

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    29
    thanks guys, got it working perfectly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. Replies: 13
    Last Post: 08-15-2002, 09:20 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM