Hi Jordy,
First of all, good effort in fixing up your program. Lots of beginners make the mistake of saying "It works, so I will leave it now". But you have made some improvements - so GOOD WORK !
However there are still a few improvements to make plus some new concepts.
The first is that you have chained all your functions together, there is nothing illegal about this, but it is more organised in this situation & IMO to call functions from main. The
menu
function is fine as
void
, but
areaoftriangle
should return a value, and the display of the results could either be done in
main()
or in it's own function.
A big help to beginners, even though it's boring, is to write psuedo code as comments. This allows one to identify functions, the control flow (looping, branching etc) structure of the program, even variable names. This can be an iterative process - you can start as simple as you like then go back & add more detail & refine things as much as you want. Leave the comments in the code, as they can serve as a bit of documentation. Variable & function names should be self documenting, that is the name says what it is, or does .
Here is some psuedo code to start with, I have put a bit of code in as well:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
includes
function declarations
main function
double TriangleArea = 0.0;
double CircleArea =0.0;
bool Quit = false;
//start loop: // use a while loop
// DisplayMenu - a void function - displays the text only
// GetMenuSelection - function return an unsigned int or a uppercase char use toupper function
// DoMenuSelection // this could be a function needs MenuSelection as argument
switch (MenuSelection) {
//case 'A' : // or numbers if you wish
//call function to do triangle area
// TriangleArea = CalcTriangleArea;
// call function to display results
// break;
//case 'C' :
// call function to do circle area say
// call function to display results
//break;
//case 'Q' : // user wants to quit
Quit = true;
break;
//default : // catch bad input
//Process bad input
break;
}
//end loop:
other function definitions
|
With functions, write a few lines of comments, stating the
preconditions. That is what type of value does the function expect & what rage of values are valid. Also state the
post conditions - what the function returns including it's type and range. For example the area function should return a positive
double
- which prompts the question of what to do with negative sine values (180 < theta < 360).
With variables, always initialise them to something that might give a clue that things have gone wrong. For example MenuSelection = 'z'
Here is some info about the switch- near the bottom
http://www.cplusplus.com/doc/tutorial/control/
There is lots of good stuff in the tutorial, also the reference pages & articles at the top left of this page.
Here is an example of a menu & switch - not a complete program, but you can see how it works.
Hope all goes well :+)