# Do while problem

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;
char Answer;
do
{
printf("Enter your Triangle's side
");
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
");
scanf("%c",&Answer);
}while(Answer=='Y');

}

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;
}

## Comments

• 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]
• Do you think getchar also works or not ?
after scanf just type
getchar();
• Do you think getchar also works or not ?
after scanf just type
getchar();
• Do you think getchar also works or not ?
after scanf just type
getchar();
• 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.
• 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.

• yes it works.
[url=http://www.communitylifeguards.com]lifeguard jobs Austin[/url]
• 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]
}while(Answer=='Y'); [/code]

That will have the [italic]scanf[/italic] call ignore any leading white space (including those pesky newlines) in the input buffer. You may also want to test Answer against the lowercase character 'y' as well.
• 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]
}while(Answer=='Y'); [/code]

That will have the [italic]scanf[/italic] call ignore any leading white space (including those pesky newlines) in the input buffer. You may also want to test Answer against the lowercase character 'y' as well.
Sign In or Register to comment.

#### Howdy, Stranger!

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