1. ## 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[])
{
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("-> 2 - Subtraction\n");
printf("-> 3 - Multiplication\n");
printf("-> 4 - Area Calculation\n");
printf("-> ");
system("CLS");

case 1:
scanf("%d",&num1);
scanf("%d",&num2);
plus = (num1+num2);
break;
case 2:
scanf("%d",&num1);
printf("Enter the ammount you wish to subtract from the first number ");
scanf("%d",&num2);
sub = (num1-num2);
break;
case 3:
scanf("%d",&num1);
printf("Enter the ammount you wish to multiply the first by ");
scanf("%d",&num2);
mul = (num1*num2);
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;
}```

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. 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".

4. why not use the windows api,which is portable across all WINDOWS ( millions users and still counting) platforms.

5. 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....

6. 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. 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. 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

9. 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.

10. 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. 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. > 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.

13. >#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.);```

14. 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. 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`