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

B

broli

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


 
R

Richard Heathfield

broli said:

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

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

broli

broli said:

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

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
 
B

broli

/* 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;

}
 
N

Nick Keighley

broli said:
[Subject: Can some one tell me what is wrong with this program ?]
<snip> So far, so good.
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.)
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.
Same applies to these.
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.
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...
 
R

Richard Bos

broli said:
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
 
B

broli

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

Richard Bos

broli said:
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
 
K

Keith Thompson

broli said:
#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.
 
K

Kenneth Brody

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:[email protected]>
 
B

broli

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:[email protected]>

I believe there are some issues with my compiler then.

What C compiler is recommended when using Win XP ?
 
B

broli

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

santosh

broli wrote:

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

Morris Dovey

broli said:
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" ?
 
K

Keith Thompson

broli said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,979
Messages
2,570,185
Members
46,727
Latest member
FelicaTole

Latest Threads

Top