Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Can some one tell me what is wrong with this program ?

Reply
Thread Tools

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

 
 
broli
Guest
Posts: n/a
 
      03-12-2008
#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.


 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      03-12-2008
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
 
Reply With Quote
 
 
 
 
broli
Guest
Posts: n/a
 
      03-12-2008
On Mar 12, 8:21 pm, Richard Heathfield <r...@see.sig.invalid> 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
 
Reply With Quote
 
broli
Guest
Posts: n/a
 
      03-12-2008
/* 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;

}


 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      03-12-2008
On 12 Mar, 16:00, broli <Brol...@gmail.com> wrote:
> On Mar 12, 8:21 pm, Richard Heathfield <r...@see.sig.invalid> 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
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      03-12-2008
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
 
Reply With Quote
 
broli
Guest
Posts: n/a
 
      03-12-2008
On Mar 12, 9:16 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
> broli <Brol...@gmail.com> 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.
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      03-12-2008
broli <> wrote:

> On Mar 12, 9:16 pm, r...@hoekstra-uitgeverij.nl (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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-12-2008
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) <kst->
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      03-12-2008
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: <private.php?do=newpm&u=>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
can some one please tell me the cause of the error ? pereges C Programming 9 06-26-2008 04:26 PM
Some people tell that at present, most web hosting servers supportall kinds of programming language, some people tell me that many web hostingserver don't support Java, What is the truth? Erwin Moller Java 3 05-07-2008 05:09 PM
CAN any one tell it whats the code tell it yogesh C++ 1 03-14-2007 01:12 PM
Re: Regedit tell me some other program is using it Toolman Tim Computer Support 7 05-26-2006 02:26 AM
can someone please tell me some Visual Basic websites for some help? valentin Computer Support 2 06-25-2003 05:33 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57