473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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


Mar 12 '08 #1
12 2084
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(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
Mar 12 '08 #2
On Mar 12, 8:21 pm, Richard Heathfield <r...@see.sig.invalidwrote:
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(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
Mar 12 '08 #3
/* 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;

}
Mar 12 '08 #4
On 12 Mar, 16:00, broli <Brol...@gmail.comwrote:
On Mar 12, 8:21 pm, Richard Heathfield <r...@see.sig.invalidwrote:
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(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. %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
Mar 12 '08 #5
broli <Br*****@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
Mar 12 '08 #6
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.
Mar 12 '08 #7
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:Th*************@gmail.com>

Mar 12 '08 #8
On Mar 13, 12:28 am, Kenneth Brody <kenbr...@spamcop.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:ThisIsASpamT...@gmail.com>
I believe there are some issues with my compiler then.

What C compiler is recommended when using Win XP ?
Mar 12 '08 #9
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..

Mar 12 '08 #10
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.

Mar 12 '08 #11
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/
Mar 13 '08 #12
broli <Br*****@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_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 13 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
6
by: xeys_00 | last post by:
I'm trying to use the character "q" to exit this program. But if anything but q is input , I want the loop to continue. Am I using the wrong loop? #include <string> #include <iostream> ...
29
by: Roy Gourgi | last post by:
Hi, I am new to C#. I have the same time scheduling program written in C++ and it is 5 times faster than my version in C#. Why is it so slow as I thought that C# was only a little slower than...
0
by: Fiona McBride | last post by:
Hi all, I have a really odd problem with some Visual Basic .NET 2003 code; I have a program that creates a number of windows which contain RichTextBox, Timers (disabled) and menus. The code...
7
by: Just Me | last post by:
Sometimes I hear the MODEM dialing and I don't know why. Is there some way I can use VB to tell which program is causing it? I'm on a LAN so it may be a remote program. Thanks
2
by: venkatesh | last post by:
hai , i am using turboc++ compiler to run my c program. i written an program with structure sa foolws struct s { float x; }y; when i am accessing the variable x it shows the following error...
13
by: Sree | last post by:
If the program (myprog) is run from the command line as myprog 1 2 3 , What would be the output? main(int argc, char *argv) { int i; for(i=0;i<argc;i++) printf("%s",argv); }
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
15
by: E-Dot | last post by:
I am trying to write a program which asks the user to enter a number in the interval , the program then gives the natural logarithm of that number, using the series for log(x+1)... Here is what...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.