P: 16

This is my code and i dont know why when i enter let's say x=3 and n=0 it gives 3.  #include <cstdlib>

#include <iostream>


using namespace std;


int main()

{

printf("==========©©Andrei78©©==========");

printf("=======©All Rights Reserved©====");

printf("===========©05062012©=========");

int count,x,u=1;

float n,y;


printf("Enter the values of x and n : ");

cin>>x>>n;

y = 1;

count = 1;

do

{

y = y*x;

count++;


}

while ( count <= n);


cout<<y;

if(n=0){

cout<<"1";

}


system("PAUSE");

return EXIT_SUCCESS;

}

 
First, before you get to your loop check to see if n is 0. If it is, skip the loop and display a 1. Anything to the 0th power is 1.
Second, if n is positive set your loop control variable to n.
Third, if n is negative, set your loop control variable to
1 *n. So 3 will end up as 3 in the loop control variable.
Fourth, save the number in x.
Fifth, inside the loop calculate y = y * x.
Sixth, after you exit the loop check if n s negative. If it is, your answer is 1/y. Otherwise, your answer is y.
Seventh, as to spending a ton of time, we once were where you are now. My story is that a barber once asked me what I did for a living. I said I was a C++ programmer. He said that sounded hard. I asked how long it took to become a barber. He said about 300 hours of class time plus 1800 hours of onthejob haircutting. I thought, hmm 2100 hours. Full tme for year. Now I tell my students C++ programming is as easy as cutting hair  after you spend 2100 hours practicing it. Good luck.
Share this Question
Expert Mod 5K+
P: 9,197

This code:  y = y*x;

count++;


}

while ( count <= n);

When n is 0 and count is 1 then count<=n is really 1<=0, which is false so you drop out of the loop.
This time x=3 and y=1 so y*n is 1*3 which is 3.
You display 3.
Beyond that, this code:
assigns 0 to n, which will always be false. Probably you meant:   Expert Mod 10K+
P: 12,297

That's because a do while loop is guaranteed to run at least once. Use a while loop instead.
 
P: 16

Got it.My bad i readed while and while but it was dowhile and while sorry
 
P: 16

Ok,i solved with positive and 0 powers.But now i am trying to do with negative power.I want it to show :
x=2
n=1
y=0.5
i changed the code
with
but nothing happens.And when i am trying to put it with if(){} it gives the same number with the x(the base) with every number i enter
  100+
P: 131

You wouldn't need to change much; add a var to check whether or not the power is negative. If it is, multiply the power by 1 and use division instead of multiplication with the loop.
Always check against div by 0.
  Expert Mod 5K+
P: 9,197

Remember, a negative power is calculated differetly from a positive power. You will need different logic inside your loop.
Plus, the loop control for 1 power needs to be +1 and the loop control for +1 power needs to be +1. I suggest you have a separate variable for your loop control.
 
P: 16

Ok so i put
and then i put  y = y*x;

count;

cout<<y;

but the output doesn't stop.I am working for like 6 hours at this project.Can u give me a hint?
  Expert Mod 5K+
P: 9,197

First, before you get to your loop check to see if n is 0. If it is, skip the loop and display a 1. Anything to the 0th power is 1.
Second, if n is positive set your loop control variable to n.
Third, if n is negative, set your loop control variable to
1 *n. So 3 will end up as 3 in the loop control variable.
Fourth, save the number in x.
Fifth, inside the loop calculate y = y * x.
Sixth, after you exit the loop check if n s negative. If it is, your answer is 1/y. Otherwise, your answer is y.
Seventh, as to spending a ton of time, we once were where you are now. My story is that a barber once asked me what I did for a living. I said I was a C++ programmer. He said that sounded hard. I asked how long it took to become a barber. He said about 300 hours of class time plus 1800 hours of onthejob haircutting. I thought, hmm 2100 hours. Full tme for year. Now I tell my students C++ programming is as easy as cutting hair  after you spend 2100 hours practicing it. Good luck.
 
P: 16

or
and then what?
how do i save a number..
  100+
P: 332

@weaknessforcats
Nice Seventh. :)
    Question stats  viewed: 1510
 replies: 10
 date asked: Jun 5 '12
