i want you to criticize my code, so i can improve it

This is a discussion on i want you to criticize my code, so i can improve it within the C Programming forums, part of the General Programming Boards category; when i compile the program, the avg is displaying a garbage value (-1.#J) because i did 0/0 which is an ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    62

    i want you to criticize my code, so i can improve it

    when i compile the program, the avg is displaying a garbage value (-1.#J) because i did 0/0 which is an error. So my question is how can i fix this. Also, when i want to input a string character how can i make the program to read the whole string character. For example if i type in (ghjk) it should be not valid. But instead its reading (g). Also, my program is reading a negative value how can i stop that it should say "inproper input".
    Last edited by joker_tony; 03-29-2008 at 09:01 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Comments on the code:
    1. Single-letter variable names should never appear except in a few circumstances such as loop indexes and x,y,z coordinates (where the one letter actually tells you what it is -- and even then you probably shouldn't use x, y, and z). The variable's name should tell you what it is, like rice_purchased -- can you tell me (without looking back!) what the variable "x" does in your code?
    2. fflush(stdin) does not exist in standard C. There's an FAQ entry on how to get rid of the rest of the things in the input buffer. And of course, the one time you don't use it is the one time you keep complaining about getting extra letters.
    3. Putting calculations inside printf statements is extremely poor programming. Move the calculations to before the results are printed. --NOTE: This is where your average #1.J comes back to kick you; you don't have a chance to do something separate in the case where nothing has been purchased yet, since your calculation is buried inside a print statement.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. How to improve code
    By rugby in forum C Programming
    Replies: 3
    Last Post: 04-15-2003, 09:24 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. help improve my code
    By lambs4 in forum C Programming
    Replies: 3
    Last Post: 11-21-2001, 10:33 AM

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