【问题标题】:Objectives: using the iomanip library to format screen output目标:使用 iomanip 库格式化屏幕输出
【发布时间】:2017-09-06 03:10:25
【问题描述】:

大家好,这是我的代码,我只是帮助我纠正了前 3 个问题,但其余的我仍然遇到错误。

以下是所有问题:

使用语句完成提供的main() 程序以完成以下各项。在每种情况下,您都必须尽可能使用适当的 I/O 流操纵器来产生适当的输出。

  1. 首先输出一个整数值,然后是一个空格,然后在 书面形式。
  2. 输出秒为以十为底的值,后跟一个空格,然后作为 十六进制
  3. 值,后跟一个空格,然后是八进制值。确保 输出中显示了适当的基本指标前缀。
  4. 输出第三个。
  5. 输出第四个四位数字,符号显示在左侧,并且 值右对齐。小数点也必须出现。
  6. 输出第四个有效数字。
  7. 输出第五位,有七位有效数字。 (注意:这里使用左对齐)
  8. 输出第五个小数点右边三位。
  9. 输出第三个。
  10. 输出第四个小数点右侧两位数。
  11. 输出第六个,不显示小数部分
  12. 输出第四个小数点右边八位数字。
  13. 以六位数输出第六个。

到目前为止,这是我的代码:

#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;
}

【问题讨论】:

  • 为了帮助人们回答您的问题,您需要更具体地说明错误。请edit 您的帖子包含您从minimal reproducible example 获得的确切错误(最好使用复制+粘贴以避免转录错误)。

标签: c++ io iomanip


【解决方案1】:

我回答 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;
}

祝你好运。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多