Thread: This should be fairly simple to fix.

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    2

    This should be fairly simple to fix.

    Okay, will somebody tell me what is wrong with this code?

    Code:
    #include <iostream.h>
    
    typedef unsigned short int USHORT;
    typedef unsigned long int ULONG;
    
    ULONG powerFunc(USHORT, USHORT, USHORT);
    
    int main()
    {
      USHORT num, orignum, pow; ULONG ans;
      cout<<"Enter a number and a power:\t"; cin>>num;
      cout<<"\t\t\t\t"; cin>>pow;
      orignum=num;
      ans=powerFunc(num, orignum, pow);
      cout<<"\n\n"<<num<<" to the power of "<<pow<<" equals "<<ans<<".\n\n";
      return 0;
    }
    
    ULONG powerFunc(USHORT num, USHORT orignum, USHORT pow)
    {
      cout<<"num="<<num<<" orignum="<<orignum<<" pow="<<pow<<"\n\n";
      if (pow!=1)
      {
        num=num*orignum;
        pow--;
        powerFunc(num, orignum, pow);
      }
      cout<<"num to return="<<num;
      return num;
    }
    Here is the output I get:
    Code:
    Enter a number and a power:     2
                                    4
    num=2 orignum=2 pow=4
    
    num=4 orignum=2 pow=3
    
    num=8 orignum=2 pow=2
    
    num=16 orignum=2 pow=1
    
    num to return=16num to return=16num to return=8num to return=4
    
    2 to the power of 4 equals 4.

    I've been messing with it for about an hour with no results. I am guessing it has something to do with local variables.

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    12
    Hi,

    I think your powerfunc is totally wrong. I suppose you want to do some 2^4 = 2 * 2 * 2 * 2.

    So you should be doing

    Code:
    ULONG powerFunc(USHORT num, USHORT pow)
    {
         if (pow < 1) return 1;
         else return num * powerFunc(num, --pow);
    }
    Edit : By the way in your original code num will always be 2 and orignum will always be 2 so num = num *orignum will always be 2 * 2 = 4.
    Last edited by gautamn; 03-04-2004 at 05:58 AM.
    Life is a piece of chocolates

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    2
    Thanks a lot, that works perfectly.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    nitpicking:
    Code:
    #include<iostream.h>
    should be
    Code:
    #include<iostream>
    using namespace std;
    it's more standards-compliant...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help in a fairly simple C prog
    By CodeSlow in forum C Programming
    Replies: 4
    Last Post: 01-04-2008, 02:49 AM
  2. Simple Socialising Chat Bots
    By bengreenwood in forum C++ Programming
    Replies: 10
    Last Post: 11-28-2007, 08:42 AM
  3. Simple CPP (i think)
    By Smoose777 in forum C++ Programming
    Replies: 3
    Last Post: 02-27-2002, 12:34 AM
  4. The following code has one simple error.....
    By bluehead in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 11-08-2001, 07:45 PM
  5. One simple error(IN DOS)
    By bluehead in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2001, 08:43 PM