如何解决如何在相同位置打印两个setfill
我正在尝试使用setfill()
和setw()
左右对齐,但是我遇到了一个问题:我无法使用{{1}打印出~
字符}字符。
-
我试图将输出显示为:
#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**
解决方法
您应该考虑部分输出。 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;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。