Apart from the "fraction" bit, the area of a circle formula looks fine to me. But yeah.. what is that fraction thing supposed to be?
Anyway... you are doing very strange things with your functions, which is why you are getting strange behavior:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
// These are all prototypes. There are no problems here:
void title(void);
char shape(void);
double triangle(void);
double rectangle(void);
double circle(void);
// They simply tell the compiler that the functions exist in your program. So now you can
// call them.
// Although the 'void' in the parameter list is entirely unnecessary... you could just do this:
void title();
char shape();
double triangle();
double rectangle();
double circle();
|
What gets weird is in your main function:
1 2 3 4 5 6 7 8 9
|
int main()
{
void title(); // <- this prototypes 'title' AGAIN, even though you just did it above.
char shape(); // <- same with 'shape'. These two lines do absolutely nothing
// and are completely unnecessary
// You do this again on lines 26, 31, and 36 with circle/rectangle/triangle.
// And again on line 41 with shape.
// Get rid of that crap.
|
And this is probably the biggest goof:
|
while ((shape() == 'C')||(shape() == 'R')||(shape() == 'T'))
|
Each one of those
shape()
s is you calling the function. IE: the function name + parenthesis means you are calling the function. Remember that when you call a function, your program effectively "jumps" to the code inside that function, then "jumps back" once the function is complete.
Knowing that... notice how your 'shape' function prompts the user for input. This means that every time you call the 'shape' function, you will be prompting the user.
On that above line... you are calling the function
three times. This means you will be prompting the user 3 times [max], once for each call.
Now it's not ALWAYS 3 because of the way the || operator works. Basically it'll do the first expression first
(shape == 'C')
and will only do the next one if that is false.
So.... let's walk through what the computer is doing.
First... the computer will do the underlined portion:
|
while ( (shape() == 'C') ||(shape() == 'R')||(shape() == 'T'))
|
Here, it calls 'shape', which prompts the user for input.
If they input 'C', then this entire line of code is done (because the condition is true), and the loop body will execute.
If they input anything else... then it moves on to the next portion:
|
while ( (shape() == 'C') || (shape() == 'R') ||(shape() == 'T'))
|
Here, it calls shape
again, which prompts the user for input
again.
This time, if they input 'R', then the condition is true and the loop body will execute.
Otherwise, it moves onto the last portion:
|
while ( (shape() == 'C') || (shape() == 'R') || (shape() == 'T') )
|
Here, it calls shape
yet again, which prompts the user for input
yet again.
This 3rd time, if they input 'T', then the condition is true and the loop body will execute.
Otherwise, the entire loop condition is false, and the loop will exit.
You probably do not want to call the function 3 times like this. You should be calling it once, and be putting the results in a variable which you can look at several times.
Example:
1 2 3 4 5
|
char usershape = shape(); // call the shape function to ask the user for input, then
// put their input in a 'usershape' variable.
// THEN you can do this without calling the function several times:
while( (usershape == 'C') || (usershape == 'R') || (usershape == 'T') )
|