1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C++ Help.

Discussion in 'Code Snippets and Tutorials' started by H3nry, Feb 23, 2010.

  1. H3nry

    H3nry Level III

    Joined:
    Nov 16, 2007
    Messages:
    407
    Likes Received:
    3
    Location:
    London
    Hey.

    Ok, I have a program in which the user enters a value and then I call a function which uses that value for a calculation.

    Code (Text):
    1. int xpower(int x)
    2. {
    3.     int n = 3;
    4.     int power = 1;
    5.     int i = 0;
    6.  
    7.     while (i < n)
    8.     {
    9.         power = power * x;
    10.         i = i + 1;
    11.     }
    12.     return power;
    13. }
    So the value the user enters is x and n is the power....so x^n.
    The equation I must use...n HAS to start at 3. I want to use the function, return the value then increment n by 2, the problem I think I have is that everytime I use the function i basically reset n to 3....so is there any way I can get around this?
     
  2. H3nry

    H3nry Level III

    Joined:
    Nov 16, 2007
    Messages:
    407
    Likes Received:
    3
    Location:
    London
    DUUUUUUUUUUUUUUUUUUUUUUHHHHHHHH, man I miss the sillyest things :x.

    I've just seen it myself loool.

    I took n out of the function, it didn't need to be there at all.
    I initialised n in the main body and added another parameter in the function.

    Thanks for looking anyway o_O