Author Message

You must be very careful how you accept numbers from the user. Your textbook
may be full of examples of the use of scanf(), but you must ignore this. Try

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

double getNumber(char *prompt)
{
char buff[128];
double x;
int converted;
do {
printf("%s: ",prompt);
fgets(buff,128,stdin);
converted = sscanf(buff,"%lf",&x);
if(converted != 1) {
}
}
while(converted != 1);
return x;

Quote:
}

int main ()
{
double m,n;
do{
m = getNumber("Enter a number (> 0)");
n = getNumber("Enter an exponent (> 0)");
if((m > 0 ) && (n > 0)) {
printf ("%f raised to the %f power is: %f.\n",m,n, pow(m,n));
}
}
while ((m > 0) && (n> 0));
return 0;

Quote:
}

--

Paul Lutus
www.arachnoid.com

Quote:
>Let me start off by saying that I am just beginning to learn C and that I
am
>only in the first chapter of my text.  My instructor wants me to write a
>program that reads positive integers m and n and then print m^n.

<snip>

Sat, 11 Aug 2001 03:00:00 GMT
Let me start off by saying that I am just beginning to learn C and that I am
only in the first chapter of my text.  My instructor wants me to write a
program that reads positive integers m and n and then print m^n.  Here is what
I came up with so far (the program compiles fine, but it always gives 1.000000
as an answer no matter what 2 integers I enter):

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

int main (void)
{
double m,n;
do{
printf("Enter a positive integer m:\n");
scanf ("%f",&m);
printf("Enter a positive integer n:\n");
scanf ("%f",&n);
printf ("m raised to the n power is: %f\n", pow(m,n));}
while (m>=1&&n>=1);
return 0;  }

Could someone explain what I am doing wrong and try to point me in the right
direction.  Any help would be appreciated.
Thanks, Mike

Sun, 12 Aug 2001 03:00:00 GMT

Quote:

> Let me start off by saying that I am just beginning to learn C and that I am
> only in the first chapter of my text.  My instructor wants me to write a
> program that reads positive integers m and n and then print m^n.  Here is what
> I came up with so far (the program compiles fine, but it always gives 1.000000
> as an answer no matter what 2 integers I enter):

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

> int main (void)
> {
>         double m,n;
>    do{
>         printf("Enter a positive integer m:\n");
>         scanf ("%f",&m);
>         printf("Enter a positive integer n:\n");
>         scanf ("%f",&n);
>         printf ("m raised to the n power is: %f\n", pow(m,n));}
>         while (m>=1&&n>=1);
>    return 0;  }

> Could someone explain what I am doing wrong and try to point me in the right
> direction.  Any help would be appreciated.
>      Thanks, Mike

Do you debug this code and see what values gets m and n ?You declared m & n as
double, wherefore

scanf (" %lf", &m );
scanf (" %lf", &n );
printf (" m( %lf ) raised to the n( %lf ) power is: %lf\n", m, n, pow( m,n ) );

I checked this work.

Sun, 12 Aug 2001 03:00:00 GMT
MStoner713 schrieb:

Quote:

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

> int main (void)
> {
>         double m,n;
>    do{
>         printf("Enter a positive integer m:\n");
>         scanf ("%f",&m);

Hi MStoner713,

statement that contains an error. The "%f" specification is defined as
reading a *float*, not a double. To read a double with the "scanf()"
function use the "%lf" specification.

(Yes, this differs from "printf()", that's the catch)

Stephan
(initiator of the campaign against grumpiness in c.l.c)
(-: A brandnew excellent FAQ version has been released !!! :-)
(-: Get it: http://www.eskimo.com/~scs/C-faq/versions.html :-)

Sun, 12 Aug 2001 03:00:00 GMT

 Page 1 of 1 [ 7 post ]

Relevant Pages