Objectives: using the iomanip library to format screen output

195 views Asked by At

Hello everyone this is my code and I just help I managed to correct the first 3 questions but the rest I am still getting errors.

Below is the all question :

Complete the provided main() program with statements to accomplish each of the following. In each case you must use the appropriate I/O stream manipulators to produce the appropriate output wherever possible.

  1. Output first first as an integer value, followed by a space, then in its written form.
  2. Output second as a base ten value, followed by a space, then as a hexadecimal
  3. value, followed by a space, then as an octal value. Make sure the appropriate base indicator prefix is shown in the output.
  4. Output third.
  5. Output fourth with four digits, with the sign shown at the left, and the value right aligned. The decimal point must also appear.
  6. Output fourth with four significant figures.
  7. Output fifth with seven significant figures. (Note: use left alignment here)
  8. Output fifth with three digits to the right of the decimal point.
  9. Output third.
  10. Output fourth with two digits to the right of the decimal point.
  11. Output sixth with no decimal portion showing
  12. Output fourth with eight digits to the right of the decimal point.
  13. Output sixth with six digits.

Here is my code so far :

#include <iostream>
#include <iomanip>
using namespace std;
int
main0()
{
    bool first;
    int second;
    long third;
    float fourth;
    float fifth;
    double sixth;

    cout << "Enter bool, int, long, float, float, and double values: ";
    cin >> first >> second >> third >> fourth >> fifth >> sixth;
    cout << endl;


    cout << noboolalpha << first;
    cout << " ";
    cout << boolalpha << first << endl;

    cout <<left << dec << showbase;
    cout << second;
    cout << " ";
    cout << internal << hex << showbase;
    cout << second;
    cout << " ";
    cout <<right << oct <<showbase;
    cout << second << endl;
    cout << third<< scientific<< endl;

    cout <<left << setw(4)<<fixed<< fourth <<endl;
    cout <<setprecision(4)<< fourth <<endl;

    cout <<left<<setw(7)<< fifth << endl;
    cout <<right<<setprecision(3)<< fifth;

    cout <<third<<endl;

    cout <<right<<setw(2)<<fourth<<endl;

    cout << fixed<<sixth<< endl;

    cout << right << fixed<<setprecision(8)<< fourth<< endl;

    cout <<left<<showpoint <<setprecision(6)<<sixth;










// ***** Solution ends here ****

    cin.get();
    return 0;
}
1

There are 1 answers

0
gsamaras On

I answered 4-6:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    long third = 123654123654123LL;
    float fourth = 12335.67890;

    std::ios initialState(nullptr);
    initialState.copyfmt(std::cout);

    // 4
    cout << third << scientific<< endl;

    // 5
    cout << showpoint << fixed << setprecision(4) << right << showpos << fourth << endl;

    cout.copyfmt(initialState);

    // 6
    cout << setprecision(4) << fourth << endl;

    return 0;
}

Good luck with the rest.