Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Do while problem

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

  • nebgastnebgast 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]
  • chess2009chess2009 Posts: 4Member
    Do you think getchar also works or not ?
    after scanf just type
    getchar();
  • chess2009chess2009 Posts: 4Member
    Do you think getchar also works or not ?
    after scanf just type
    getchar();
  • chess2009chess2009 Posts: 4Member
    Do you think getchar also works or not ?
    after scanf just type
    getchar();
  • nebgastnebgast 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.
  • pseudocoderpseudocoder Posts: 673Member ✭✭
    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. :D




  • HK_MP5KPDWHK_MP5KPDW Posts: 767Member ✭✭✭
    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.
  • HK_MP5KPDWHK_MP5KPDW Posts: 767Member ✭✭✭
    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.