Do while problem - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Do while problem

Posts: 4Member
Could you please help why my program goes through do while loop just once, although the condition is true.
//Program is getting numbers from user and realize whether they are correct numbers for triangle side or not and if tey find out the area//

#include
#include
double TraingleArea();
int main(void)
{
int Firstside,Secondside,Thirdside;
do
{
");
scanf("%d %d %d", &Firstside, &Secondside, &Thirdside);
if(Firstside<=0 || Secondside<=0 || Thirdside<=0)
printf("Sides of traingle are positive,please enter three positive integer numbers.");
else
{
if(Firstside+Secondside<=Thirdside || Secondside+Thirdside<=Firstside ||Firstside+Thirdside<=Secondside)
printf("They are not valid numbers for sides of traingle.");
else
{
if(Firstside==Secondside && Secondside==Thirdside)

printf("The tringle is Equilateral Its area is equal to %f
",TraingleArea(Firstside,Secondside,Thirdside) );

else if(Firstside==Secondside || Firstside==Thirdside || Secondside==Thirdside)

printf("The triangle is Isosceles Its area is equal to %f
",TraingleArea(Firstside,Secondside,Thirdside) );

else
printf("The triangle is Scalene Its area is equal to %f
",TraingleArea(Firstside,Secondside,Thirdside) );
}

}
printf("Would you like to continue? Y/N
");

}

double TraingleArea(unsigned Firstside,unsigned Secondside, unsigned Thirdside)
{
double halfPerimeter=(double)(Firstside+Secondside+Thirds ide)/2;
double Area;
Area=sqrt(halfPerimeter*(halfPerimeter-Firstside)*(halfPerimeter-Secondside)*(halfPerimeter-Thirdside));
return Area;
}

• Posts: 54Member
Besides all of the minor issues in the code you posted. The simple solution to your problem is to call fflush() on stdin after you read the three numbers. Things get left on the input buffer which scanf will pickup the next time it is called. Which in this case is the newline character that was entered to confirm the entry of the numbers.

[code]
//...
scanf("%d %d %d", &Firstside, &Secondside, &Thirdside);
fflush( stdin );
//...
[/code]
• Posts: 4Member
Do you think getchar also works or not ?
after scanf just type
getchar();
• Posts: 4Member
Do you think getchar also works or not ?
after scanf just type
getchar();
• Posts: 4Member
Do you think getchar also works or not ?
after scanf just type
getchar();
• Posts: 54Member
Might, depends on what is left in the input buffer.

If it is just the newline then yes, if not then no. I would personally flush the stream since it covers you in both cases.
• Posts: 693Member ✭✭✭
fflush(stdin) results in undefined behavior; it should never be used on input streams.

scanf("%d %d %d%*c", &n1, &n2, &n3) *may* work, but reading numbers directly is dangerous... especially with this crazy function.

• Posts: 1Member
yes it works.
[url=http://www.communitylifeguards.com]lifeguard jobs Austin[/url]
• Posts: 770Member ✭✭✭
Easiest solution is to alter the [italic]scanf[/italic] call to have a space in front of the %c format specifier.

[code]printf("Would you like to continue? Y/N
");
scanf(" %c",&Answer); [color=Blue]/* Notice the space in front of the %c */[/color]