helllllp!!!!

This is a discussion on helllllp!!!! within the C Programming forums, part of the General Programming Boards category; Ok here is the source for a function which according to my book should raise x to the y power. ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    78

    helllllp!!!!

    Ok here is the source for a function which according to my book should raise x to the y power.
    #include <stdio.h>



    double pow(double , double );
    int main()
    {
    double x, y, count, power;
    for (count=1; count<=15; ++count)
    {
    printf("enter the number you would like to use\n", x);
    scanf("%lf", &x);
    printf("enter the power the number is placed to\n", y);
    scanf("%lf", &y);
    power = pow( x , y);
    printf("the answer is %5.3lf\n", power);

    return 0;
    }

    double pow(double x, double y)
    {

    return(power);
    }

    I keep getting 2 error messages the first one says 'pow' local functions not supported the other says unexpected end of file found. Now can anyone tell me where I've went wrong

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Well firstly, you must be reading a CRAP book. Also, you're missing a closing brace for your for loop. As for that pow() function, well err.. yeah, that's just plain wrong. Get another book or read a few basic tutorials on functions in C.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    78

    I figured as much

    Thats what I thought dude this book has been spitting one piece of bogus code right after another. The auther didn't eve have the sense to check his own code b4 he put it in the book

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    102
    Well First off you might want to use another subject line like
    "Im having problems using my pow function"

    also code tags help. they are [ code] [ /code]


    here is a quick fix that I can come up with

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
        double x = 0, y =0;
        printf("Enter x:");
        fflush(stdout);
        x = atoi(getch());
        printf("Enter y:");
        fflush(stdout);
        y = atoi(getch());
        printf("x^y = %f",pow(x,y);
        return 0;
    }
    You are trying to use inline functions..
    I just looked over you code closer and you forgot to close your for loop..

    Here is the fix to your code

    Code:
    #include <stdio.h>
    
    
    
    double pow(double , double );
    int main()
    {
    double x, y, count, power;
    for (count=1; count<=15; ++count)
    {
    printf("enter the number you would like to use\n", x);
    scanf("%lf", &x);
    printf("enter the power the number is placed to\n", y);
    scanf("%lf", &y);
    power = pow( x , y);
    printf("the answer is %5.3lf\n", power);
    }
    return 0;
    }
    
    double pow(double x, double y)
    {
    double power= x*y;
    return(power);
    }
    :edit:
    Blast Dog beat me to it while I was writing my reply.

    If you want to learn C I would recomend that you get the K&R
    Last edited by squid; 04-04-2003 at 07:15 PM.

  5. #5
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Funny thing is that you both has printf() functions taking an argument but have no conversion specifier to take that argument.
    printf("enter the number you would like to use\n", x);
    printf("enter the power the number is placed to\n", y);
    You dont need the x, or the y.
    The keyboard is the standard device used to cause computer errors!

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    78

    that was pretty painless

    I understand everything except this portion of the code

    double pow(double x, double y)
    {
    double power= x*y;
    return(power);
    }

    why x * y;
    doesn't that say multiply x times y.

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    102

    Re: that was pretty painless

    Originally posted by drdodirty2002
    I understand everything except this portion of the code

    double pow(double x, double y)
    {
    double power= x*y;
    return(power);
    }

    why x * y;
    doesn't that say multiply x times y.
    Yea.. I was writing it while helping someone reset there password. Sorry.

    Sumpton I didnt even look at his printf() statements. I just fixed the for loop (you know cut and paste

  8. #8
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323

    Re: Re: that was pretty painless

    Sumpton I didnt even look at his printf() statements. I just fixed the for loop (you know cut and paste
    I figured, and my name is stumon, lol. No biggy. Dr Dirty, something like this would pow the number.
    Code:
    double pow(double x, double y)
    {
    int i;
    double x2 = x;
    for (i = 0; i < y; i++){
    x = x * x2;
    }
    return(x);
    }
    Or, you could include the math.h header and it has a pow() function in it. If you needed to use it.
    Last edited by stumon; 04-04-2003 at 09:38 PM.
    The keyboard is the standard device used to cause computer errors!

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    102
    LMAO.. Sorry about the name.. Doing Tech Support over the phone and tyring to respond at the same time can mess up my spelling. Thank god its friday!!!

  10. #10
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Whadda bout this?

    Code:
    #include <stdio.h>
    
    long getPow(long base, long power)
    {
       if(power == 1)
          return base;
       else
          return base * getPow(base, power - 1);
    }
    
    int main()
    {
      long num1, num2;
      
      printf("Enter the base: ");
      scanf("%ld", &num1);
      
      printf("Enter the power: ");
      scanf("%ld", &num2);
      
      printf("%ld^%ld = %ld", num1, num2, getPow(num1, num2));
      
      return 0;
    }
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21