Jim Blumberg posted

Quote

This link may explain the width and precision modifiers a little better.

Jim

regarding formatting numbers.

I have a similar question re: floating point numbers, the questions are in the comments of the code. I would be interested in how other beginners would attack the problem as well, sharing ideas and all that stuff.

Bear with my programming style, I am trying something newsince I started reading Practical C++ Programming from O'Reilly, awesome book with many insights into programming in general.

On with the code.

/*************************************************************************/ /* sphere.cpp: -- program to calculate the volume of a sphere */ /* */ /* Author: -- Bryston */ /* */ /* PURPOSE: -- Practical C++ exercise 5-2 */ /* */ /* USAGE: -- user enters a number */ /* */ /*************************************************************************/ #include<iostream>// for all the i/o goodness #include<iomanip> using namespace std;// so we don't have to use verbose std::cin etc... // Global Variables const long double PI=3.1415926; // declare PI as a constant long double sphere_radius=0.0; //radius of the sphere from the user long double sphere_volume=0.0; //volume of the sphere long double r_cubed=0.0; //the value of the radius cubed /************************************************************************** ******************************** WARNING ********************************** ******************* IF YOU STEAL THIS CODE AND SUBMIT IT ****************** ***************** AS YOUR HOMEWORK YOU MAY FAIL YOUR COURSE**************** *********** AND END UP FLIPPING BURGERS FOR THE REST OF YOUR LIFE********** *************************************************************************** **************************************************************************/ int main(){ //>>>>>>>>>>>>>>>>>>>>> Initial Display Section <<<<<<<<<<<<<<<<<<<<<<<<<<< while (1){ cout << endl << "Calculate the volume of a sphere!"; cout << endl << "The user supplies the radius of the sphere."; cout << endl << "Will display the results of ((4/3)*PI)*r^3"; cout << endl << endl << "Please enter the value for r (0 to exit):> "; //>>>>>>>>>>>>>>>>>>>>> Get User Input Section <<<<<<<<<<<<<<<<<<<<<<<<<<<< cin >> sphere_radius;// user supplied floating point value if (sphere_radius == 0)// give the user a way to exit the program break;// exit the while loop //>>>>>>>>>>>>>>>>>>>>> Process Data Section <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /* Calculate volume of a sphere from known radius supplied by the user. Using the variable r_cubed to make the actual formula flow a little better. Could have done it by including the cmath header file and using pow(r,3) */ r_cubed = sphere_radius * sphere_radius * sphere_radius; // remember to use the decimal point for type float or double!!! sphere_volume = ((4.0 / 3.0) * (PI * r_cubed)); //>>>>>>>>>>>>>>>>>>>>> Display Results Section <<<<<<<<<<<<<<<<<<<<<<<<<<< /************************ UNEXPECTED RESULTS *****************************/ // any input that yields a result higher than 99999.9 (approx 28.75) // will cause the decimal point to go POOFng // compiled it in DevC++ and Visual C++2010 with equal results // using setprecision(n) from <iomanip> gives a better result // but doesn't scale well to a wide range of inputs // too precise for small inputs && not precise enough for large inputs // input of 2880 once again strips the decimal if setprecision(12) // is there a way to use setprecision or something else to force // a result that is always exactly (n) digits after the decimal point? cout << endl << endl << "A sphere with the radius : " << sphere_radius; cout << " units" << endl << "has a volume of "; // setprecision(n) inline helps preserve the decimal up to a point cout << setprecision(12) << sphere_volume << " cubic units."; cout << endl << endl; }// end while loop return (0); }// end function main