Overloaded Function/Bad Number Suffix

This is a discussion on Overloaded Function/Bad Number Suffix within the C++ Programming forums, part of the General Programming Boards category; Hello, I just finished, well sorta finished a program I have been working on for my last assignment in my ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    4

    Post Overloaded Function/Bad Number Suffix

    Hello, I just finished, well sorta finished a program I have been working on for my last assignment in my COSC C++ class. I've used all of these components in other programs before, but in this specific condition it is giving me a fit.

    The goal of the program is to calculate the conditions of the quadratic formula (will the answer be one root? two roots? or imaginary roots?), and provide the roots if it is one or two. I opted to use a switch statement instead of if/else.

    However, It seems either my secondary function is severely messed up, or I just have a small typo. However, I cannot find either.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    
    using namespace std;
    
    
    
    int DetermineRootTypes(int a, int b, int c){
    	if(b * b - 4 * a * c > 0){
    		return 1;
    	}	
    	if(b * b - 4 * a * c == 0){
    		return 2;
    	}
    	if(b * b - 4 * a * c < 0){
    		return 3;
    	}
    
    return 0;
    }
    
    
    int main(){
    
    	int a, b, c;
    	float root1;
    	float root2;
    	int condition;
    
    	cout << showpoint << fixed << setprecision(4);
    
    	for(int i = 0;i < 3;i++){
    		cout << "Please input the 3 variables of the quadratic equation. (a, b, and c)." << endl;
    			cin >> a >> b >> c;
    
    	condition = DetermineRootTypes(a,b,c);
    
    		switch(condition){ // Switch statements, in which the roots are computed.
    			case 1:
    				root1 = (-1 * b * sqrt(b * b - 4 * a * c)) / 2a;
    				root2 = (-1 * b * ( -1 * sqrt(b * b - 4 * a * c))) / 2a;
    				cout << "The answer are two real roots." << endl;
    				cout << "The first is " << root1 << endl;
    				cout << "The second is " << root2 << endl;
    			break;
    			case 2:
    				root1 = (-1 * b * 0) / 2a;
    				cout << "The answer is one repeated root." << endl;
    				cout << "The answer is " << root1 << endl;
    			break;
    			case 3:
    				cout << "The answer is two imaginary roots." << endl;
    			break;
    			default:
    				cout << "Values entered were not numbers." << endl;
    			break;
    		}
    	}
    }

    Now, for the various errors...

    Code:
    (42) : error C2668: 'sqrt' : ambiguous call to overloaded function
    (42) : error C2059: syntax error : 'bad suffix on number'
    (42) : error C2146: syntax error : missing ';' before identifier 'a'
    Those pretty much repeat for a few more lines down the code.

    Any help would be greatly appreciated. Feel free to flame my horrid coding methods too.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    a is not a valid suffix on literal numbers. If you meant to multiply 2 by a you obviously need to use the * operator (also don't forget brackets).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,662
    sqrt is overloaded for different types, and the compiler isn't sure which one you want. A cast will suffice:

    Code:
    sqrt(float(b * b - 4 * a * c))
    >> 2a

    Did you mean 2 * a, or are you trying to use a variable with that name (which you can't do, of course, since it begins with a digit)?



    ITSA
    Socket Library!

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    There are 3 defined prototypes for sqrt:
    Code:
    double sqrt (      double x );
    float sqrt (       float x );
    long double sqrt ( long double x );
    You are passing an int into the sqrt function. The problem is that none of the overloaded sqrt functions take an int, so it must be converted. But which one is it converted to? This is where the ambiguity comes from. To resolve this ambiguity, a simple cast would suffice.
    Code:
    sqrt(static_cast<double>(b * b - 4 * a * c))

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    4
    thanks all. Yeah, it was meant to be 2 * a. I just changed the outside functions inputs to float, so its not trying to shove ints in there. Thank you again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  2. Calculating next prime number
    By anilemon in forum C Programming
    Replies: 8
    Last Post: 04-17-2006, 10:38 AM
  3. suffix help.
    By milkyway_sushi in forum C++ Programming
    Replies: 1
    Last Post: 11-27-2004, 04:41 PM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  5. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 12:10 PM

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