eriktorres
IS-IT--Management
Coders,
I have been working on this code for the last few days. I have the just of the code complete, yet one issue still arises. This code needs to calculate the area of a triangle. The sides a, b, and c are provided by the users and the sides are divided by 2, then that answer is calculated to give the area. These formulas are as follows:
A = sqrt((s(s-a)(s-b)(s-c)) where s= ((a+b+c)/2)
If the answer within the sqrt is less then 0 then the numbers provided in a, b, and c do not make a triangle.
Here is what the issue is. The program will work ok it I type in numbers that provide a negative under the sqrt, and a prompt is outputted to the user giving them the error and re-prompts them to reinput data. But if I retype data that is correct, no zero under the sqrt, the output if not correct.
For exmample. Type 2 3 and 6, this should give you the reprompt. Now type 999 999 999, which should give your 432147.1, or there abouts, but all i get is "Here is the area of the triangle 'nan'" and not 432147.1. But if I type 999 999 999 the first time then I do get the right output.
I hope this makes sence. It seems to me that the issue is with the loop somewhere. Any help would be greatly appreciated. I am comipiling under GCC with the -lm.
Code:
************************************************
#include <stdio.h>
#include <math.h>
/****Demostrates Declerations****/
float aside;
float bside;
float cside;
float sides;
float area;
float insidearea;
void calcsides(void);
void calcarea(void);
void inputsides(void);
void scannum(void);
/****Main Function****/
main(){
inputsides();
calcsides();
calcarea();
//area = sqrt(insidearea);
printf("Here is the area of the triangle");
printf(" %.4f\n", area);
//printf(" %.4f\n", sqrt((sides*((sides - aside)*(sides - bside)*(sides - cside)))));
}
/****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();
while(aside <= 0 || bside <= 0 || cside <= 0){
while(getchar() != '\n'){
}
printf("You have entenred an incorrect number!\n");
printf("Try again.\n");
printf(">>");
scannum();
}
calcsides();
}
void calcsides(void){
sides = ((aside + bside + cside) / 2);
}
void calcarea(void){
insidearea = (sides*((sides - aside)*(sides - bside)*(sides - cside)));
//area = sqrt(insidearea);
if (insidearea < 0){
printf("Error: Lengths do not form a triangle!\n");
printf("\n");
inputsides();
}
area = sqrt(insidearea);
}
void scannum(void){
scanf("%f %f %f", &aside, &bside, &cside);
}
***************************************************
I have been working on this code for the last few days. I have the just of the code complete, yet one issue still arises. This code needs to calculate the area of a triangle. The sides a, b, and c are provided by the users and the sides are divided by 2, then that answer is calculated to give the area. These formulas are as follows:
A = sqrt((s(s-a)(s-b)(s-c)) where s= ((a+b+c)/2)
If the answer within the sqrt is less then 0 then the numbers provided in a, b, and c do not make a triangle.
Here is what the issue is. The program will work ok it I type in numbers that provide a negative under the sqrt, and a prompt is outputted to the user giving them the error and re-prompts them to reinput data. But if I retype data that is correct, no zero under the sqrt, the output if not correct.
For exmample. Type 2 3 and 6, this should give you the reprompt. Now type 999 999 999, which should give your 432147.1, or there abouts, but all i get is "Here is the area of the triangle 'nan'" and not 432147.1. But if I type 999 999 999 the first time then I do get the right output.
I hope this makes sence. It seems to me that the issue is with the loop somewhere. Any help would be greatly appreciated. I am comipiling under GCC with the -lm.
Code:
************************************************
#include <stdio.h>
#include <math.h>
/****Demostrates Declerations****/
float aside;
float bside;
float cside;
float sides;
float area;
float insidearea;
void calcsides(void);
void calcarea(void);
void inputsides(void);
void scannum(void);
/****Main Function****/
main(){
inputsides();
calcsides();
calcarea();
//area = sqrt(insidearea);
printf("Here is the area of the triangle");
printf(" %.4f\n", area);
//printf(" %.4f\n", sqrt((sides*((sides - aside)*(sides - bside)*(sides - cside)))));
}
/****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();
while(aside <= 0 || bside <= 0 || cside <= 0){
while(getchar() != '\n'){
}
printf("You have entenred an incorrect number!\n");
printf("Try again.\n");
printf(">>");
scannum();
}
calcsides();
}
void calcsides(void){
sides = ((aside + bside + cside) / 2);
}
void calcarea(void){
insidearea = (sides*((sides - aside)*(sides - bside)*(sides - cside)));
//area = sqrt(insidearea);
if (insidearea < 0){
printf("Error: Lengths do not form a triangle!\n");
printf("\n");
inputsides();
}
area = sqrt(insidearea);
}
void scannum(void){
scanf("%f %f %f", &aside, &bside, &cside);
}
***************************************************