Programing tips?

This is a discussion on Programing tips? within the C Programming forums, part of the General Programming Boards category; Hey all, I have little programming experience...I have pretty much been just making this one program and expanding upon it ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    12

    Programing tips?

    Hey all, I have little programming experience...I have pretty much been just making this one program and expanding upon it when I find some new stuff that can be added. It is a small calculator I am pretty proud of it considering how small it was when it started out (It's still small I know) I was just wondering if you all could take a look at it and tell me if there is any thing that I can improve upon. I know I should not use system pause or system CLS lol I should realy stop doing that still trying to figure out the "right" way to clear screen and what not . Any constructive critique is greatly appreciated. Thanks in advance.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define PI 3.14156
    
    int main(int argc, char *argv[])
    {
    int num1,num2,pi,plus,sub,le,ba,ra,ar,he,menu,a,mul;
    float di, ci;
         printf("-----------------------------------------\n"
                "Welcome to my simple Calculation Program!\n"
                "-----------------------------------------\n"
                "(c) copyright 2005 James L. Curtis\n"
                "-----------------------------------------\n");
         system("PAUSE");
         system("CLS");
         printf("   Main Menu \n"); 
         printf("-> 1 - Addition\n");
         printf("-> 2 - Subtraction\n");
         printf("-> 3 - Multiplication\n");
         printf("-> 4 - Area Calculation\n");
         printf("-> ");
         scanf("%d", &menu);
         system("CLS");
        switch (menu) {
    
        case 1:
         printf("Please enter a number ");
         scanf("%d",&num1);
         printf("Please enter another number ");
         scanf("%d",&num2);
         plus = (num1+num2);
         printf("Your answer is %d\n\n",plus);
          break;
        case 2:
         printf("Please enter a number ");
         scanf("%d",&num1);
         printf("Enter the ammount you wish to subtract from the first number ");
         scanf("%d",&num2);
         sub = (num1-num2);
         printf("Your answer is %d\n\n",sub);    
          break;
        case 3:
         printf("Please enter a number ");
         scanf("%d",&num1);
         printf("Enter the ammount you wish to multiply the first by ");
         scanf("%d",&num2);
         mul = (num1*num2);
         printf("Your answer is %d\n\n",mul);    
          break;
        case 4:
         printf("   Select the shape you wish to calculate the area of\n"); 
         printf("-> 1 - Square\n");
         printf("-> 2 - Rectangle\n");
         printf("-> 3 - Triangle\n");
         printf("-> 4 - Circle\n");
         printf("-> ");
         scanf("%d", &a);
    if(a == 1){
         printf("Please enter the length of one side of the square: ");
         scanf("%d",&le);
         printf("\n");
         ar = (le*le);
         printf("The area of the square is %d",ar);
         printf("\n\n");
    }
    else if(a == 2){
         printf("Please enter the length of the rectangle: ");
         scanf("%d",&le);
         printf("Please enter the heigth of the rectangle: ");
         scanf("%d",&he);
         ar = (le*he);
         printf("The area of the square is %d",ar);
         printf("\n\n");
    }
    else if(a == 3){
         printf("Please enter The base of the triangle: ");
         scanf("%d",&ba);
         printf("Please enter The height of the triangle: ");
         scanf("%d",&he);
         ar = (ba/2*he);
         printf("The area of the triangle is %d",ar);
         printf("\n\n");
    }
    else if(a == 4){
         printf("Please enter the diameter of the circle: ");
         scanf("%f",&di);
         ci = (PI*di);
         printf("The circumferance of the circle is %f",ci);
         printf("\n\n");
    }
          break;
    }
      printf("Thank you for using Curtis Calculator v1.2\n\nTo report any errors or to send ideas for future versions\n"
              "Please E-mail me at Jlc_1988@hotmail.com \n\n");
      system("PAUSE");	
      return 0;
    }

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Not bad

    A few hints are:

    1 space out your switch cases stements, it makes them easier to read

    Code:
    case 1:
    code here
    break;
    
    case 2:
    code here
    break
    2 Do not use system pause. If you are not including user entry, use getchar()

    it is defined with stdio.h. If you are using user input, include conio.h as a header

    and then before return 0, enter getch() this will allow the same result as system("pause")

    and looks better too.

    3 Your indentation needs a little work, although it is hard to begin with and does depend

    on the IDE you are using. DevC++ has rather poor indent help where as MSVC++ helps

    you the best although writing C programs in MSVC++ creates warnings.

    Apart from that it is good, keep up the good work

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    I'd also mention that if you are going to use conio.h, which by the way isn't standard, then you should replace system("CLS") with clrscr() which is also defined in conio.h and is safer, neater, and more portable than your call to "CLS".
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    why not use the windows api,which is portable across all WINDOWS ( millions users and still counting) platforms.

  5. #5
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Probably just a personal choice, but in case 4 you use a series of if-else statements based on the value of a, an int.

    You could just use another switch statement for that. Probably a bit easier to read, and, in some case, depending on the compiler, and the range of your values of a, the switch statement might compile to more efficient code than the if-else-if....
    Mr. Blonde: You ever listen to K-Billy's "Super Sounds of the Seventies" weekend? It's my personal favorite.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Location
    Sydney, Australia
    Posts
    40
    Quote Originally Posted by swgh
    it is defined with stdio.h. If you are using user input, include conio.h as a header

    Is that a good idea? As far as I am aware, conio.h is not a standard C header file. It works under some Windows systems/compilers (Borland I think and maybe some others), but I don't think it is ANSI.

    I could be wrong though. Can anyone who knows for sure confirm or deny? Salem perhaps? He ( I assume "he". Sorry if not), seems to really know his stuff when it comes to the standards.

  7. #7
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    If your compiling under windows you're pretty much safe, you would have to be using a pretty awkward development set to be missing it.

    But for the record, conio.h is not standard.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    conio.h isn't a standard header - it isn't even a current header. Only old compilers, and some newer ones wanting to maintain some kind of backward compatibility have it.
    Certainly any new code can easily do without it.

    The average student assignment using the console for input and output shouldn't need any non-standard headers.

    As for pausing, try the FAQ
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    Edit: fixed typo
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    12
    Thank you all for your input...you seem a nice bunch of people with some helpful comments. I'll be sure to try some of your sugestions. I am using Dev C++ By the way, ande if you could point me to a site that would explaim to me exactly how you are supposed to indent your code I would be greatly appreciative. Thanks for the info about the pause thing.
    Last edited by Malthazar; 03-17-2006 at 03:37 PM.

  10. #10
    Registered User
    Join Date
    Dec 2005
    Location
    USA
    Posts
    29
    Hi Malthazar

    Great start in programming in C -- well done.

    I just want to make sure you are aware that you have two variables that aren't used at all...

    Code:
    ra
    and

    Code:
    pi
    It's a minor point, and your compiler probably already pointed this out to you... but... this is just a friendly reminder... if you come to read your source code in 6 months from now, you'll see those variables and wonder what they do... and then only after reading all the code, will you realise that they are not used -- this just wasted your time. As your programs get bigger, this will be a more valid concern... so it is always good to get into a good habit now. Once again, your compiler should point out little issues like unused variables... but unless you remove them... it is still possible you'll come to try and read your code a few months down the line (without compiling)... you'll see those variables and assume they are used for something.

    Anyway... that's my 2 cents worth.

    Eddie

  11. #11
    Registered User
    Join Date
    Dec 2005
    Location
    USA
    Posts
    29
    Hello again Malthazar

    Once again, well done for getting your program up and running without errors.

    Here's an interesting suggestion to make an improvement to your program.

    How about adding input validation to your program. For instance, see what happens when you try to add 15 and Fred. I know that 'Fred' isn't a number... but when you write real programs for others to use, you are going to want to check that the user inputs sensible data.

    Good luck with your continued programming experience.

    Eddie

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > explaim to me exactly how you are supposed to indent your code I would be greatly appreciative.
    http://en.wikipedia.org/wiki/Indent_style

    Actually, indent styles are the subject of holy wars - there are stalwarts firmly entrenched in each camp. It's generally a pretty pointless exercise to get into 'x vs. y' over indent style (much like arguing over which is the best OS/Editor/Compiler/IDE or anything else).

    Being consistent in the style you choose however will make your own life easier, and make it easier for people to read your code, even if they don't happen to agree with the style you choose.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >#define PI 3.14156
    I did notice your value of PI is slightly off. Here's another idea:
    Code:
    #include <math.h>
    .
    .
       const double PI = acos(-1.);
    Last edited by swoopy; 03-17-2006 at 04:07 PM.

  14. #14
    Registered User
    Join Date
    Feb 2006
    Location
    Sydney, Australia
    Posts
    40
    Quote Originally Posted by valis
    If your compiling under windows you're pretty much safe, you would have to be using a pretty awkward development set to be missing it.

    But for the record, conio.h is not standard.
    I'm not sure what constitutes an awkward development set but I am using the latest version of MacOS X (v10.4.5) with the latest version of Xcode (v2.2) and all the latest associated bits and pieces (GCC, GDB, etc.) and my computer doesn't recognise it. In fact, it seems from past experience that my compiler doesn't recognise anything that isn't standard.

    I've only been at C for 7 weeks and have so far come across 3 things that seem to be in common usage but are not strictly standard, and each time my compiler has balked at it. I'm not sure if this is a good thing, a bad thing, or totally neutral thing, but it certainly makes me aware of what is and isn't standard C.

    My setup is totally stock (exactly what the installer DVD installs by default if you include the developers package in the OSX install) and I haven't removed or added anything so I'd assume that anyone using a Mac to develop C code would have the same issues. That would make it very widespread.

    It seems that most of the non-standard C stuff that is in common usage only works on Windows based development setups. I lack the variety of different machines to make a comprehensive comparison, so I am unable to tell if it's the Mac that is the odd one out (by not supporting the non-standard stuff) or if it Windows that is the weird one (because it does support it). I'd expect that, given the similarities between *nix systems and the MacOS, it'd be Windows that would be different from the other two but I really don't know.

    Of course, the majority is not always right so I have no idea which is the best solution. I suspect, like most thing like this that there is a long, long list of pros and cons for all of them.

    I sure wish I could get my hands on a list of the non-standard functions and header files that are in common usage though. It'd be great to be able to know if a function is going to cause me problems before I try to use it

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I just want to make sure you are aware that you have two variables that aren't used at all...
    Code:
    ra
    and
    Code:
    pi
    It's a minor point, and your compiler probably already pointed this out to you
    If Dev-C++ didn't point this out to you, add the following in Compiler Options under Extra Parameters (or something):
    Code:
    -W -Wall -ansi -pedantic
    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.

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

Similar Threads

  1. Programming Tips
    By webmaster in forum General Discussions
    Replies: 18
    Last Post: 12-20-2009, 01:14 PM
  2. Ballon Tips in C++ (Borland C++ Builder 6)
    By dfghjk in forum C++ Programming
    Replies: 4
    Last Post: 05-11-2008, 08:00 PM
  3. Human brain and programing
    By avgprogamerjoe in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 08-27-2007, 04:48 PM
  4. Tips on becoming a competent programmer
    By trickae2 in forum C Programming
    Replies: 16
    Last Post: 08-28-2006, 07:33 PM
  5. C++ Programming Tips
    By gflores in forum C++ Programming
    Replies: 20
    Last Post: 09-14-2004, 07:53 PM

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