淡泊名利韬韬 2023-03-30 20:15 采纳率: 100%
浏览 30
已结题

C++制表符显示问题

我在Ubuntu下编写了如下程序,使用制表符:

cout << "Please enter one of the following choices: " << endl;
cout << "c) carnivore\tp) pianist." << endl
         << "t) tree\tg) game" << endl;

并获得了如下输出

Please enter one of the following choices: 
c) carnivore    p) pianist.
t) tree g) game

但我希望获得如下格式的输出:

Please enter one of the following choices:
c) carnivore    p) pianist
t) tree         g) game

想问一下我的问题出在哪里,万分感谢。

  • 写回答

2条回答 默认 最新

  • Vayne16 2023-03-30 20:17
    关注

    该回答引用GPT:
    你的问题可能是因为没有设置正确的制表符宽度,导致输出的格式不正确。可以使用setw函数来设置制表符的宽度,如下所示:

    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
        cout << "Please enter one of the following choices: " << endl;
        cout << setw(20) << "c) carnivore" << setw(20) << "p) pianist." << endl
             << setw(20) << "t) tree" << setw(20) << "g) game" << endl;
        return 0;
    }
    

    运行上面的代码,可以得到如下输出:

    Please enter one of the following choices:
    c) carnivore p) pianist.
    t) tree g) game
    如还有疑问,可留言帮助解决。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月8日
  • 已采纳回答 3月31日
  • 修改了问题 3月30日
  • 创建了问题 3月30日

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: