pow function not working

This is a discussion on pow function not working within the C++ Programming forums, part of the General Programming Boards category; This is supposed to be a very simple program but for some reason I cannot get the pow function to ...

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

    pow function not working

    This is supposed to be a very simple program but for some reason I cannot get the pow function to work. I keep on coming up with an error that states
    "could not find a match for pow(float), am I missing something? Thanks.

    Code:
    // Basic C++ info
    
    #include <iostream>
    #include <string>
    #include <math.h>
    
    using namespace std;
    
    // Declare Global Variables
    float fltNumbac, fltBacpresent, fltDays;
    
    // Modules
    void MainMenu();
    void BacteriaLoop();
    
    int main()
    {
    	MainMenu();
    	cout << "Day     Number of Bacteria Present" << endl;
    	BacteriaLoop();
    
    	return 0;
    }
    
    
    void MainMenu()
    {
    	cout << " Welcome to the WilliamK99 Bacteria Calculator!" << endl;
    	cout << " How Many Bacteria are Present at the Start of the Experiment?";
    	cin >> fltBacpresent;
    	
    	return;
    }
    
    void BacteriaLoop()
    {
    	while (fltDays <=10)
    	{
    		fltNumbac = fltBacpresent * pow(fltDays / 10);
    		cout << fltDays << "           " << fltNumbac << endl;
    		fltDays++;
    	}
    	return;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to link with libm, so add "-lm" to your commendline, e.g.:
    Code:
    gcc x.c y.c -lm
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    I think the problem is that pow() takes two arguments, the base and the exponent, but you are passing only one argument.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    The error is because you are calling pow() wrong, and the C++ compiler cannot find a template for the pow() function with one parameter.

    http://www.cppreference.com/stdmath/pow.html

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    If you want to exponentiate using base e, use exp() instead of pow(). pow() takes two arguments.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    5
    Thanks I changed
    fltNumbac = fltBacpresent * pow(fltDays / 10);

    to

    fltNumbac = pow(fltBacpresent, fltDays/10);


    and it worked like a charm.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM

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