math.h problem..

H

herrcho

i am learning C with 'a book on c' by kelley/pohl
and got stuck in chapter 3 .. here is the code.

#include <math.h>
#include <stdio.h>

main()
{
double x;
while(1)
{
printf("Input x: ");
scanf("%lf",&x);
if(x>=0.0)
printf("\n%15s%22.15e\n%s22.15e\n%15s%22.15e\n\n",
"x = ",x,
"sqrt(x) = ",sqrt(x),
"pow(x,x)=",pow(x,x));
else
printf("\nSorry, your number must be nonnegative.\n\n");
}
}

when i run this and input 1. the value of x shows alright
but it shows 'sqrt(x) = 22.15e'
(null)1.788174022695118e-307'

and when i input 2 or 3 , error window pops up saying 'there is problem
with this executable file'

bythe way i run xp and gcc compiler. could anyone help me please ?
 
P

pete

herrcho said:
printf("\n%15s%22.15e\n%s22.15e\n%15s%22.15e\n\n",
"x = ",x,
"sqrt(x) = ",sqrt(x),
"pow(x,x)=",pow(x,x));

printf("\n%15s%22.15e\n", "x = ", x);
printf( "%15s%22.15e\n\n", "sqrt(x) = ", sqrt(x));
printf( "%15s%22.15e\n\n","pow(x,x)=", pow(x,x));
 
M

Martin Ambuhl

herrcho said:
i am learning C with 'a book on c' by kelley/pohl
and got stuck in chapter 3 .. here is the code.

#include <math.h>
#include <stdio.h>

main()
{
double x;
while(1)
{
printf("Input x: ");
scanf("%lf",&x);
if(x>=0.0)
printf("\n%15s%22.15e\n%s22.15e\n%15s%22.15e\n\n",
"x = ",x,
"sqrt(x) = ",sqrt(x),
"pow(x,x)=",pow(x,x));
else
printf("\nSorry, your number must be nonnegative.\n\n");
}
}

when i run this and input 1. the value of x shows alright
but it shows 'sqrt(x) = 22.15e'
(null)1.788174022695118e-307'

and when i input 2 or 3 , error window pops up saying 'there is problem
with this executable file'

bythe way i run xp and gcc compiler. could anyone help me please ?

#include <math.h>
#include <stdio.h>
#include <float.h> /* mha: added so I can be rid of magic
numbers in the printf statement */


int /* mha: made return type explicit so C99
won't complain */ main()
{
double x;
while (1) {
printf("Input x: ");
fflush(stdout); /* mha: added to synchronize input and
output */
scanf("%lf", &x); /* mha: This has got to go! using scanf
for keyboard input, especially for
numeric data, is asking for trouble */
if (x >= 0.0)
/* mha: cleaned up printf, rather than just fixing the second
'22.15e', which had no '%'. */
printf("%-15s%.*g\n%-15s%.*g\n%-15s%.*g\n\n",
"x = ", DBL_DIG, x,
"sqrt(x) = ", DBL_DIG, sqrt(x),
"pow(x,x)=", DBL_DIG, pow(x, x));
else {
printf("Sorry, your number must be nonnegative.\n\n");
break; /* mha: to provide a way out of this
endless loop */
}
}
return 0; /* mha: made return value explicit so

C90 won't complain */
}
 
B

Bill Cunningham

#include said:
#include <stdio.h>
#include <float.h> /* mha: added so I can be rid of magic
numbers in the printf statement */


int /* mha: made return type explicit so C99
won't complain */ main()
{
double x;
while (1) {
printf("Input x: ");
fflush(stdout); /* mha: added to synchronize input and
output */
scanf("%lf", &x); /* mha: This has got to go! using scanf
for keyboard input, especially for
numeric data, is asking for trouble */

Then use what? fgets redirected to stdin?
 
N

Nick Austin

i am learning C with 'a book on c' by kelley/pohl
and got stuck in chapter 3 .. here is the code.

#include <math.h>
#include <stdio.h>

main()
{
double x;
while(1)
{
printf("Input x: ");
scanf("%lf",&x);
if(x>=0.0)
printf("\n%15s%22.15e\n%s22.15e\n%15s%22.15e\n\n",

Five % characters.
"x = ",x,
"sqrt(x) = ",sqrt(x),
"pow(x,x)=",pow(x,x));

Six arguments.
else
printf("\nSorry, your number must be nonnegative.\n\n");
}
}

when i run this and input 1. the value of x shows alright
but it shows 'sqrt(x) = 22.15e'

There is a clue here. It printed "22.15e".

Nick.
 
E

Emmanuel Delahaye

herrcho said:
#include <math.h>
#include <stdio.h>

main()

int main()
or
int main(void)
{
double x;
while(1)
{
printf("Input x: ");
scanf("%lf",&x);
if(x>=0.0)
printf("\n%15s%22.15e\n%s22.15e\n%15s%22.15e\n\n",

Hard to read and to check. My colmpilers says there is a error at parameter 5
(wrong type).

Let's rewrite it line by line:

printf("\n%15s%22.15e\n"
"%s22.15e\n"
"%15s%22.15e\n\n",

Now, it's clear that a '%' is missing at line 2:

"%s%22.15e\n"

and probably a width qualifier:

"%15s%22.15e\n"
"x = ",x,
"sqrt(x) = ",sqrt(x),
"pow(x,x)=",pow(x,x));
else
printf("\nSorry, your number must be nonnegative.\n\n");
}

return 0;

Try that:

#include <math.h>
#include <stdio.h>

int main (void)
{
while (1)
{
double x;
printf ("Input x: ");
fflush (stdout);
{
int n = scanf ("%lf", &x);

if (n == EOF)
{
break;
}
}

if (x >= 0.0)
{
#define FMT "%15s%22.15e\n"
printf ("\n"
FMT
FMT
FMT
"\n"
,"x = ", x
,"sqrt(x) = ", sqrt (x)
,"pow(x,x) = ", pow (x, x));
#undef FMT
}
else
{
printf ("\nSorry, your number must be nonnegative.\n\n");
}
}

return 0;
}
 
E

Emmanuel Delahaye

Bill Cunningham said:
Then use what? fgets redirected to stdin?

Yes, or a home made 'getline' function based on [f]getc(), followed by
strtod().
 
M

Martin Ambuhl

Bill said:
Then use what? fgets redirected to stdin?

Since when was fgers(buf, sizeof buf, stdin); a problem? Are you asking
some deep question that I just am too dense to understand? Of course one
uses fgets on stdin.
 

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,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top