Thread: C 101 critique, please?

  1. #1
    Registered User adobephile's Avatar
    Join Date
    Dec 2002
    Posts
    8

    C 101 critique, please?

    As a beginner's programming exercise I wrote this "letter grade calculator":

    Code:
    #include <stdio.h>
    char	line[100];	/* line of data for input */
    int	grade;		/* numeric grade */
    
    int main()
    {
        while (1) {
            printf("Enter the numeric grade\n");
            printf("  or 0 to stop:");
            fflush(stdout);
            
            fgets(line, sizeof(line), stdin);
            sscanf(line, "%d", &grade);
            
            if (grade == 0)
                break;
            else if
                (grade > 0)
                    if
                    (grade <= 60)
                        printf("F\n");
                    else if
                    (grade <= 70)
                        printf("D\n");
                    else if
                    (grade <= 80)
                        printf("C\n");
                    else if
                    (grade <= 90)
                        printf("B\n");
                    else if
                    (grade <= 100)
                        printf("A\n");
        }
        return (0);
    }
    which looks like this on the console:

    Enter the numeric grade
    or 0 to stop:66
    D
    Enter the numeric grade
    or 0 to stop:77
    C
    Enter the numeric grade
    or 0 to stop:33
    F
    Enter the numeric grade
    or 0 to stop:88
    B
    Enter the numeric grade
    or 0 to stop:100
    A
    Enter the numeric grade
    or 0 to stop:81
    B
    Enter the numeric grade
    or 0 to stop:0

    Critiques, please! Is there a way to double up the comparison operators around the variable to test each range?

  2. #2
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    I don't know what curriculum topics you are trying to show here, but if global variables isn't one of them you should probably put your grade and line[] variables inside of main(). Also you could condense your test of grade from

    Code:
    if (grade == 0)
                break;
            else if
                (grade > 0)
    to the single operation of

    Code:
    if (grade >0)
           {
         /* do test operations */
    }else{
         break;
           }
    Hopefully this will help you. Also, when you ask for critiques, try to not only give areas you think could be improved, but also give your ideas of how to actually change the code. This will give us more of an idea of what you're thinking of and how much you know . Most of us around here don't like it when you ask a question without trying to answer it yourself . People are more likely to respond when you do.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Critiques, please!
    Don't use global variables unless you have to. In your case, you don't.

    >>sscanf(line, "%d", &grade);
    Choose a more controllable method to convert data, like strtol(). At the very least, check the return code from sscanf() to ensure it did what you asked.


    >printf("F\n");
    You don't have a print for grade E. Did you mean to?

    As for the if tests, a simple layout change would be beneficial:
    Code:
    if (grade <= 0 || grade > 100) break;
    
    if      (grade <= 60)   printf("F\n");
    else if (grade <= 70)   printf("D\n");
    else if (grade <= 80)   printf("C\n");
    else if (grade <= 90)   printf("B\n");
    else                    printf("A\n");
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    Re: Hammer

    Are you aware that in America we don't have an 'E' letter grade?

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Draco
    Re: Hammer

    Are you aware that in America we don't have an 'E' letter grade?
    No, I wasn't, my mistake Seems a bit strange, why have ABCDF, and no E.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463

    I don't know why not. Just like you Brits, the way we do some things is a little weird to other people. I'll admit, after taking physics our system of measurement seems a little odd compared to metrics.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    403
    there is a tiny optimization you could make, since you are outputting \n everytime you could simply output that after the if-else if-else block. I see that more in C++ (cout << endl so maybe it's just a style thing for C only programmers.


    useless information:
    some schools in the US actually do use E instead of F, F is F simply to denote failure which I guess someone decided was too harsh, when we took some US standardized test 2 years ago (I forget, maybe the PSAT) and you had to bubble your average grade in some subjects the options were A,B,C,D,E/F.

  8. #8
    Registered User adobephile's Avatar
    Join Date
    Dec 2002
    Posts
    8
    Thanks to all for your helpful replies so far on my thread.

    I suppose any single book on the subject would present C from the author's viewpoint and involve that person's particular strategy of presentation. Though I remember reading about global variables in passing, I hadn't realized that that's what I had constructed in this exercise, as I was merely following the author's example coding so far in the book.

    For what it's worth this C language is unfolding for me nicely from the book and from your respective inputs. I intend to return the favors in due course.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, you could streamline it a bit like:


    Code:
    #include <stdio.h>
    
    char         letter;
    char	line[100];	
    int	grade;
    
    
    int main()
    {
        while (1) {
            printf("Enter the numeric grade\n");
            printf("  or 0 to stop:");
            fflush(stdout);
            
            fgets(line, sizeof(line), stdin);
            sscanf(line, "%d", &grade);
            
                    if(grade > 0 && grade <= 60)
                        letter = 'F';
                    else if
                    (grade <= 70)
                        letter = 'D';
                    else if
                    (grade <= 80)
                        letter = 'C';
                    else if
                    (grade <= 90)
                        letter = 'B';
                    else if
                    (grade <= 100)
                        letter = 'A';
                    else break;
              
             printf("%c\n", letter);       
    
        }
        return (0);
    }


    But yeah, looks good for newb code.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Registered User adobephile's Avatar
    Join Date
    Dec 2002
    Posts
    8
    Thanks, Sebastiani, for the tips and the compliment!

  11. #11
    Guest21
    Guest
    Code:
    if(grade > 0 && grade <= 60)
        letter = 'F';
    else if
        (grade <= 70)
            letter = 'D';
    This won't work too well. Entering 0 or less will result in a grade D and will never exit the loop.

  12. #12
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    Re: Guest21

    Good catch on that. I'd just like to point out that it would actually be an 'A' letter. Notice that all the letter grades above F have the same test as grade D.

    Code:
    if(grade > 0 && grade <= 60)
                        letter = 'F';
                    else if
                    (grade <= 70)
                        letter = 'D';
                    else if
                    (grade <= 80)
                        letter = 'C';
                    else if
                    (grade <= 90)
                        letter = 'B';
                    else if
                    (grade <= 100)
                        letter = 'A';
                    else break;
    Since a negative number (or zero) would be less than one it would follow all the letters up to A.

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >> I'd just like to point out that it would actually be an 'A' letter.
    No, it wouldn't. Seb's code gives D in response to 0 (or less than) being entered. You only need follow the code as quoted by Guest21, no need to read any further.

    Still don't believe it: Compile it and see for yourself.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    I don't know how I thought of that. I was thinking of switch statements. My bad

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List question from a newb(Need code Critique)
    By Gatt9 in forum C++ Programming
    Replies: 2
    Last Post: 01-08-2006, 03:11 AM
  2. Critique my StringTokenizer please...
    By maxhavoc in forum C++ Programming
    Replies: 10
    Last Post: 11-24-2004, 09:08 PM
  3. College courses and 101
    By DeepFyre in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 11-18-2004, 08:40 PM
  4. Critique my program
    By Jason Spaceman in forum C Programming
    Replies: 6
    Last Post: 10-26-2004, 05:38 PM