Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Not Sure What the Issue Is 3

Status
Not open for further replies.

eriktorres

IS-IT--Management
Apr 15, 2005
10
US
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.


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]
Any help would be great. Thanks.

et
 
First of all, you should NOT be calling [tt]scannum()[/tt] from within [tt]scannum()[/tt]. You've made it a recursive function which is NOT what you want here. You should set up your loops elsewhere.

Next, your logic in [tt]scannum()[/tt] is wrong. The [tt]while (getchar() != '\n')[/tt] isn't doing what you think it is. That's what's keeping you in the loop asking for more dimensions.

Hope this helps.
 
SamBones or Coders,

Even after I remove the getchar() and move the scannum() I still get the following return:
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.109501
Areas do not form a triangle

As you can see the "Areas do not for a triangle" is still printed, even though it is in the else portion of the if. Any ideas? Thanks.

et
 
SamBones and Coders,

Also how do you recommend that I move the scannum()? Now I just put it in another function which is calls the scannum(). Thanks.
 
You can't correct your design defects (see your previous posts in the other thread on the same issue).
Try to adopt this:
Code:
typedef struct 
{
  double x, y, z;
} Triangle;

void scannum(Triangle* pt);
void inputsides(Triangle* pt);
double calcarea(Triangle* pt);

int main()
{
	Triangle t;
	double   area;

    inputsides(&t);
    area = calcarea(&t);
	if (area > 0.0)
		printf("Here is the area:%g\n",area);
	else
		printf("Sides do not form a triangle\n");
    return 0;
}

void inputsides(Triangle* pt)
{
    
    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(pt);
}


double calcarea(Triangle* pt)
{    
    
    double insidearea;
    double sides = (pt->x+pt->y+pt->z)*0.5;

    if ((insidearea = (sides*((sides-pt->x)*(sides-pt->y)*(sides-pt->z)))) > 0.0)
        return sqrt(insidearea);
        
    else
		return 0.0;
}

static void waw()
{
    printf("You have entered an incorrect number!\n");
    printf("Run again.\n");
    printf(">>");

}

void scannum(Triangle* pt)
{
    double aside, bside, cside;

	if (!pt)
		return;
	for(;;)
	{
          while (scanf("%lf %lf %lf", &aside, &bside, &cside) != 3) /* not a number */
		{
			if (feof(stdin))
			{
				pt->x = pt->y = pt->z = 0.0;
				return;
			}
			fflush(stdin);
			waw();
		}
		if (aside <= 0.0 || bside <= 0.0 || cside <= 0.0)
			waw();
		else
		{
			pt->x = aside;
			pt->y = bside;
			pt->z = cside;
		break;
		}
	}
}
 
ArkM,

Thanks for your post. In truth I am not to that level of coding, I don't even understand the majority of the code. I was just trying to fix mine. So there is not hope for mine? It seems there must be something I can do to fix it. Well thanks anyway.

et
 
There is hope for yours, but you need to understand the logic of what you've written. It's a bit twisted up and messy. Even if you don't understand all the details of ArkM's code, read through and try to understand the flow of the function calls he's making. Follow it starting from [tt]main[/tt], looking at the looping structures, the [tt]if[/tt] structures, and the order that the functions get called. Don't worry about the pointers and structures or how the calculations are done yet, just see how he structured the flow of the program.

By the way ArkM, well written example! I give you an A+ on Erik's assignment.

Hope this helps.
 
SamBones,
10000 Thanks... The code is a compromise mixture of original source and ad hoc improvisation.

erictorrs, don't worry about structures and pointers. Redesign function's interfaces in your usual style with three doubles as args, for example. It's not a question of principle.
The only (slightly;) non-trivial enchancement (side by side with more consistent functional decomposition) is
Code:
while (scanf(...) != 3)
/* and */
if (feof(stdin))
You verify all three input values (<0), but if a user print not-a-number, scanf breaks and returns a number < 3. No 3 values in that case!

If a user press Ctrl-Z (on Windows), scanf breaks (because of end of file condition on stdin) and your (old) program verifies a garbage in not defined variables. Read more about scanf return codes.

Good luck (to you and I;)!
 
ArkM and SamBones,

I did eventually get through ArkM's code. Although I do not yet fully understand the code, I can see the logic of it all. It seems that it is not exactly what code is used, but how it is called.

I have much to learn, and hope to get there. I can't believe you even have to worry about OS, wow!

Well I got my code to work, although not in the way that I wanted, but it is done. I will actually keep working on it now that I learned about using the return() function. Thanks a lot guys. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top