Thread: Problem With Pow Function, Help?

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    28

    Problem With Pow Function, Help?

    Hey...

    So I'm having a bit of difficulty with a program I've put together, and it all seems to be surrounding the pow function. I can't even do a simple pow(2,2) without getting an error. And I have included the cmath library.

    This is the error I get:
    ld:
    Unresolved:
    pow

    Any ideas?
    (It's a binary to decimal converter)

    Thanks!

    Code:
              #include <iostream>
              #include <iomanip>
              #include <cmath>
    
              using namespace std;
    
              void main ()
                 {
                 int count = 0, bin, sum;
                 char line[32] ;
    
                 cin >> noskipws >> line[0] ;
                 while ( line[count] != '\n' )
                    {
                    count ++ ;
                    cin >> line[count] ;
                    }
    
                 sum = 0;
    
                 for ( bin = count - 1; bin >= 0; bin -- )
                    {
                    sum = sum + (line[bin] * pow(2,bin));
                    }
    
                 cout << sum;     
                 cout << endl ;
                 }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    main() should be defined as an int, not void.

    The pow function expects doubles, not ints, but the only one that's really required is the first arguement. Change your call to this:

    Code:
    pow(2.0,bin);
    Also, if you want this thing to work, you should really look up the difference between 1 and '1'.
    Last edited by SlyMaelstrom; 02-26-2006 at 07:04 PM.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    28
    Hey,

    Thanks for responding... I tried your suggestions, both the int in the main() and making the pow a double, but neither seem to be working. I still get the same error.

    Any ideas?

    Also, what do you mean, the difference between '1' and 1?


    Thanks for your help!


    Here's the code:
    Code:
              #include <iostream>
              #include <iomanip>
              #include <cmath>
    
              using namespace std;
    
              int main ()
                 {
                 int count = 0, bin, sum;
                 char line[32] ;
    
                 cin >> noskipws >> line[0] ;
                 while ( line[count] != '\n' )
                    {
                    count ++ ;
                    cin >> line[count] ;
                    }
    
                 sum = 0;
    
                 for ( bin = count - 1; bin >= 0; bin -- )
                    {
                    sum = sum + (line[bin] * pow(2.0,bin));
                    }
    
                 cout << sum;
                 cout << endl ;
                 }
    Last edited by verd; 02-26-2006 at 07:14 PM. Reason: Forgot Code

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Your compiler doesn't like that you're multiplying a character by a double, then. Perhaps it's reading the * as a dereferencing operator? :/

    As, I said, you have to know why the character '1' and the integer 1 are different. Go to http://www.lookuptables.com, look at what the decimal value of the character 1 is.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    your sum should also be a double!
    Code:
     #include <iostream>
              #include <iomanip>
              #include <cmath>
    
              using namespace std;
    
              int main ()
                 {
                 int count = 0, bin;
    			 double sum;
                 char line[32] ;
    
                 cin >> noskipws >> line[0] ;
                 while ( line[count] != '\n' )
                    {
                    count ++ ;
                    cin >> line[count] ;
                    }
    
                 sum = 0.0;
    
                 for ( bin = count - 1; bin >= 0; bin -- )
                    {
                    sum = sum + (line[bin] * pow(2.0,bin));
                    }
    
                 cout << sum;
                 cout << endl ;
                 }

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    28
    Hey,

    Thanks for all of the suggestions, but I'm still getting the same error. ...I don't know what to do. I just want to get past this problem with the pow function.

    Thanks!

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    28
    Even when I change the bin variable to a double number, 2.0, I still get the error.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I just want to get past this problem with the pow function.
    Have you ever succesfully used pow()? Try this: write a program that uses pow() to square the number 3 and display it. Does it work?

    Also, what do you mean, the difference between '1' and 1?
    Try this code:
    Code:
    char ch = '1';
    int bin = ch;
    
    cout<<bin<<endl;
    Is the result what you expected?

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    28
    No, I understand that. And yes, I have written several programs that have allowed me to use the pow function. I have reason to believe that it has something to do with the compiler. I've tried the same source code on another compiler and the problem was resolved.

    However, I'm having a different problem at this point...

    At present, the program is for the most part finished, and it successfully converts any positive binary number up to 32 bits into its corresponding decimal form. However, I can't seem to make it user friendly, ie, I can't get it to successfully ask the user whether or not they want to convert bin to dec, or whether or not they want to continue.

    I've got to get the program to initially ask whether or not they want to convert from bin to dec, if not the program is supposed to terminate. If so, it's supposed to execute. After it executes, it's supposed to ask the user whether or not they want to try again with another number, and at that point, it's supposed to loop.

    Here's the source code:
    Code:
    #include <iostream>
    
    #include <iomanip>
    
    #include <cmath>
    
    
    
    using namespace std;
    
    
    
    main ()
    
          {
    
          int count = 0, bin, num=0, sum=0;
    
          char forward[32], backward[32], convert, swap, cont;
    
          
    
          cout << "This program converts positive integer" << endl;
    
          cout << "binary numbers (up to 32 bits) to decimal." << endl << endl;
    
          cout << "Enter binary number you wish to convert: ";
    
               
    
               
    
              cont = 'y'; 
    
               
    
              while(cont == 'y' || cont == 'Y') 
    
              {              
    
                 cin >> noskipws >> forward[0];
    
                 
    
                 while(forward[count] != '\n')
    
                    {
    
                    count ++ ;
    
                    cin >> forward[count] ;
    
                    }
    
    
    
                 for ( bin = count-1; bin >= 0  ; bin -- )
    
                        {
    
                              swap = forward[bin];
    
                              backward[num] = swap;    
    
                              num++;
    
                        }
    
                        
    
                 sum = 0;
    
                 cout << endl << endl;
    
                 
    
                 for ( num=0; num <= count-1; num++)
    
                     {
    
                          sum = sum + ((backward[num]-48) * pow(2.0,num));                     
    
                     }
    
                     
    
                 for ( bin = 0; bin <= count-1; bin++)
    
                     {
    
                           cout << forward[bin];                 
    
                     }
    
                     
    
                     cout << " in decimal is " << sum << endl;
    
                     
    
                     cout << "Continue? (y/n): ";
    
                     cin >> cont;                
    
    
    
           }
    
    }

    This code isn't letting me insert a cin before the first cin there with the noskipws. And that includes when it loops after the first run through. If I have a cin before that intial cin there, it just takes whatever value it has stored and reruns or runs that, whether null or with previous results.

    ...Does anyone know what's wrong with this? Why is this happening?

    This should just be a matter of an if/else and a while loop, but it doesn't seem to be working so flawlessly.


    Thanks!

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Remember the main() function should be declared to return an int. To fix your looping problem, you need to eat the newline after asking if the user wants to continue (I used cin.ignore()). Also you never reset count and num.
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    
    using namespace std;
    
    int main ()
    {
    
          int bin, sum=0;
    
          char forward[32], backward[32], convert, swap, cont;
    
          
    
          cout << "This program converts positive integer" << endl;
    
          cout << "binary numbers (up to 32 bits) to decimal." << endl << endl;
    
          //cout << "Enter binary number you wish to convert: ";
    
               
    
               
    
          cont = 'y'; 
    
               
    
          while(cont == 'y' || cont == 'Y') 
    
          {              
    
             int count = 0;
             cout << "Enter binary number you wish to convert: ";
             cin >> noskipws >> forward[0];
    
                 
    
             while(forward[count] != '\n')
    
             {
    
                count ++ ;
    
                cin >> forward[count] ;
    
             }
    
    
    
             int num = 0;
             for ( bin = count-1; bin >= 0  ; bin -- )
    
             {
    
                swap = forward[bin];
    
                backward[num] = swap;    
    
                num++;
    
             }
    
                        
    
             sum = 0;
    
             cout << endl << endl;
    
             for ( num=0; num <= count-1; num++)
             {
    
                          sum = sum + ((backward[num]-48) * pow(2.0,num));                     
    
             }
    
             for ( bin = 0; bin <= count-1; bin++)
             {
                           cout << forward[bin];
             }
    
             cout << " in decimal is " << sum << endl;
    
             cout << "Continue? (y/n): ";
    
             cin >> cont;                
             cin.ignore(80, '\n');
    
           }
    
    }

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    28
    Right. Thank you. How would I reset num and count? Wouldn't I have to then reset bin as well??

    ...Do I just do a num=0; or something?


    Thanks!

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >How would I reset num and count?
    I added that in the above code. Here are the two lines I added:
    Code:
    >         int count = 0;
    .
    .
    >         int num = 0;
    I basically just moved their declarations from where you had them (at the top) to inside the loop.
    >Wouldn't I have to then reset bin as well??
    You already initialized bin in this loop:
    Code:
             int num = 0;
             for ( bin = count-1; bin >= 0  ; bin-- )
    As an aside, num could also be initialized this way:
    Code:
             int num;
             for ( bin = count-1, num=0; bin >= 0  ; bin-- )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Replies: 3
    Last Post: 10-28-2009, 09:25 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM