#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
usingnamespace std;
int main()
{
double a, b, area, circumference, focus;
cout <<" This program calculates the focus, area and circumference of a ellipse "<<endl;
cout <<" Enter the semi-major axis value: "; cin>> a;
cout <<" Enter the semi-minor axis value: "; cin>> b; cout<<"\n";
cout <<" Thank you "; cout<<"\n";
// formulas for focus, area and circumference of the ellipse.
focus = sqrt(pow(a,2)-pow(b,2));
area = M_PI*a*b;
circumference = (M_PI*3*(a+b)) - (M_PI*sqrt((10*a*b)+3*pow(a,2)+3*pow(b,2)));
cout << " The focus of the ellipse is: " <<setw(15)<<setprecision(2)<<fixed<<focus<<endl;
cout << " The ellipse area is: " <<setw(15)<<setprecision(2)<<fixed<<area<<endl;
cout << " The ellipse circumference is: " <<setw(15)<<setprecision(2)<<fixed<<circumference<<endl;
return 0;
}
Put the code you need help with here.
i want to know how to format the output of focus,area and circumference line up on one column from right to left
If you want the words and numbers to loo like they are in two columns then in the final cout statements you need to precede the quoted text with a "setw()". 31 seemed to work the best although you might want to decrease the 15.
Before the final three cout statements you just need one line like std::cout << std::fixed << std::showpoint << std::setprecision(2);. This will affect every cout after. The "showpoint" will print ".00" if that happens.
thank you! also you said it will affect every cout after but is there anyway so that i could change some specific cout without being modified by this line
I guess what I should have said is that the line I showed you will affect every floating point number in any cout that follows until it is changed. If in one of the cout statements you need to change the number of decimal places, just add a new setprecision before the variable to change that variable and the another setprecision before the next variable to change it back or to something different.
The fixed and showpoint only needs to be done once. Unless you switch between fixed an scientific or between showpoint an nosshowpoint.
If I think of something else later I will let you know.
#include <iostream>
#define _USE_MATH_DEFINES // for M_PI (note: M_PI is not defined in standard C++).
#include <cmath>
#include <iomanip>
usingnamespace std;
int main()
{
cout <<" This program calculates the focus, area and circumference of a ellipse\n" ;
double a, b ;
cout <<" Enter the semi-major axis value: "; cin>> a;
cout <<" Enter the semi-minor axis value: "; cin>> b;
cout <<"\n Thank you \n\n";
// formulas for focus, area and circumference of the ellipse.
constdouble focus = sqrt( pow(a,2) - pow(b,2) );
constdouble area = M_PI*a*b;
constdouble circumference = (M_PI*3*(a+b)) - (M_PI*sqrt((10*a*b)+3*pow(a,2)+3*pow(b,2)));
std::cout << std::fixed << std::setprecision(2) ;
cout << " The focus of the ellipse is: " << setw(15) << focus << '\n';
cout << " The ellipse area is: " << setw(15) << area << '\n' ;
cout << " The ellipse circumference is: " << setw(15) << circumference << '\n' ;
}