POW function

Nov 3, 2008 at 12:55am
wrote code that asks you to enter 2 numbers, x and y. program is then supposed to take x and use it as the base and take Y and use it as the exponent and print the result.I am supposed to be storing x and y as long integers also.. when I execute this (using x = 4 and y = 2) I get the following:
4 to the power of -858993460 is 2.

I looked at a lot of info online and they all talk about using POW as double pow, float pow. I dont understand the difference. I think I am getting the error in how I am declaring x y and z but am not sure. Only 2 weeks into this class and my first programming adventure-any help apprecaited

#include <stdafx.h>
#include<math.h>
int
main( void )
{
char name [40];
double x;
double y;
double z;

printf("Type in your name and press return.\n");
scanf("%s",&name);
printf("Hello %s. Enter your first number-this will be the base.\n",name);
scanf("%d",&x);
printf("Enter your second number-this will be the exponent.\n");
scanf("%d",&y);
z = pow(x,y);
printf( "%d to the power of %d is %d\n",x,y,z);
}
Nov 3, 2008 at 12:58am
Try declaring x y and z with default values (like 0.0) and see if you get any default values at the end.
Nov 3, 2008 at 1:13am
mate, I took, modified and compiled ur code and it works fine, check it out:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream.h>
#include<conio.h>
#include<math.h>
int main( void )
{
char name [40];
double x;
double y;
double z;

cout<<"Type in your name and press return.\n";
cin>>name;
cout<<"Hello "<<name<<" Enter your first number-this will be the base, "<<name;
cin>>x;
cout<<"Enter your second number-this will be the exponent. ";
cin>>y;
z = pow(x,y);
cout<<x<<" to the power of "<<y<<" is "<<z<<"\n";
getche();
}


I've found out that cin and cout are much more accurate than printf and scanf, basically I only replaced printf and scanf for cin and cout and it worked

Nov 3, 2008 at 1:20am
Now I get 4 to the power of 0 is 2
I cannot see why it picks up the x value but not the y value that is input
Nov 3, 2008 at 1:24am
is that happening with the code I posted? It works perfect for me, which compiler are u using?
Nov 3, 2008 at 1:27am
well, not working for me. tells me i have undeclared identifiers. that aside, cout and cin are unheard of to me, only been in the class 2 weeks. I looked it up and it is in the last chapter of my text. Given that, I think my professor may find it suspect if I use it. I do appreicate your help and from I just read it seems easier than printf and scanf
Nov 3, 2008 at 1:28am
coule be the compiler. I am using visual c++ 2008 express.
Nov 3, 2008 at 1:33am
if ur having too much trouble, you can calculate the result without using the "pow" function, it is a little harder, but if pow doesn't work and u really need it, it's worth a shot
Nov 3, 2008 at 1:37am
thanks-will give that a try
Nov 4, 2008 at 7:59pm
First off this is a pretty simple assignment. Your problem is not in your use of the pow() function but in the variable types and specifiers used in the program. They are not all consistent with each other.

(1) You declared your variables x, y, and z as doubles not long int as your original post said the assignment calls for.

(2) Your scanf's from the original post should be
scanf("%ld",&x); or scanf("%li",&x); for long int
and
scanf("%ld",&y); or scanf("%li",&y);
to match the changes in (1).

(3) The specifiers (%d's) in your final printf() statement do not match your original requirements of using long int variable declarations.
Your original code line is
printf( "%d to the power of %d is %d\n",x,y,z);
and means all of the variables will be printed to the screen as int, but you originally declared them as double. They should be long int. To follow the guidelines of the assignment, you should change the line to read like this...
printf( "%ld to the power of %ld is %ld\n",x,y,z);
or
printf( "%li to the power of %li is %li\n",x,y,z);
Just like the scanf's above. Pay careful attention to the use of specifiers in the printf/scanf functions. In the future remember that mixing and switching types of variables can lead to loss of data. Make sure your input, output, and calculations all are consistent with your variable type declarations!!

Also are you programming in c or c++. It makes a difference as to whether you use printf/scanf or cin/cout respectivley. I would assume you are starting in C not C++ which is why the cin/cout statements are in the back of your book!! Stick with the printf/scanf functions until they are properly introduced by your instructor so you don't get yourself confused. Also if you are using C you should use
1
2
#include <stdio.h>
#include <stdlib.h> 
in the pre-processor directives, unless the header file #include <stdafx.h> is covering the requirements these originally would. Standard Input Output <stdio.h> and Standard Library <stdlib.h> are exactly what they sound like, pretty standard for most simple programs like this, and should help remedy some of the problems. Add these changes to the original code ad see what you get.
Topic archived. No new replies allowed.