Thread: Calculating A Desired Grade Using C

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    28

    Question Calculating A Desired Grade Using C

    Hello,

    I am trying to create a program using C where the user enters a desired grade (A, B, C) , along with the minimum average needed for that grade (90%, 80%, 70%), their current average, and how much the final counts as a percentage of the course grade (25%).

    The program should output:

    You need a score of ... on the final to get a ...

    Currently, I am getting a error that says "expected expression before '%' token on the line where it says grade_calc =

    This is what I have so far and my new function is giving me a headache with new debugging problems. If someone could help me fix my current problem and give me a nudge in the right direction as to what to do I would greatly appreciate it.

    insert
    Code:
    #include <stdio.h>
    
    int
    main()
    {
    
        char grade[5];
        float minimumavg[5];
        float currentavg[5];
        float finaltest[5];
        float newgrade [5];
    
        printf("Enter desired grade: \n");
        scanf(" %c", grade);
    
        printf("Enter minimum average required: \n");
        scanf("%f", &minimumavg);
    
        printf("Enter current average in course: \n");
        scanf("%f", &currentavg);
    
        printf("Enter how much the final accounts for as a percentage: \n");
        scanf("%f", &finaltest);
    
    
        printf("You need a score of %f on the final to get a % c" , &newgrade, &grade);
    
    double
    grading(double x, float currentavg, float finaltest)
    {
        double grade_calc;
    
         grade_calc = %f * x + (1 - %f) * %f , &finaltest, &finaltest, &currentavg;
    
         return (grade_calc);
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    char grade[5];
    float minimumavg[5];
    float currentavg[5];
    float finaltest[5];
    float newgrade [5];
    Why do you use arrays?

    Code:
    printf("You need a score of %f on the final to get a % c" , &newgrade, &grade);
    There shouldn't be a space between "%" and "c".

    Code:
    grade_calc = %f * x + (1 - %f) * %f , &finaltest, &finaltest, &currentavg;
    That's just wrong. You don't need format specifiers if you want to use variables in expressions. Just write it like:
    Code:
    grade_calc = finaltest * x + (1 - finaltest) * currentavg;
    You are also missing the return statement and the closing } in main().

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    Thank you Andreas. I thought the square brackets represented the length of the input, not arrays. I am just learning this for the first time and I greatly appreciate the help. After I did your suggestions, and changed the code to this:

    Code:
    #include <stdio.h>
    
    int
    main()
    {
    
        char grade;
        float minimumavg;
        float currentavg;
        float finaltest;
        float newgrade;
    
        printf("Enter desired grade: \n");
        scanf("% c", grade);
    
        printf("Enter minimum average required: \n");
        scanf("%f", &minimumavg);
    
        printf("Enter current average in course: \n");
        scanf("%f", &currentavg);
    
        printf("Enter how much the final accounts for as a percentage: \n");
        scanf("%f", &finaltest);
    
    
        printf("You need a score of %f on the final to get a % c" , &newgrade, &grade);
    
        return(0);
    }
    
    
    
    double
    grading(double x, float currentavg, float finaltest)
    {
        double grade_calc;
    
         grade_calc = finaltest * x + (1 - finaltest) * currentavg;
    
         return (grade_calc);
    }
    
    
    
    
    I got an output of this when I entered "B":
    
    Enter desired grade:
    B
    Enter minimum average required:
    Enter current average in course:
    Enter how much the final accounts for as a percentage:
    You need a score of 0.000000 on the final to get a b
    It successfully runs now, its just not asking for the inputs after I enter in the grade. How can I get it to ask the minimum average, current average, without skipping it?

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    The warnings hold the answer

    Code:
    linux05:/home/users/std10093>gcc -Wall px.c -o px
    px.c: In function 'main':
    px.c:13: warning: format '%c' expects type 'char *', but argument 2 has type 'int'
    px.c:26: warning: format '%f' expects type 'double', but argument 2 has type 'float *'
    px.c:26: warning: ' ' flag used with '%c' printf format
    px.c:26: warning: format '% c' expects type 'int', but argument 3 has type 'char *'
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    You need address operator when scanning char:

    Code:
    printf("Enter desired grade: \n");
        scanf("%c", &grade);
    You are using address operator while printing variables, it will just print their memory addres not their value, try this:

    Code:
    printf("You need a score of %f on the final to get a %c" , newgrade, grade);

    You did not called your function grading??? You need to call function in your main.

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    Thank you so much for all the help. I have made the correct changes with the address operators, but the last thing that is confusing me is the function that I have preforming the calculations. It is outside the main function, and I want it to compute the value needed and send it back to the main function.

    I have tried debugging it and I still cannot get the other function to work. This is what I have for my other function besides main called grading:

    Code:
    double
    grading(double x, float currentavg, float finaltest)
    {
        double newgrade;
    
         newgrade = finaltest * x + (1 - finaltest) * currentavg;
    
         return (newgrade);
    And this is the last line in the function main where it should take the value it gets from the other function and feed it in here:

    Code:
    printf("You need a score of %f on the final to get a %c" , newgrade, grade);
    I am getting an output of 0.0000000 needed to get a B. Why isn't my other function working?

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by M_A_T_T View Post
    Why isn't my other function working?
    Which line of code is calling the function?
    Is it storing the result of calling the function?

    Edit: FAQ that might help: Functions in C - Cprogramming.com

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Not Getting Desired Output
    By DevoAjit in forum C Programming
    Replies: 2
    Last Post: 03-09-2012, 05:46 AM
  2. Not getting the desired o/p with ~ operator. Kindly help.
    By DevinLgls in forum C Programming
    Replies: 5
    Last Post: 05-24-2011, 12:03 PM
  3. Replies: 2
    Last Post: 01-29-2011, 12:58 PM
  4. Much desired program.
    By MannyCalavera in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 09-23-2005, 05:40 PM
  5. not getting the desired output. WHY???
    By KristTlove in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2003, 02:08 PM