Thread: Do while problem

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    11

    Angry 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<stdio.h>
    #include<math.h>
    double TraingleArea();
    int main(void)
    {
    int Firstside,Secondside,Thirdside;
    char Answer;
    do
    {
    printf("Enter your Triangle's side \n");
    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\t Its area is equal to %f \n",TraingleArea(Firstside,Secondside,Thirdside) );

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

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

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

    }
    printf("Would you like to continue? Y/N\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;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Does your user have their caps lock turned on?
    Code:
    }while(Answer=='Y');
    All this stuff is case sensitive... Y is not the same as y

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Please read the forum guidelines and code posting rules. Please edit your post to use code tags and proper indentation, so people can actually read your code and see what it's doing.

    You said main would return an int, so please make it do so (put a return 0 at the end of it). Read this to see why it matters.
    Also, you need to flush the input buffer. Read this FAQ article.
    You should also allow a lower case 'y' to continue.
    Last edited by anduril462; 02-18-2011 at 12:45 PM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by anduril462 View Post
    You should also allow a lower case 'y' to continue.
    Evreything you said... except he should be testing for both upper and lower case.

  5. #5
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Alternatively, he could do:
    Code:
    #include <ctype.h>
    
    (tolower(Answer) == 'y')
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    11

    Angry clearing buffer

    Thanks everybody.Is there any easier way to flush the buffer?
    (my college professor said use either getcha(); or %*c . but none of these are working .)

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Easier than what? You want it to read your mind and magically flush? Did you read the FAQ on it. The solution is little more than 1 line, how much simpler do you want it?

    Code:
    void flush_input(void)
    {
        int c;
        while ((c = getchar()) != '\n' && c != EOF)
            ;
    }
    Then just call flush_input() wherever appropriate.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    scanf("%d %d %d", &Firstside, &Secondside, &Thirdside);
    
    //add this:
    getchar();
    This is not as robust as the one that Anduril has posted, above. It works fine (don't forget the 'r'), but it has the limit of pulling only one char off the input buffer. That's usually enough (one per scanf(), is normal), but not always. User's are pretty clever about making good input, turn bad!

    When the above is working properly, you will NOT notice it - execution of the program will continue, and not pause. ONLY time it will pause, is if the input buffer is already empty, and that is not the normal case after a scanf().

    I have tried to use the %*c format to remove the newline char, but it failed. getchar() works, however.
    Last edited by Adak; 02-18-2011 at 04:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM