I want to recieve comments!

This is a discussion on I want to recieve comments! within the C Programming forums, part of the General Programming Boards category; Hi every one iam new to c programming and i would like to receive comments in the following code. In ...

  1. #1
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    I want to recieve comments!

    Hi every one iam new to c programming and i would like to receive comments in the following code. In how to improve this skills!
    I would also want this grade program to go in a loop, like instead of return 0; will return to main and do ask the user for their grade. How can i do that!
    Thanks, for your help!

    Code:
    /*This program will print the letter grade according to the numeric grade*/
    
    #include <stdio.h>
    main()
    {
     int grade;
     printf("Please enter your grade\n");
     scanf("%d", &grade);
     if (grade >= 90)
     printf("A\n");
     else if(grade >= 80)
     printf("B\n");
     else if(grade >= 70)
     printf("C\n");
     else if(grade >= 60)
     printf("D\n");
     else if(grade >= 50)
     printf("F\n");
     else
     printf("You should get a personal tutor immediatly\n");
      
     return 0;
    }

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Code:
    main()
    And main() is of what return type? int. Say as such, it never hurts:
    Code:
    int main()
    I personally use a tab as my indent, if you use spaces, I would recommend more than 1.

    On if() statements that don't require { }'s, I tend to either write:
    Code:
    if(blah) blah;
    Or, if I move it to the next line,
    Code:
    if(blah)
       blah;
    ... then I indent it, as above. This avoids some confusion with what the if statement actually deals with.

    How to make it repeat? You could stick it in a while loop:
    Code:
    int main()
    {
      int grade;
      
      while(1)
      {
        printf("Please enter your grade:\n");
        ...
      }
      return 0;
    }
    But that will go on forever, so you'll want to provide a way out. (Entering something like -1 to quit, etc.)

    Hope that helps!
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    I recommend you get this book: The C Programming Language by Brian W. Kernighan and Dennis M. Ritchie.
    Code:
    main()
    Should be:
    Code:
    int main()
    Here is a simple loop construct to be executed 5 times:
    Code:
    int counter;
    for(counter=0; counter<5; counter++)
    {
        \* to do. *\
    }
    edit
    Darn it, beaten again!
    /edit
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,625
    puts() does everything for your program that printf() does in a much easier way. printf was made to validate format strings, which your program doesn't use. puts writes a string to the stdout and appends a newline character.

    Before you loop your program, I would feel better if you got rid of scanf entirely.Use fgets() to read in a string from the user, and then you can use the function strtol in <stdlib.h> to turn it into a number.
    Here's an example:
    Code:
    char userin[40];
    char *end = NULL;
    puts("Enter your grade");
    fgets(userin, sizeof userin, stdin);
    long grade = strtol(userin, &end, 10);
    more about this in the FAQ

    Now as for looping the program, after you output the grade, ask the user if he's like to exit. Use his answer as a loop condition.
    Last edited by whiteflags; 05-10-2006 at 10:13 PM.

  5. #5
    wise_ron wise_ron's Avatar
    Join Date
    May 2006
    Posts
    75

    Thanks

    I really apreciate your help guys thanks for your advice and tips. This forum rocks because you all...

    Citizen i will try do as you sayed and i will post it later on, i dont get it right now iam readiing that tutorial in cprogramming.c : FAQ

    thanks

    wise_ron

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. removing comments of type '//' and '/*'
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2007, 02:24 AM
  2. text rpg problem
    By xxwerdxx in forum C Programming
    Replies: 19
    Last Post: 12-19-2005, 07:23 AM
  3. need help with my text rpg
    By xxwerdxx in forum Game Programming
    Replies: 0
    Last Post: 12-08-2005, 08:13 PM
  4. answer calls and setup to recieve data using TAP
    By mk_xyon in forum C Programming
    Replies: 3
    Last Post: 11-13-2004, 07:39 PM
  5. The Art of Writing Comments :: Software Engineering
    By kuphryn in forum C++ Programming
    Replies: 15
    Last Post: 11-23-2002, 04:18 PM

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