Thread: The last few errors in my first program!

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    29

    Exclamation The last few errors in my first program!

    Hey first of all, first post! I think this community is a great place for learning, i cant wait to start helping you guys when i get up to par

    Anyways... This program i made defines a triangle in very basic ways... Basically its giving me 2 problems, one is that if i type in a letter... the program goes haywire.
    The other problem is that my area pops up twice on 2 on equilateral and scalene... I believe this is because of my else statement for isosceles
    Thanks again

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
    /** Declaring Integers **/
    
        float sidea, sideb, sidec, s, area;
        char y, n, loop, trash, loop2;
        y=1;
        n=0;
    /** While clause**/
        do
        {
    /**Enter Sides of Triangle**/
    
        printf( "Enter Side A:" );
        scanf( "%f", &sidea);
        printf( "Enter Side B:" );
        scanf( "%f", &sideb);
        printf( "Enter Side C:" );
        scanf( "%f, %c", &sidec);
        trash = getchar();
    /** Error Checking**/
       
    /**Imposible Triangle**/
        if ((sidea + sideb <= sidec) || (sideb + sidec <= sideb) || (sidea +sideb <= sidec))
        {
            printf( "Triangle is impossible\n" );
        }
    
    /**Equilateral Triangle**/
        else if ((sidea == sideb) && (sidea == sidec) && (sideb == sidec))
        {
            printf( "Triangle is Equilateral\n" );
            s = (sidea + sideb + sidec)/2;
            area=sqrt(s*(s-sidea)*(s-sideb)*(s-sidec));
            printf( "%6.2f", area);
        }
    
    /**Scalene Triangle**/
        else if ((sidea != sideb) && (sidea != sidec) && (sideb != sidec))
        {
            printf( "Triangle is Scalene\n" );
            s = (sidea + sideb + sidec)/2;
            area=sqrt(s*(s-sidea)*(s-sideb)*(s-sidec));
            printf( "%6.2f", area);
        }
    
    /**Isosceles**/
        else printf( "Triangle is Isosceles\n" );
        {
            s = (sidea + sideb + sidec)/2;
            area=sqrt(s*(s-sidea)*(s-sideb)*(s-sidec));
            printf( "%6.2f", area);
        }
    /**Loop**/
       
        do
        {
        loop2=0;
        printf( "Would you like to create another triangle? (Y/N)\n" );
        scanf( "%c, %c", &loop);
        getchar();
        if ((loop == 'y') || (loop == 'Y'))
        loop=y;
        else if ((loop == 'n') || (loop == 'N'))
        loop=n;
        else ((printf ("Please enter either (Y/N)\n"))&& (loop2=1));
        }
        while (loop2==1);
        }
    
        /*close of do loop*/
       
    while(loop == y);
       
    return 0;
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    Here's a logic error:
    Code:
    if ((sidea + sideb <= sidec) || (sideb + sidec <= sideb) || (sidea +sideb <= sidec))
    Here's a redundant test:
    Code:
    /**Equilateral Triangle**/
        else if ((sidea == sideb) && (sidea == sidec) && (sideb == sidec))
    One too many %c.
    Code:
        printf( "Would you like to create another triangle? (Y/N)\n" );
        scanf( "%c, %c", &loop);
    Last edited by Dino; 09-14-2009 at 09:29 PM.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    "haywire" may seem descriptive, but in the case of a programming error, it is kind of the opposite. In any case, it is not really surprising that a letter is not well absorbed as a floating point number, I think you are aware of that. To circumvent the problem, you must perform some "input validation". But don't worry about that quite yet.

    Letters, and anything else that counts as a char, do have an integer value, known as the "ascii value".

    ASCII Table / Extended ASCII Codes

    "Anything else" here includes the newline, aka '\n', with a value of 10 in the ascii table. Most likely, your problem is because at certain points you use scanf("%c"). What the user enters in such a case is not one character, but two, the second one being '\n' from the ENTER key. That character is left in the input buffer, and is absorbed by the next scanf() call.

    Here are some ways to deal with that:

    alternative for fflush(stdin)

    Do not use fflush(stdin), obviously.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    We just had this same program, and sorted it out last week. Search for "triangle" in the body of the post.

    You have the same errors, almost.

    Then, as now, you can't scanf() a float and a char, and somehow put it into just one variable.

    I'd avoid turning this program in as yours. Make some other changes to it (although you can't change the equation basics).

    If side a == side b, and side a == side c, you don't have to check if side b == side c for an equilateral triangle. That's a given.
    Last edited by Adak; 09-14-2009 at 09:36 PM.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    29

    Exclamation thank you :)

    thanks that actually fixed about everything. I want to read more on the ASCII values i think its intriguing!

    anyways, I'm not sure if you guys are willing to find any other errors in my program. I know the area comes out twice when i work on an equilateral or scalene triangle... Is that because of my else statement with Iscosceles?

    Also it spits out an area when I am working with impossible triangles (when i thought i never told it to???)
    Thanks again ahead of time
    -James

    PS: Adak i cant find the post your talking about Could i get a link?

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
    /** Declaring Integers **/
    
        float sidea, sideb, sidec, s, area;
        char y, n, loop, trash, loop2;
        y=1;
        n=0;
    /** While clause**/
        do
        {
    /**Enter Sides of Triangle**/
    
        printf( "Enter Side A:" );
        scanf( "%f", &sidea);
        printf( "Enter Side B:" );
        scanf( "%f", &sideb);
        printf( "Enter Side C:" );
        scanf( "%f, %c", &sidec);
        trash = getchar();
    /** Error Checking**/
       
    /**Imposible Triangle**/
        if ((sidea + sideb <= sidec) || (sideb + sidec <= sideb) || (sidea + sidec <= sideb))
        {
            printf( "Triangle is impossible\n" );
        }
    
    /**Equilateral Triangle**/
        else if ((sidea == sideb) && (sidea == sidec))
        {
            printf( "Triangle is Equilateral\n" );
            s = (sidea + sideb + sidec)/2;
            area=sqrt(s*(s-sidea)*(s-sideb)*(s-sidec));
            printf( "%6.2f", area);
        }
    
    /**Scalene Triangle**/
        else if ((sidea != sideb) && (sidea != sidec) && (sideb != sidec))
        {
            printf( "Triangle is Scalene\n" );
            s = (sidea + sideb + sidec)/2;
            area=sqrt(s*(s-sidea)*(s-sideb)*(s-sidec));
            printf( "%6.2f", area);
        }
    
    /**Isosceles**/
        else printf( "Triangle is Isosceles\n" );
        {
            s = (sidea + sideb + sidec)/2;
            area=sqrt(s*(s-sidea)*(s-sideb)*(s-sidec));
            printf( "%6.2f", area);
        }
    /**Loop**/
       
        do
        {
        loop2=0;
        printf( "Would you like to create another triangle? (Y/N)\n" );
        scanf( "%c", &loop);
        getchar();
        if ((loop == 'y') || (loop == 'Y'))
        loop=y;
        else if ((loop == 'n') || (loop == 'N'))
        loop=n;
        else ((printf ("Please enter either (Y/N)\n"))&& (loop2=1));
        }
        while (loop2==1);
        }
    
        /*close of do loop*/
       
    while(loop == y);
       
    return 0;
    }
    Last edited by krazymanrebirth; 09-14-2009 at 09:50 PM.

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    fixed it, thanks anyways ^_^

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Hey, sorry, I just noticed you did do this:
    Code:
    scanf( "%f", &sidec);
    trash = getchar();
    so sorry for needlessly filling you in on something you already know.

    Your problem is this:
    Code:
    /**Isosceles**/
        else printf( "Triangle is Isosceles\n" );
        {
            s = (sidea + sideb + sidec)/2;
            area=sqrt(s*(s-sidea)*(s-sideb)*(s-sidec));
            printf( "%6.2f", area);
        }
    the printf should be inside the block -- as is, the block is not part of the if/else if /else and always executed.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    29

    Talking /**Isosceles**/ else if ((sidea == sideb) || (sidea == sidec) || (sideb == sidec)

    hmm this is what i did to fix it. Just changed it from an else to an else if.

    Code:
    /**Isosceles**/
        else if ((sidea == sideb) || (sidea == sidec) || (sideb == sidec))
        {
            printf( "Triangle is Isosceles\n" );
            s = (sidea + sideb + sidec)/2;
            area=sqrt(s*(s-sidea)*(s-sideb)*(s-sidec));
            printf( "Squareroot is:%6.2f\n", area);
        }
    Also here is my finished code, If anyone wants to learn from it go ahead thats why its there
    also..Is this an A in my lab by all your standards? My tabbing etc is readable? integers make sense? ^_^ thanks


    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
    /** Declaring Integers **/
    
        float sidea, sideb, sidec, s, area;
        char y, n, loop, trash, loop2;
        y=1;
        n=0;
    /** While clause**/
        do
        {
    /**Header**/
        printf( "Welcome to the Triangle Calculator, Please enter the sides of the triangle:\n" );
        printf( "---------------------------------------------------------------------------\n" );
    /**Enter Sides of Triangle**/
    
        printf( "Enter Side A:" );
        scanf( "%f", &sidea);
        printf( "Enter Side B:" );
        scanf( "%f", &sideb);
        printf( "Enter Side C:" );
        scanf( "%f, %c", &sidec);
        trash = getchar();
    /** Error Checking**/
        
    /**Imposible Triangle**/
        if ((sidea + sideb <= sidec) || (sideb + sidec <= sideb) || (sidea + sidec <= sideb))
        {
            printf( "Triangle is impossible\n" );
        }
    
    /**Equilateral Triangle**/
        else if ((sidea == sideb) && (sidea == sidec))
        {
            printf( "Triangle is Equilateral\n" );
            s = (sidea + sideb + sidec)/2;
            area=sqrt(s*(s-sidea)*(s-sideb)*(s-sidec));
            printf( "Squareroot is:%6.2f\n", area);
        }
    
    /**Scalene Triangle**/
        else if ((sidea != sideb) && (sidea != sidec) && (sideb != sidec))
        {
            printf( "Triangle is Scalene\n" );
            s = (sidea + sideb + sidec)/2;
            area=sqrt(s*(s-sidea)*(s-sideb)*(s-sidec));
            printf( "Squareroot is:%6.2f\n", area);
        }
    
    /**Isosceles**/
        else if ((sidea == sideb) || (sidea == sidec) || (sideb == sidec))
        {
            printf( "Triangle is Isosceles\n" );
            s = (sidea + sideb + sidec)/2;
            area=sqrt(s*(s-sidea)*(s-sideb)*(s-sidec));
            printf( "Squareroot is:%6.2f\n", area);
        }
    /**Loop**/
        
        do
        {
        loop2=0;
        printf( "Would you like to create another triangle? (Y/N)\n" );
        scanf( "%c", &loop);
        getchar();
        if ((loop == 'y') || (loop == 'Y'))
        loop=y;
        else if ((loop == 'n') || (loop == 'N'))
        loop=n;
        else ((printf ("Please enter either (Y/N)\n"))&& (loop2=1));
        }
        while (loop2==1);
        }
    
        /*close of do loop*/
        
    while(loop == y);
        printf( "Thank you come again -- Program Created By: James Goetz\n" );
    return 0;
    }

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    AH dammit! when you put a letter instead of a number (for any side variable) it throws it into an infinite loop! Can anyone tell me what im missing here

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If that is a criteria of your assignment, you will have to write a validation function that simply takes input as a string, using fgets(), then you can validate the string and get data from it in the way you are now but using sscanf().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    hmm, we havent learned fgets() yet, We just learned loops in our C book (C by discovery)

    I'm all for learning so im reading up on the fgets() now... but would there be any other way?

    thanks again MK27 your awesome

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by krazymanrebirth View Post
    I'm all for learning so im reading up on the fgets() now... but would there be any other way?
    Yes, you can check the return value of scanf(). Here is a clue:
    Code:
    if (!(scanf( "%f", &sidea))) puts("!NaN!");
    put that in and figure out how to make it happen.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    Im sorta lost on where to put it, But I tried.. and failed...

    Code:
    /**Imposible Triangle**/
        if ((sidea + sideb <= sidec) || (sideb + sidec <= sideb) || (sidea + sidec <= sideb))
        {
            if (!(scanf( "%f", &sidea)))
            printf( "Triangle is impossible\n" );
        }

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by krazymanrebirth View Post
    Im sorta lost on where to put it, But I tried.. and failed...
    yeah. maybe I should have said "replace one of the scanf lines" instead of "put this in".

    The problem is scanf is not reading anything when you enter a letter, so you end up with an undefined value in one of your floats, which you feed to sqrt().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    Registered User
    Join Date
    Sep 2009
    Posts
    29
    Im not understanding the mathematics (or coding explanation) for what the
    if (!(scanf( "%f", &sidea))) puts("!NaN!");
    does... I mean i understand your explanation at least ^_^

    So turning these into if statements... I dont understand that or the puts("!NaN!") << not a number??


    /**Enter Sides of Triangle**/

    printf( "Enter Side A:" );
    if (!(scanf( "%f", &sidea))) puts("!NaN!");
    printf( "Enter Side B:" );
    if (!(scanf( "%f", &sideb))) puts("!NaN!");
    printf( "Enter Side C:" );
    if (!(scanf( "%f", &sidec))) puts("!NaN!");
    trash = getchar();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. mega compile errors for small program
    By s_UNI_ in forum C++ Programming
    Replies: 4
    Last Post: 04-28-2005, 12:00 PM
  3. Errors with program
    By nizbit in forum C Programming
    Replies: 37
    Last Post: 12-19-2004, 09:56 PM
  4. Average Rainfall Program Errors
    By JamesAnthony23 in forum C Programming
    Replies: 1
    Last Post: 09-11-2002, 10:44 PM
  5. I'm a newbie and I can't understand the errors in my program
    By iluvmyafboys in forum C++ Programming
    Replies: 19
    Last Post: 02-20-2002, 10:40 AM