eriktorres
IS-IT--Management
Coders,
This code is to scan three values, a, b,and c, and calculate the area of a triangle. If the input is negative there needs to be a error, is the value underneath the sqrt is negative there needs to be an error. So the code compiles OK, but it seems to be caught in a loop when a negative number is scanned it also seems to run code in a while loop without needing to. Here is the code.
***********************
Messages at Run:
Any help would be great. Thanks.
et
This code is to scan three values, a, b,and c, and calculate the area of a triangle. If the input is negative there needs to be a error, is the value underneath the sqrt is negative there needs to be an error. So the code compiles OK, but it seems to be caught in a loop when a negative number is scanned it also seems to run code in a while loop without needing to. Here is the code.
Code:
#include <stdio.h>
#include <math.h>
/****Demostrates Declerations****/
void inputsides();
void calcarea();
void scannum(void);
/****Main Function****/
main(){
inputsides();
calcarea();
//printf("Debug main()\n");
}
/****Function Definitions****/
void inputsides(void){
printf("**********************************************************\n");
printf("Please enter three sides A, B, and C to calculate the area\n");
printf("of the triangle, negative numbers will not be calculated!\n");
printf(">>");
scannum();
}
void calcarea(double sidea, double sideb, double sidec){
double insidearea;
double sides = (sidea + sideb + sidec)/2.0;
if ((insidearea = (sides*((sides-sidea)*(sides-sideb)*(sides-sidec))))>0)
{printf("Here is the area:%f", sqrt (insidearea));
}
else{
printf("Areas do not form a triangle");
}
}
void scannum(void){
double aside, bside, cside;
scanf("%lf %lf %lf", &aside, &bside, &cside);
while (aside <= 0 || bside <= 0 || cside <= 0){
while (getchar() != '\n'){
}
printf("You have entered an incorrect number!\n");
printf("Run again.\n");
printf(">>");
scannum();
}
calcarea(aside, bside, cside);
}
Messages at Run:
Code:
[etorres@onyx prog7]$ a.out
**********************************************************
Please enter three sides A, B, and C to calculate the area
of the triangle, negative numbers will not be calculated!
>>999 999 999
Here is the area:432147.109501Areas do not form a triangle[etorres@onyx prog7]$ a.out
**********************************************************
Please enter three sides A, B, and C to calculate the area
of the triangle, negative numbers will not be calculated!
>>-999 999 999
You have entered an incorrect number!
Run again.
>>999 999 999
Here is the area:432147.109501You have entered an incorrect number!
Run again.
>>999 999 999 [COLOR=red]<<------------------LOOP[/color]
Here is the area:432147.109501You have entered an incorrect number!
Run again.
>> [COLOR=red]<<-----------LOOP[/color]
et