Click here to Skip to main content
15,885,998 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
/*to evaluate exponent*/
#include<stdio.h>
#include<conio.h>
int main()
{
    clrscr();
    int i,exp;
    float tot=1,num;
    printf("enter the number and the exponent to which it should be raised:");
    scanf("%f,%f",&num,&exp);
    for(i=1;i<=exp;i++){
        tot=tot*num;
    }
    printf("the required value of the exponent is  %f",tot);
    getch();
    return 0;
}

[Edit: Fixed code formatting, added indention, added some line feeds]
Posted
Updated 20-Dec-14 3:56am
v2

The second argument to your scanf function is of type int but you use the float "%f" format specifier.

You must use the "%d" int format specifier:
scanf("%f,%d",&num,&exp);
 
Share this answer
 
Comments
CPallini 20-Dec-14 10:01am    
5.
Member 11324568 20-Dec-14 10:07am    
that doesnt work...first i did the above thing ,..second time to use decimals in the entered nuumber i changed the code.both arent working
Jochen Arndt 20-Dec-14 10:17am    
It is working for me. I have just compiled and tested it.

You may insert this line after the scanf call to see what is not working:
printf("\nYou entered: %f,%d\n",num, exp);

Or use the debugger as suggested by Griff.
Member 11324568 20-Dec-14 11:04am    
:)
Come on now!
This is your second question today, and they are both fairly simple things that a quick check with a debugger would have found.

SO, lets try and debug it shall we?
Use the debugger: put a breakpoint on the line:
C++
scanf("%f,%f",&num,&exp);

And run your program.
When it stops, single step the debugger and enter your two numbers: "2,3" will do.
When you press ENTER, you will be back to the debugger.
So - look at the two numbers you just entered.
What's the value of num? For me, it's 2.0000000
What's the value of exp? For me it's 1077936128

Now, that's not right!
So look at your code, and see if you can see why it's wildly inaccurate!
What is the difference between the two variables num and exp and how you use them?
 
Share this answer
 
Comments
CPallini 20-Dec-14 10:01am    
5.
Member 11324568 20-Dec-14 10:10am    
sorry,here i have given the wrong thing...i have done it using the correct ones..i.e i used int-%d and float -%f ..yet it doesn work..please check it..the entered values are correct but the value shows zero sometimes else it shows 1
OriginalGriff 20-Dec-14 10:26am    
And what happens when you use the debugger?
Because when I fix the format / variable type problem, "2,3" gives me "8.0000000" which is what I expect.

Please, try the debugger - it lets you see exactly what you code is doing - input, output, and what path the code is taking. It's a skill - and you don't develop it by getting others to fix problems! :laugh:
I'll help you look at it, but you so need to do most of the work. ;)

So make a note of your input, check the values in the num and exp variables are correct, and follow the code through. At what point does it get the wrong value, and where?
Member 11324568 20-Dec-14 10:37am    
ok thanks griff...and today is my second day of programming and i'm trying to learn myself...so you cant expect me to be an expert..atleast i tried to complete loops in few hours,..thanks for helping...and btw i don't even know what a debugger is..i have been just into converting all the maths functions i knew into c language...blindly...
Member 11324568 20-Dec-14 10:30am    
@originalgriff please reply! does changing the format working form your compiler?for me its showing value as 1 now..
Because exp is an int but you read it as a float

Change
C++
scanf("%f,%f",&num,&exp);

to
C++
scanf("%f,%d",&num,&exp);
 
Share this answer
 
Comments
Member 11324568 20-Dec-14 10:15am    
that didn't work debashish...in my turboc i have done it correctly...here i typed it wrong...i used int-%d float-%f yet its showing 0 or 1 AS output

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900