Why does setw misbehave when displaying the second variable (you.date)?

40 Views Asked by At

Im using setw to display my content, but one value has too much spaces which decreases the spaces from others

this is my code:

#include <iostream>
#include <cstring>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
    struct customerinfo
    {
        int policy;
        string date;
        string loc;
        string state;
        string region;
        double ivalue;
        string cons;
        string btype;
        char earth;
        char flood;
        string dummy;
    };
    
    customerinfo you;
    
    cout<<"Enter your 8-digit policy number: ";
    cin>>you.policy;
    cout<<"Enter your account expiry date [MM/DD/YYYY]: ";
    getline(cin,you.date);
    cin.ignore();
    cout<<"Enter your location [Urban or Rural]: ";
    getline(cin,you.loc);
    cin.ignore();
    cout<<"Enter your current residing state: ";
    getline(cin,you.state);
    getline(cin,you.dummy);
    cout<<"Enter your current residing region: ";
    cin>>you.region;
    cin.ignore();
    cout<<"Enter property value: ";
    cin>>you.ivalue;
    cin.ignore();
    cout<<"Enter property construction type: ";
    getline(cin,you.cons);
    cout<<"Enter your business type: ";
    getline(cin,you.btype);
    cout<<"Is earthquake coverage included? [Y or N]: ";
    cin>>you.earth;
    cout<<"Is flood coverage included? [Y or N]: ";
    cin>>you.flood;
    
    system("CLS");
    
    cout<<setw(11)<<"Policy #"<<setw(13)<<"Expiry Date"<<setw(11)<<"Location"<<setw(8)<<"State"<<setw(9)<<"Region"<<setw(16)<<"Insured Value"<<setw(14)<<"Const. Type"<<setw(10)<<"B. Type"<<setw(13)<<"Earthquake"<<setw(8)<<"Flood"<<setw(11)<<endl;
    cout<<"---------------------------------------------------------------------------------------------------------------------"<<endl;
    cout<<setw(11)<<you.policy<<setw(13)<<you.date<<setw(11)<<you.loc<<setw(8)<<you.state<<setw(9)<<you.region<<setw(16)<<you.ivalue<<setw(14)<<you.cons<<setw(10)<<you.btype<<setw(13)<<you.earth<<setw(8)<<you.flood<<setw(11);
}

after running my code and after data input: This is what i get. I has an excessive amount of spaces before "you.date" which causes the other variables to suffer

for reference, my inputs were: 12345678 date loc state reg 123 cons bus y y

0

There are 0 best solutions below