Can some one tell me what is wrong with this program ?

Discussion in 'C Programming' started by broli, Mar 12, 2008.

  1. broli

    broli Guest

    #include<stdio.h>
    #include<stdlib.h>


    struct point

    {

    double x, y, z;

    };

    typedef struct point point;



    struct triangle

    {

    point x,y, z;

    };

    typedef struct triangle triangle;



    int main(void)

    {

    triangle *T;

    T = malloc(sizeof(triangle));
    printf("Enter the vertices of the triangle\n");
    printf("First vertex:\n");
    scanf("%lf %lf %lf", &(T->x.x), &(T->x.y), &(T->x.z));
    printf("Second vertex:\n");
    scanf("%lf %lf %lf", &(T->y.x), &(T->y.y), &(T->y.z));
    printf("Third vertex:\n");
    scanf("%lf %lf %lf", &(T->z.x), &(T->z.y), &(T->z.z));
    printf("Vertex 1: %lf\t%lf\t%lf\n", T->x.x, T->x.y, T->x.z);
    printf("Vertex 2: %lf\t%lf\t%lf\n", T->y.x, T->y.y, T->y.z);
    printf("Vertex 3: %lf\t%lf\t%lf\n", T->z.x, T->z.y, T->z.z);

    return 0;

    }

    I'm getting some garbage values in the output.

    
     
    broli, Mar 12, 2008
    #1
    1. Advertising

  2. broli said:

    [Subject: Can some one tell me what is wrong with this program ?]

    <snip> So far, so good.

    > int main(void)
    >
    > {
    >
    > triangle *T;
    >
    > T = malloc(sizeof(triangle));


    Check that it succeeded. If not, T will be a null pointer.

    if(T != NULL)
    {

    (and, if it /is/ a null pointer, your 'else' should, at the very least,
    report the lack of memory.)

    > printf("Enter the vertices of the triangle\n");
    > printf("First vertex:\n");
    > scanf("%lf %lf %lf", &(T->x.x), &(T->x.y), &(T->x.z));


    Check that scanf returns 3 (because you are asking for three fields to be
    converted). If not, there was an error in the input data.

    > printf("Second vertex:\n");
    > scanf("%lf %lf %lf", &(T->y.x), &(T->y.y), &(T->y.z));
    > printf("Third vertex:\n");
    > scanf("%lf %lf %lf", &(T->z.x), &(T->z.y), &(T->z.z));


    Same applies to these.

    > printf("Vertex 1: %lf\t%lf\t%lf\n", T->x.x, T->x.y, T->x.z);


    Unlike scanf, printf does not require an 'l' in the format specifier. %f is
    sufficient for doubles. In fact, unless you are fortunate enough to have a
    C99 compiler, %lf is actually incorrect. Use %f instead, since it is
    correct in both C90 and C99.

    > I'm getting some garbage values in the output.


    Unless you're running out of memory (deeply unlikely in this case) or
    providing bad input, the only reason you should get broken output is
    because of the %lf thing. Even then, some implementations (such as the one
    I used for testing your code) accept %lf quite happily and do what you
    expect - i.e. print a double. Nevertheless, change each %lf in each printf
    to %f (but leave the scanf ones alone), and re-test.

    Let us know how you get on.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Mar 12, 2008
    #2
    1. Advertising

  3. broli

    broli Guest

    On Mar 12, 8:21 pm, Richard Heathfield <> wrote:
    > broli said:
    >
    > [Subject: Can some one tell me what is wrong with this program ?]
    >
    > <snip> So far, so good.
    >
    > > int main(void)

    >
    > > {

    >
    > > triangle *T;

    >
    > > T = malloc(sizeof(triangle));

    >
    > Check that it succeeded. If not, T will be a null pointer.
    >
    > if(T != NULL)
    > {
    >
    > (and, if it /is/ a null pointer, your 'else' should, at the very least,
    > report the lack of memory.)
    >
    > > printf("Enter the vertices of the triangle\n");
    > > printf("First vertex:\n");
    > > scanf("%lf %lf %lf", &(T->x.x), &(T->x.y), &(T->x.z));

    >
    > Check that scanf returns 3 (because you are asking for three fields to be
    > converted). If not, there was an error in the input data.
    >
    > > printf("Second vertex:\n");
    > > scanf("%lf %lf %lf", &(T->y.x), &(T->y.y), &(T->y.z));
    > > printf("Third vertex:\n");
    > > scanf("%lf %lf %lf", &(T->z.x), &(T->z.y), &(T->z.z));

    >
    > Same applies to these.
    >
    > > printf("Vertex 1: %lf\t%lf\t%lf\n", T->x.x, T->x.y, T->x.z);

    >
    > Unlike scanf, printf does not require an 'l' in the format specifier. %f is
    > sufficient for doubles. In fact, unless you are fortunate enough to have a
    > C99 compiler, %lf is actually incorrect. Use %f instead, since it is
    > correct in both C90 and C99.
    >
    > > I'm getting some garbage values in the output.

    >
    > Unless you're running out of memory (deeply unlikely in this case) or
    > providing bad input, the only reason you should get broken output is
    > because of the %lf thing. Even then, some implementations (such as the one
    > I used for testing your code) accept %lf quite happily and do what you
    > expect - i.e. print a double. Nevertheless, change each %lf in each printf
    > to %f (but leave the scanf ones alone), and re-test.
    >
    > Let us know how you get on.
    >
    > --
    > Richard Heathfield <http://www.cpax.org.uk>
    > Email: -http://www. +rjh@
    > Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    > "Usenet is a strange place" - dmr 29 July 1999


    Strangely, Im still not getting it. Here's the input I used -

    First Vertex:
    23 34 44
    Second Vertex:
    33 55 66
    Third Vertex:
    -77 88 99
     
    broli, Mar 12, 2008
    #3
  4. broli

    broli Guest

    /* UPDATED */

    #include<stdio.h>
    #include<stdlib.h>


    struct point

    {

    double x, y, z;

    };

    typedef struct point point;



    struct triangle

    {

    point x,y, z;

    };

    typedef struct triangle triangle;



    int main(void)

    {

    triangle *T;
    T = malloc(sizeof(triangle));
    if(T==NULL)
    printf("Low memory\n");
    else
    {
    printf("Enter the vertices of the triangle\n");
    printf("First vertext:\n");
    if(scanf("%lf %lf %lf", &(T->x.x), &(T->x.y), &(T->x.z))!=3)
    printf("error\n");
    printf("Second vertex:\n");
    if(scanf("%lf %lf %lf", &(T->y.x), &(T->y.y), &(T->y.z))!=3)
    printf("error\n");
    printf("Third vertex:\n");
    if(scanf("%lf %lf %lf", &(T->z.x), &(T->z.y), &(T->z.z))!=3);
    printf("error\n");
    printf("Vertex 1: %f\t%f\t%f\n", T->x.x, T->x.y, T->x.z);
    printf("Vertex 2: %f\t%f\t%f\n", T->y.x, T->y.y, T->y.z);
    printf("Vertex 3: %f\t%f\t%f\n", T->z.x, T->z.y, T->z.z);

    }

    return 0;

    }
     
    broli, Mar 12, 2008
    #4
  5. On 12 Mar, 16:00, broli <> wrote:
    > On Mar 12, 8:21 pm, Richard Heathfield <> wrote:
    > > broli said:


    > > [Subject: Can some one tell me what is wrong with this program ?]

    >
    > > <snip> So far, so good.

    >
    > > > int main(void)

    >
    > > > {

    >
    > > >   triangle *T;

    >
    > > >   T = malloc(sizeof(triangle));

    >
    > > Check that it succeeded. If not, T will be a null pointer.

    >
    > >     if(T != NULL)
    > >     {

    >
    > > (and, if it /is/ a null pointer, your 'else' should, at the very least,
    > > report the lack of memory.)

    >
    > > >   printf("Enter the vertices of the triangle\n");
    > > >   printf("First vertex:\n");
    > > >   scanf("%lf %lf %lf", &(T->x.x), &(T->x.y), &(T->x.z));

    >
    > > Check that scanf returns 3 (because you are asking for three fields to be
    > > converted). If not, there was an error in the input data.

    >
    > > >   printf("Second vertex:\n");
    > > >   scanf("%lf %lf %lf", &(T->y.x), &(T->y.y), &(T->y.z));
    > > >   printf("Third vertex:\n");
    > > >   scanf("%lf %lf %lf", &(T->z.x), &(T->z.y), &(T->z.z));

    >
    > > Same applies to these.

    >
    > > >   printf("Vertex 1: %lf\t%lf\t%lf\n", T->x.x, T->x.y, T->x.z);

    >
    > > Unlike scanf, printf does not require an 'l' in the format specifier. %f is
    > > sufficient for doubles. In fact, unless you are fortunate enough to have a
    > > C99 compiler, %lf is actually incorrect. Use %f instead, since it is
    > > correct in both C90 and C99.

    >
    > > > I'm getting some garbage values in the output.

    >
    > > Unless you're running out of memory (deeply unlikely in this case) or
    > > providing bad input, the only reason you should get broken output is
    > > because of the %lf thing. Even then, some implementations (such as the one
    > > I used for testing your code) accept %lf quite happily and do what you
    > > expect - i.e. print a double. Nevertheless, change each %lf in each printf
    > > to %f (but leave the scanf ones alone), and re-test.

    >
    > > Let us know how you get on.

    >
    > Strangely, Im still not getting it. Here's the input I used -
    >
    > First Vertex:
    > 23 34 44
    > Second Vertex:
    > 33 55 66
    > Third Vertex:
    > -77 88 99- Hide quoted text -


    and the output was...

    --
    Nick Keighley
     
    Nick Keighley, Mar 12, 2008
    #5
  6. broli

    Richard Bos Guest

    broli <> wrote:

    > if(scanf("%lf %lf %lf", &(T->x.x), &(T->x.y), &(T->x.z))!=3)
    > printf("error\n");
    > printf("Second vertex:\n");
    > if(scanf("%lf %lf %lf", &(T->y.x), &(T->y.y), &(T->y.z))!=3)
    > printf("error\n");
    > printf("Third vertex:\n");
    > if(scanf("%lf %lf %lf", &(T->z.x), &(T->z.y), &(T->z.z))!=3);
    > printf("error\n");
    > printf("Vertex 1: %f\t%f\t%f\n", T->x.x, T->x.y, T->x.z);
    > printf("Vertex 2: %f\t%f\t%f\n", T->y.x, T->y.y, T->y.z);
    > printf("Vertex 3: %f\t%f\t%f\n", T->z.x, T->z.y, T->z.z);


    I can find nothing technically wrong with that program. I'd never use
    scanf() in this way, directly, for groups of numbers, without any real
    error recovery; but for a test program, it will suffice. When I compile
    and run it, it does as I expect: repeat the values I entered.

    What _exactly_ do you enter when you run this program (including whether
    or not you pressed return), what _exactly_ is the resulting output, and
    what output did you expect?

    Richard
     
    Richard Bos, Mar 12, 2008
    #6
  7. broli

    broli Guest

    On Mar 12, 9:16 pm, (Richard Bos) wrote:
    > broli <> wrote:
    > > if(scanf("%lf %lf %lf", &(T->x.x), &(T->x.y), &(T->x.z))!=3)
    > > printf("error\n");
    > > printf("Second vertex:\n");
    > > if(scanf("%lf %lf %lf", &(T->y.x), &(T->y.y), &(T->y.z))!=3)
    > > printf("error\n");
    > > printf("Third vertex:\n");
    > > if(scanf("%lf %lf %lf", &(T->z.x), &(T->z.y), &(T->z.z))!=3);
    > > printf("error\n");
    > > printf("Vertex 1: %f\t%f\t%f\n", T->x.x, T->x.y, T->x.z);
    > > printf("Vertex 2: %f\t%f\t%f\n", T->y.x, T->y.y, T->y.z);
    > > printf("Vertex 3: %f\t%f\t%f\n", T->z.x, T->z.y, T->z.z);

    >
    > I can find nothing technically wrong with that program. I'd never use
    > scanf() in this way, directly, for groups of numbers, without any real
    > error recovery; but for a test program, it will suffice. When I compile
    > and run it, it does as I expect: repeat the values I entered.
    >
    > What _exactly_ do you enter when you run this program (including whether
    > or not you pressed return), what _exactly_ is the resulting output, and
    > what output did you expect?
    >
    > Richard


    You can see what numbers I entered a few posts above. I was expecting
    the same output and I don't see what the problem is. But I personally
    think there is something very wrong witht he compiler I'm using( TC
    2.01) because when I use the OS shell option in the FILE menu bar, and
    try to execute tc from there, it shows "Not enough memory". I need to
    find a good compiler for win xp.
     
    broli, Mar 12, 2008
    #7
  8. broli

    Richard Bos Guest

    broli <> wrote:

    > On Mar 12, 9:16 pm, (Richard Bos) wrote:
    > > I can find nothing technically wrong with that program. I'd never use
    > > scanf() in this way, directly, for groups of numbers, without any real
    > > error recovery; but for a test program, it will suffice. When I compile
    > > and run it, it does as I expect: repeat the values I entered.
    > >
    > > What _exactly_ do you enter when you run this program (including whether
    > > or not you pressed return), what _exactly_ is the resulting output, and
    > > what output did you expect?

    >
    > You can see what numbers I entered a few posts above.


    Did you enter them with the prompts in between, as in that post, or
    didn't you? Did you use tabs or spaces? Where, precisely, did you press
    return? Be _exact_, damn it! Are you a programmer or a middle manager?

    > I was expecting the same output


    And what did you _get_? No output? Wild numbers? Completely random
    garbage including text? A crash? A cheeseburger? Fries with that?

    > and I don't see what the problem is.


    I do. You're not thinking, you're flailing. Start over at the beginning,
    be precise this time, and don't assume that we can read your mind or
    your screen.

    Richard
     
    Richard Bos, Mar 12, 2008
    #8
  9. broli <> writes:
    > #include<stdio.h>
    > #include<stdlib.h>
    >
    >
    > struct point
    >
    > {
    >
    > double x, y, z;
    >
    > };
    >
    > typedef struct point point;
    >
    >
    >
    > struct triangle
    >
    > {
    >
    > point x,y, z;
    >
    > };
    >
    > typedef struct triangle triangle;
    >
    >
    >
    > int main(void)

    [snip]

    A reasonable amount of vertical whitespace (blank lines) is a good
    thing. It makes it easier to see the structure of the program at a
    glance.

    The excessive blank lines you're using make my eyes bleed. (Yes, I'm
    exaggerating.)

    For the above section of your program, I suggest that this is a better
    way to format it:

    #include <stdio.h>
    #include <stdlib.h>

    struct point
    {
    double x, y, z;
    };
    typedef struct point point;

    struct triangle
    {
    point p0, p1, p2;
    };
    typedef struct triangle triangle;

    int main(void)
    ....

    I didn't leave blank lines between the struct and typedef declarations
    because they're closely associated with each other. I also snuck in a
    renaming of the members of struct triangle, for greater clarity.

    Personally, I'd write it even more tersely, but I won't go into that.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Mar 12, 2008
    #9
  10. broli wrote:
    [...]
    > Strangely, Im still not getting it. Here's the input I used -
    >
    > First Vertex:
    > 23 34 44
    > Second Vertex:
    > 33 55 66
    > Third Vertex:
    > -77 88 99


    What output do you get?

    Here's my session:

    ==========
    Enter the vertices of the triangle
    First vertex:
    23 34 44
    Second vertex:
    33 55 66
    Third vertex:
    -77 88 99
    Vertex 1: 23.000000 34.000000 44.000000
    Vertex 2: 33.000000 55.000000 66.000000
    Vertex 3: -77.000000 88.000000 99.000000
    ==========

    Looks right to me.

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Mar 12, 2008
    #10
  11. broli

    broli Guest

    On Mar 13, 12:28 am, Kenneth Brody <> wrote:
    > broli wrote:
    >
    > [...]
    >
    > > Strangely, Im still not getting it. Here's the input I used -

    >
    > > First Vertex:
    > > 23 34 44
    > > Second Vertex:
    > > 33 55 66
    > > Third Vertex:
    > > -77 88 99

    >
    > What output do you get?
    >
    > Here's my session:
    >
    > ==========
    > Enter the vertices of the triangle
    > First vertex:
    > 23 34 44
    > Second vertex:
    > 33 55 66
    > Third vertex:
    > -77 88 99
    > Vertex 1: 23.000000 34.000000 44.000000
    > Vertex 2: 33.000000 55.000000 66.000000
    > Vertex 3: -77.000000 88.000000 99.000000
    > ==========
    >
    > Looks right to me.
    >
    > --
    > +-------------------------+--------------------+-----------------------+
    > | Kenneth J. Brody |www.hvcomputer.com| #include |
    > | kenbrody/at\spamcop.net |www.fptech.com | <std_disclaimer.h> |
    > +-------------------------+--------------------+-----------------------+
    > Don't e-mail me at: <mailto:>


    I believe there are some issues with my compiler then.

    What C compiler is recommended when using Win XP ?
     
    broli, Mar 12, 2008
    #11
  12. broli

    broli Guest

    Strange I just ran the program in Dos prompt and its working fine but
    it is going me problems when I switch between dos and TC 2.01

    Menubar>>File>>OSshell

    And then in OS shell I tried to execute the program.

    It was giving garbage values as output of the program..
     
    broli, Mar 12, 2008
    #12
  13. broli

    santosh Guest

    broli wrote:

    <snip>

    > What C compiler is recommended when using Win XP ?


    None. You need to pick one that suits your needs. Some popular compilers
    are Microsoft (both the "free" Express Edition as well as the
    commercial version), Borland C++ (again a "free" version is available,
    which is rather dated, as well as more recent commercial ones), Intel
    C++ (once again "free" trial versions and for pay ones), gcc (Cygwin,
    DJGPP and MinGW all use gcc, among others), lcc-win32, PellesC and many
    many others.
     
    santosh, Mar 12, 2008
    #13
  14. broli

    Morris Dovey Guest

    broli wrote:
    >
    > Strange I just ran the program in Dos prompt and its working fine but
    > it is going me problems when I switch between dos and TC 2.01


    Did you say: "Thank you, Mr Gates" ?

    --
    Morris Dovey
    DeSoto Solar
    DeSoto, Iowa USA
    http://www.iedu.com/DeSoto/
     
    Morris Dovey, Mar 12, 2008
    #14
  15. broli <> writes:
    > Strange I just ran the program in Dos prompt and its working fine but
    > it is going me problems when I switch between dos and TC 2.01
    >
    > Menubar>>File>>OSshell
    >
    > And then in OS shell I tried to execute the program.
    >
    > It was giving garbage values as output of the program..


    *What* garbage values?

    Copy-and-paste the input you gave the program and the output you get
    and post it here. If you've modified the program, post its source
    again. Don't make us guess.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Mar 13, 2008
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. 2Barter.net

    " PLEASE TELL SOME ONE "

    2Barter.net, Dec 13, 2006, in forum: C++
    Replies:
    0
    Views:
    265
    2Barter.net
    Dec 13, 2006
  2. 2Barter.net

    " PLEASE TELL SOME ONE "

    2Barter.net, Dec 13, 2006, in forum: C++
    Replies:
    0
    Views:
    280
    2Barter.net
    Dec 13, 2006
  3. yogesh
    Replies:
    1
    Views:
    398
    Victor Bazarov
    Mar 14, 2007
  4. pereges
    Replies:
    9
    Views:
    263
    pereges
    Jun 26, 2008
  5. Asfand Yar Qazi
    Replies:
    0
    Views:
    117
    Asfand Yar Qazi
    Nov 26, 2003
Loading...

Share This Page