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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • broli

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

    #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(t riangle));
    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.

    
  • Richard Heathfield

    #2
    Re: Can some one tell me what is wrong with this program ?

    broli said:

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

    <snipSo far, so good.
    int main(void)
    >
    {
    >
    triangle *T;
    >
    T = malloc(sizeof(t riangle));
    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

    Comment

    • broli

      #3
      Re: Can some one tell me what is wrong with this program ?

      On Mar 12, 8:21 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
      broli said:
      >
      [Subject: Can some one tell me what is wrong with this program ?]
      >
      <snipSo far, so good.
      >
      int main(void)
      >
      {
      >
      triangle *T;
      >
      T = malloc(sizeof(t riangle));
      >
      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

      Comment

      • broli

        #4
        Re: Can some one tell me what is wrong with this program ?

        /* 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(t riangle));
        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;

        }


        Comment

        • Nick Keighley

          #5
          Re: Can some one tell me what is wrong with this program ?

          On 12 Mar, 16:00, broli <Brol...@gmail. comwrote:
          On Mar 12, 8:21 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
          broli said:
          [Subject: Can some one tell me what is wrong with this program ?]
          >
          <snipSo far, so good.
          >
          int main(void)
          >
          {
          >
            triangle *T;
          >
            T = malloc(sizeof(t riangle));
          >
          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. %fis
          sufficient for doubles. In fact, unless you are fortunate enough to havea
          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

          Comment

          • Richard Bos

            #6
            Re: Can some one tell me what is wrong with this program ?

            broli <Broli00@gmail. comwrote:
            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

            Comment

            • broli

              #7
              Re: Can some one tell me what is wrong with this program ?

              On Mar 12, 9:16 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
              broli <Brol...@gmail. comwrote:
              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.

              Comment

              • Kenneth Brody

                #8
                Re: Can some one tell me what is wrong with this program ?

                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:ThisIsA SpamTrap@gmail. com>

                Comment

                • broli

                  #9
                  Re: Can some one tell me what is wrong with this program ?

                  On Mar 13, 12:28 am, Kenneth Brody <kenbr...@spamc op.netwrote:
                  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:ThisIsA SpamT...@gmail. com>
                  I believe there are some issues with my compiler then.

                  What C compiler is recommended when using Win XP ?

                  Comment

                  • broli

                    #10
                    Re: Can some one tell me what is wrong with this program ?

                    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..

                    Comment

                    • santosh

                      #11
                      Re: Can some one tell me what is wrong with this program ?

                      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.

                      Comment

                      • Morris Dovey

                        #12
                        Re: Can some one tell me what is wrong with this program ?

                        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

                        Comment

                        • Keith Thompson

                          #13
                          Re: Can some one tell me what is wrong with this program ?

                          broli <Broli00@gmail. comwrites:
                          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_Keit h) <kst-u@mib.org>
                          Nokia
                          "We must do something. This is something. Therefore, we must do this."
                          -- Antony Jay and Jonathan Lynn, "Yes Minister"

                          Comment

                          Working...