0

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

  • Are you not curious about the missing `d` in `date`? – john Jan 09 '23 at 08:40
  • I can't identify the excessive spaces in your result. Please be more explicit which you mean. – Yunnosch Jan 09 '23 at 08:41
  • The input isn't working as you expected. `you.date` is an empty string, `you.loc` is `"ate"`, the `d` has been ignored. etc. etc. Basically you have `cin.ignore()` in all the wrong places (and with the wrong arguments). – john Jan 09 '23 at 08:42
  • My advice would be to use `getline` throughout to read strings from `cin` and then if you need to convert that string to a number use `stoi` or `stod` as appropriate. – john Jan 09 '23 at 08:45
  • In case you haven't worked it out the extra space is because `you.date` is the empty string and so you are getting a double dose of spaces before `"ate"` is printed which is the value of `you.loc`. – john Jan 09 '23 at 08:47

0 Answers0