decimal ?

This is a discussion on decimal ? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() { float a, b, c; float x1, x2; ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    55

    decimal ?

    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
    	float a, b, c;
    	float x1, x2;
    	float root, temp;
    
    	cout << "Enter the coefficient of the quadratic function (a*x^2+bx+c): ";
    	cin >> a >> b >> c;
    
    	temp = (b*b) - (4*a*c);
    
    	if(temp>=0)
    	{
    		root = sqrt(temp);
    
    		x1 = (-b+root) / (2.0*a);
    		x2 = (-b-root) / (2.0*a);
    	
    		if (x1==x2)
    		{
    			cout << "The single root for the quadratic function -> x = "
    			 << setprecision(2) << x1 << endl;
    		}
    		else
    		{
    			cout << "The root for the quadratic function -> x = "
    			 << setprecision(2) << x1 << " and " << setprecision(2) << x2 << endl;
    		}
    	}
    	else
    		cout << "Invalid input" << endl;
    
    	system("pause");
    	return 0;
    }
    when i enter 1 2 1, the output is -1. Why the output is -1 and not -1.00 ?

  2. #2
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    I believe you need to set the format flags to floating-point, something like this IIRC:
    Code:
    cout.setf(ios_base::fixed, ios_base::floatfield);
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    55
    i declared all my variables as float .... but why it still come out as integer value ? even if i do not use the flag ... it should printf out float value right ? the value stored in each variable should be in decimal format rite ?

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    setprecision sets how precise it will be, up to 2 decimal places in this case...if both those are zero, then it wont print them out, but if the number was something like 1.2345 it prints out 1.23
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting 32 bit binary IP to decimal IP (vice-versa)
    By Mankthetank19 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 07:17 PM
  2. I need help with decimal to binary Algorithm
    By webznz in forum C Programming
    Replies: 4
    Last Post: 03-13-2008, 04:52 AM
  3. hex to binary,hex to decimal
    By groovy in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 02:14 AM
  4. Confused by expression.
    By Hulag in forum C Programming
    Replies: 3
    Last Post: 04-07-2005, 08:52 AM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 11:07 PM

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