Thread: plz help me make my assigment more bulletproof

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

    emergency please help me make my program more bulletproof before i fail this course

    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".
    Code:
    
    
    Last edited by joker_tony; 03-29-2008 at 01:29 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Firstly, don't use fflush(stdin).
    cpwiki.sf.net/fflush
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    Code:
    while(time!='q'&& time!='Q');
    That's the same as
    Code:
    while(tolower(time) != 'q');
    if you have <ctype.h> included. And time() is a standard function, so calling a variable that is not the best idea. It works, however, at least until you try to call time().

    tolower()/toupper() would make your switch statement a great deal smaller. You should look into it. http://www.cprogramming.com/fod/tolower.html

    Also, instead of having all of those variables (a, b, c, d, ...), consider looking into arrays. http://www.cprogramming.com/tutorial/c/lesson8.html

    Code:
    #define    A       "smarties"
    #define    A_P     4.55
    #define    B       "Flour   "
    #define    B_P     1.5
    printf() can add spaces for you. &#37;10s pads the string with spaces to be at least 10 characters long, and %-10s will left align it, printing the spaces on the right. (%10s will give you " one", %-10s will give you "one ".) For example:
    Code:
    printf("%-10s something\n%-10s else\n", "one", "two";
    prints
    Code:
    one        something
    two        else
    Now for your real questions.
    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.
    Whenever you do division, check to see if you're dividing by zero. For example:
    Code:
    if(y == 0) puts("Division by zero");
    else printf("= %f\n", x / y);
    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).
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    In essence, you need to declare variables as arrays:
    Code:
    char data[10];  /* a string with a maximum length of 9 (add one for the NULL) */
    Then read them with, say, fgets():
    Code:
    fgets(data, 10, stdin);
    Then, if you use fgets(), remove the newline from the end of the string:
    Code:
    char *p;
    p = strchr(data, "\n");
    if(p) *p = 0;
    Then compare the string with strcmp,
    Code:
    if(strcmp(data, "yes") == 0) {
        puts("You entered \"yes\".");
    }
    or, if you like, examine the first character with data[0].
    Last edited by dwks; 03-28-2008 at 04:34 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > p = strchr(data, "\n");
    dwks, means
    Code:
    p = strchr(data, '\n');

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    ...
    printf("\n\n\tA\tSmarties\t%3.2f\t%8.2f\t$%8.2f",A_P,a,sub1=a*A_P);
    ...
    printf("\n\n\n\ttotal pounds =\t%8.2f\tSUBTOTAL\t$%8.2f",
              i=a+b+c+d+e+f+g+h,run=sub1+sub2+sub3+sub4+sub5+sub6+sub7+sub8);
    Excuse me while I go away and puke.

    This printf is unreadable enough without the assignment inside it. Do at least this:
    Code:
    ...
    sub1=a*A_P;
    printf("\n\n\tA\tSmarties\t%3.2f\t%8.2f\t$%8.2f",A_P,a,sub1);
    ...
    run=sub1+sub2+sub3+sub4+sub5+sub6+sub7+sub8;
    i=a+b+c+d+e+f+g+h;
    printf("\n\n\n\ttotal pounds =\t%8.2f\tSUBTOTAL\t$%8.2f", i, run);
    Using single letter variable names is probably not the greatest idea.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    What a mess. Adding some spaces between your operators would also make the code much easier to read. And you really need to simplify some of your statements because they are very difficult to follow. It's ugly not to mention highly error prone.

    Also are you working with floats or doubles? Floats would work just fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [Request] Need Help Plz
    By TylerD in forum Tech Board
    Replies: 4
    Last Post: 01-03-2009, 09:54 AM
  2. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM
  3. make all rule
    By duffy in forum C Programming
    Replies: 9
    Last Post: 09-11-2003, 01:05 PM
  4. Question about atheists
    By gcn_zelda in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 08-11-2003, 11:50 AM
  5. 'functions' in make?
    By mart_man00 in forum C Programming
    Replies: 1
    Last Post: 06-21-2003, 02:16 PM