Help with program

This is a discussion on Help with program within the C Programming forums, part of the General Programming Boards category; I worte this program to convert the examination scores to letter grades but the output isn't coming out correctly. Here's ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    41

    Help with program

    I worte this program to convert the examination scores to letter grades but the output isn't coming out correctly. Here's what i have:
    Code:
     
    
    #include <stdio.h> 
    #include <stdlib.h> 
    
    int getGradeValue(int, int); 
    //char letterGrade(int*, int, int, int, int, int, int, int ,int); 
    void displayGrade( int*, char*); 
    char makeLetterGrade(char); 
    
    int main(void) 
    { 
    char letter; 
           int score; 
           getGradeValue(0,110); 
    makeLetterGrade(letter); 
    displayGrade(&score, &letter); 
    
    return EXIT_SUCCESS; 
    } 
    int getGradeValue(int mingrade, int maxgrade) 
    { 
    int score; 
           mingrade = 0; 
           maxgrade = 110; 
    
           printf("Enter a value, between 0 and 110, representing the\n"); 
           printf("grade for the examination\n"); 
           scanf("%d", &score); 
           if (0<= &score <= 110) 
    { 
           return(score); 
    } 
    else 
    { 
           printf("Invalid grade\n"); 
    } 
           printf("Enter a value, between 0 and 110, representing the\n"); 
           printf("grade for the examination\n"); 
           scanf("%d",&score);    
    
           return EXIT_SUCCESS; 
    } 
    
    char makeLetterGrade(char letter) 
    { 
    //char letterGrade( int *score, int a_max, int a_min, int b_max, int b_min, int c_max, int c_min, int f_max, int f_min) 
    int *score,  a_max,  a_min, b_max,  b_min,  c_max,  c_min,  f_max,  f_min; 
    char A; 
    char B; 
    char C; 
    char F; 
    char X; 
    
           printf("Enter max and min grade value for A\n"); 
           scanf("%d %d", &a_max, &a_min); 
    
           printf("Enter max and min grade value for B\n"); 
           scanf("%d %d", &b_max, &b_min); 
    
           printf("Enter max and min grade value for C\n"); 
           scanf("%d %d", &c_max, &c_min); 
    
           printf("Enter max and min grade value for F\n"); 
           scanf("%d %d", &f_max, &f_min); 
    
    if (a_max>=*score&&*score>=a_min) 
    letter = A; 
    else if (b_max>=*score&&*score>=b_min) 
    { 
           letter = B; 
    } 
    else if (c_max>=*score&&*score>=c_min) 
    { 
           letter = C; 
    } 
    else if (f_max>*score&&*score>=f_min) 
    { 
           letter = F; 
    } 
    else 
    { 
           letter = X; 
    } 
    return letter; 
    } 
          
    void displayGrade (int *score, char *letter) 
    { 
           static int nscore = 0; 
           int tscore; 
    
           printf("The score of %d earned a grade of %c\n", *score, *letter); 
           printf("%d scores were entered so far.\n", ++nscore ); 
           printf("All scores entered so far total %d\n", tscore); 
           printf("The average score is %d\n", tscore/nscore); 
    
    }
    This is the output:

    The score of 7106548 earned a grade of
    1 scores were entered so far.
    All scores entered so far total 85
    The average score is 85

    The problem is the first line. any suggestions on why i'm getting that value for score and letter? Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your test 0 <= &score <= 110 does not do what you think it does, unless you think it compares 0 to the memory address of the variable score, and then compares that truth-value to 110.

    You return the score from getGradeValue, but since you don't assign it anywhere in main(), it just goes away on a puff of smoke. Note that just because you use the same variable names everywhere, does not in any way mean that they are the same variables.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    It's a scope issue.

    You've declared "score" in main, but never initialized it. You have "score" also defined in your other functions, but they have no relation to score in main(), and that's the copy being passed to displayGrade().

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    I changed my code to this:
    Code:
    int getGradeValue(int mingrade, int maxgrade)
    {
            int value;  
            mingrade = 0;
            maxgrade = 110;
            
            printf("Enter a value, between 0 and 110, representing the\n");
            printf("grade for the examination\n");
            scanf("&#37;d", &value);
            if (0<= value <= 110)
    {
            return(value);
    }
    But i still get the same results. now i return value and use

    displayGrade(value, letter); in my main function but it still gives me the same thing.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You don't appear to have changed anything that matters. Your if-statement is still b0rken, and do you assign the return value to anything in main(), i.e., like so:
    Code:
    value = getGradeValue(0,110)
    As an aside, why do you pass mingrade and maxgrade into your function if you completely ignore their values and set them to 0 and 110 again?

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    if (0<= value <= 110)
    While mathematically this makes sense, it doesn't mean the same in C. Such an animal is generally expressed:
    Code:
    if (0<= value && value <= 110)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > getGradeValue(0,110);
    To use the returned value from a function, assign it to something:
    score = getGradeValue(0,110);

    >char makeLetterGrade(char letter)
    score should be an argument to this function:
    char makeLetterGrade(char letter, int score)

    >letter = A;
    The following makes more sense:
    letter = 'A';

    >void displayGrade (int *score, char *letter)
    There's really no reason to pass these as pointers. Why? Because displayGrade does not change either score or letter. So this would make more sense:
    Code:
    void displayGrade (int score, char letter)

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    Ok...i got the score to appear in the output but i'm a little confused that if i do this:
    Code:
            letter = makeLetterGrade(?????);
    what do i put in ???

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I think all you need to pass is the score.
    Code:
            letter = makeLetterGrade(score);

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    Thanks so far so good. i think that my
    Code:
    char makeLetterGrade(char letter)
    isn't working corectly b/c it returns an X if i enter 86.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    When you compare the score with the user inputed max and min's don't you point to score?

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I would guess makeLetterGrade should be passed a score, not a letter, unlike what you've declared here:
    >char makeLetterGrade(char letter)

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    Okay. program is working. Thanks a bunch. Now if i wanted to enter s5 scores before the program terminates i know i need to insert a loop but i'm a lil lost here b/c in the main function is where the loop should be but how do you loop functions?

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    how do you loop functions
    what is the difference from looping any other type of statements?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    on your Nested IF statments for grades
    I would recomend a Switch

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

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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