GPA Program Problems

This is a discussion on GPA Program Problems within the C Programming forums, part of the General Programming Boards category; I am currently working on a program so that when a student enters letter grades the program will calculate GPA. ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    2

    GPA Program Problems

    I am currently working on a program so that when a student enters letter grades the program will calculate GPA. The problem I am having with + and - . The + and - are causing errors in compiling. My code is below. There may be other mistakes in the code that I just haven't fixed yet I just stopped working on it untill I could figure this problem out. Any suggestions? Thanks.

    Code:
    /*! \file finalproject.c
    * \brief My final project GPA calculator
    *
    * \author Mantz Sussky
    * \date 8 April 2005
    * 
    * This program accepts as inputs as integer numbers and characters and then prints the students current 
    * GPA and calculates the grades they need to achieve their goal GPA
    */
    
    #include <stdio.h>
    #include <assert.h>
    #include <math.h>
    
    /*! \fn
    * \brief This is the entry point.
    *
    * These are the only functions in this simple program.  The return type of
    * main is int by default.
    */
    
    int main(void) {
    
    /*! \var number
    * number is the number of integers in the array
    */
    int number;
    
    /*! \var sum
    * sum is the sum of integers in the array
    */
    double sum;
    
    /*! \var mean
    * mean is the average of integers in the array
    */
    double mean;
    
    /*! \var i
    * i is the marker number in the array
    */
    int i;
    int semester_comp;
    int semester_left;
    int classes;
    double goal_gpa;
    double gpa_needed;
    int class_left;
    int total_class;
    
    
    /*! Prompt user and get input */ 
    printf("How many semesters have you been in school? \n");
    printf("\n >");
    scanf("%i", &semester_comp);
    
    printf("\n How many semesters do you have left to complete? \n");
    printf("\n >");
    scanf("%i", &semester_left);
    
    printf("\n How many classes do you take per semester? \n");
    printf("\n >");
    scanf("%i", &classes);
    
    printf("\n What is your goal GPA you would like to achieve? \n");
    printf("\n >");
    scanf("%lf", &goal_gpa);
    
    /*! \ var x[number]
    * x[number] defines the size of the array.
    */
    number = classes * semester_comp;
    int x[number];
    
    /*char A;
    
    double A-;
    double "B+";
    double B;
    double "B-";
    double "C+";
    double C;
    double "C-";
    double "D+";
    double D;
    double "D-";
    double F; */
    
    /*(A) = 4.0;
    (A-) = 3.67;
    (B+) = 3.33; 
    (B) = 3.0;
    (B-) = 2.67;
    (C+) = 2.33;
    (C) = 2.0;
    (C-) = 1.67;
    (D+) = 1.33;
    (D) = 1.0;
    (D-) = .67;
    (F) = 0;*/
    
    printf("Enter your grades for the %i classes you have completed and press return after each letter \n", number);
    printf("\n **Please enter letter grades A through F in all caps with pluses or minuses**\n \n");
    for ( i = 0; i < number;  ++i)
    scanf("%i", &x[i]);
    printf("\n >");
    printf("%i\n", x[i]);
    /*
    if (x[i] == "A")
    	y[i] = 4.00;
    else if (x[i] == "A-")
    	y[i] = 3.67;
    else if (x[i]=="B+")
    	y[i] = 3.33;
    else if (x[i]=="B")
    	y[i] = 3.00;
    else if (x[i]=="B-")
    	y[i] = 2.67;
    else if (x[i]=="C+")
    	y[i] = 2.33;
    else if (x[i]=="C")
    	y[i] = 2.00;
    else if (x[i]=="C-")
    	y[i] = 1.67;
    else if (x[i]=="D+")
    	y[i] = 1.33;
    else if (x[i]=="D")
    	y[i] = 1.00;
    else if (x[i]=="D-")
    	y[i] = 0.67;
    else if (x[i]=="F")
    	y[i] = 0.00;
    else printf("\n Incorrect Data Entry Program Failed");
    */
    /*! Compute the sum */
    sum = 0;
    for (i = 0; i < number; ++i) {
    sum += x[i];
    }
    
    /*! Find GPA */
    mean = sum / number;
    printf("\n Your current G.P.A. is %.2f.\n", mean);
    
    class_left = semester_left * classes;
    total_class = class_left + number;
    gpa_needed = (((goal_gpa * total_class)-(mean * number))/class_left);
    
    if (gpa_needed > 4.00)
    printf("\n I'm sorry that GPA is out of reach.");
    else if (gpa_needed <= 4.00 && gpa_needed > 3.67)
    printf("\n You need an A average for your next %i semesters", semester_left);
    else if (gpa_needed <= 3.67 && gpa_needed > 3.33)
    printf("\n You need an A- average for your next %i semesters", semester_left);
    else if (gpa_needed <= 3.33 && gpa_needed > 3.00)
    printf("\n You need a B+ average for your next %i semesters", semester_left);
    else if (gpa_needed <= 3.00 && gpa_needed > 2.67)
    printf("\n You need a B average for your next %i semesters", semester_left);
    else if (gpa_needed <= 2.67 && gpa_needed > 2.33)
    printf("\n You need a B- average for your next %i semesters", semester_left);
    else if (gpa_needed <= 2.33 && gpa_needed > 2.00)
    printf("\n You need a C+ average for your next %i semesters", semester_left);
    else if (gpa_needed <= 2.00 && gpa_needed > 1.67)
    printf("\n You need a C average for your next %i semesters", semester_left);
    else if (gpa_needed <= 1.67 && gpa_needed > 1.33)
    printf("\n You need a C- average for your next %i semesters", semester_left);
    else if (gpa_needed <= 1.33 && gpa_needed > 1.00)
    printf("\n You need a D+ average for your next %i semesters", semester_left);
    else if (gpa_needed <= 1.00 && gpa_needed > 0.67)
    printf("\n You need a D average for your next %i semesters", semester_left);
    else if (gpa_needed <= 0.67 && gpa_needed > 0.00)
    printf("\n You need a D- average for your next %i semesters", semester_left);
    else if (gpa_needed <= 0.00)
    printf("\n You grades are good enough to achieve your goal gpa without even passing your next %i semesters.", semester_left);
    
    return 0;
    
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    At a glance...

    Code:
    number = classes * semester_comp;
    int x[number];
    That needs to be done with dynamic memory allocation (i.e. malloc). Even so, looking further down, you appear to want to store character/strings in this so it would have to be a two-dimensional character array and not an integer array since you would be storing up to 3 characters ("A+" for example is 2 plus 1 for the terminating NULL) for each grade.

    Code:
    double A-;
    double "B+";
    double B;
    double "B-";
    double "C+";
    double C;
    double "C-";
    double "D+";
    double D;
    double "D-";
    double F;
    Except for the declarations in blue above, you cannot declare variables like that. Variable names cannot contain things like double-quotes or plus and minus characters.

    Code:
    if (x[i ] == "A")
    	y[i ] = 4.00;
    else if (x[i ] == "A-")
    	y[i ] = 3.67;
    else if (x[i ]=="B+")
    	y[i ] = 3.33;
    else if (x[i ]=="B")
    	y[i ] = 3.00;
    else if (x[i ]=="B-")
    	y[i ] = 2.67;
    else if (x[i ]=="C+")
    	y[i ] = 2.33;
    else if (x[i ]=="C")
    	y[i ] = 2.00;
    else if (x[i ]=="C-")
    	y[i ] = 1.67;
    else if (x[i ]=="D+")
    	y[i ] = 1.33;
    else if (x[i ]=="D")
    	y[i ] = 1.00;
    else if (x[i ]=="D-")
    	y[i ] = 0.67;
    else if (x[i ]=="F")
    	y[i ] = 0.00;
    else printf("\n Incorrect Data Entry Program Failed");
    First, you have no y array anywhere in your program. Second, you cannot compare string (character array) values like you are attempting to do here. You would need to use the strcmp function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    2
    Wow you seem to know exactly how to fix this. Thank you for the help. I still have a problem though I am in a beginners programming course and have a terrible instructor. I don't know how to do the things you are saying. Not having a [y] array and not using double quotes I understand but I don't know what malloc or a terminating NULL are.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    I suggest you start with a much simpler program then, one demonstrating what you do understand and a couple of bits you don't understand.

    Writing a whole bunch of stuff and dumping it on a message board is no way to go.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  3. Problems with DLLEXPORT while updating a program
    By pirata in forum C++ Programming
    Replies: 3
    Last Post: 09-05-2008, 01:00 PM
  4. Program calculation problems?
    By rebel in forum C++ Programming
    Replies: 7
    Last Post: 11-28-2005, 02:31 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

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