bader
bader

Reputation: 3

How to print two setfill() in same position

I'm trying to practice left and right justification with setfill() and setw() but I'm facing a problem: I cannot print out the ~ character with the - character.

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

I'm trying to get the output as:

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

but instead I get:

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

Upvotes: 0

Views: 142

Answers (1)

Anton Shwarts
Anton Shwarts

Reputation: 525

You should think about output in parts. setw and other output manipulators sets parameters for the next stream's data.

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;

Upvotes: 1

Related Questions