format editing

Write your question here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  #include <iostream>
#include <string>
#include <cmath>
#include <iomanip>

using namespace 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
Last edited on
Hello andersonlam,

You already have good start.

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.

Hope that helps,

Andy
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
Hello andersonlam,

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.

Hope that helps,

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#define _USE_MATH_DEFINES // for M_PI (note: M_PI is not defined in standard C++).
#include <cmath>
#include <iomanip>

using namespace 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.
    const double focus = sqrt( pow(a,2) - pow(b,2) );
    const double area = M_PI*a*b;
    const double 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' ;
}
Topic archived. No new replies allowed.