繁体   English   中英

如何在同一位置打印两个 setfill()

[英]How to print two setfill() in same position

我正在尝试使用setfill()setw()练习左右对齐,但我遇到了一个问题:我无法用-字符打印出~字符。

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

typedef struct kereta{
    string brand, model;
    int year;
    float price;
}car;

int main(){
        
    kereta car[100];
    int num,i;
    
    cout<<"How many cars?: ";
    cin >>num;
    
    for(i=0;i<num;i++){
        cout<<"Insert brand for car "<<i+1<<": ";
        cin>>car[i].brand;
        cout<<"Insert model for car "<<i+1<<": ";
        cin>>car[i].model;
        cout<<"Insert year for car "<<i+1<<": ";
        cin>>car[i].year;
        cout<<"Insert price for car "<<i+1<<": ";
        cin>>car[i].price;
        cout<<endl;
    }
    for(i=0;i<num;i++){
        cout<<fixed;
        cout<<car[i].brand<<right<<setfill('*')<<setw(10);
        cout<<car[i].model<<right<<setfill('-')<<setw(10);
        cout<<car[i].year<<setfill('~')<<setw(15)<<right<<setfill('.')<<setw(15);
        cout<<showpoint<<setprecision(3)<<car[i].price;
        cout<<endl;
    }
    
}

我正在尝试将输出设为:

**Proton****Hahaha~~~~------2004......40000.000**

但我得到:

**Proton****Hahaha------2004......40000.000**

您应该分部分考虑输出。 setw和其他输出操纵器为下一个流的数据设置参数。

cout << left << setfill('*') << setw(10) << car[i].brand;
cout << left << setfill('~') << setw(10) << car[i].model;
cout << right << setfill('-') << setw(15) << car[i].year;
cout << right << setfill('.') << setw(15) << setprecision(3) << showpoint << car[i].price;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM