1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

string format

Discussion in 'C++' started by kumamako, Jul 13, 2009.

  1. kumamako

    kumamako New Member

    Joined:
    Jul 13, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hello guys. I have finished my project and its working perfectly fine. however, my output is not aligned properly and needs some minor adjustment. I have played around with the setw function but i really do not know what is causing the problem. I mean, the output does not look very bad. i just feel it could be much better. anyways

    1) i need to right justify the values on the far right colum(difference column).

    2) i need to align the columns perfectly as they are not aligned. you could try putting in the values and you will see what i am talkin about.

    i am attaching my file below.

    Code:
    #include<iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    using std::fixed;
    using std::right;
    using std::left;
    
    
    #include<iomanip>
    using std::setw;
    using std::setprecision;
    
    double NetIncome(double);
    void   BalanceCheck(double);
    
    
    
    int main()
    {
    
    double GrossIncome;
    double housing;
    double food;
    double automobile;
    double insurance;
    double debt;
    double entertainment;
    double clothing;
    double savings;
    double MedicalDental;
    double investments;
    double miscellaneous;
    double NetSpendableYearly;
    double NetSpendableMonthly;
    double Balance;
    double TotalActual=0;
    
    double HouseBudget;
    double FoodBudget;
    double AutoBudget;
    double InsuranceBudget;
    double DebtBudget;
    double EntertainmentBudget;
    double ClothingBudget;
    double SavingsBudget;
    double MedicalDentalBudget;
    double InvestBudget;
    double MiscBudget;
    
    double HouseBudgetMonth;
    double FoodBudgetMonth;
    double AutoBudgetMonth;
    double InsuranceBudgetMonth;
    double DebtBudgetMonth;
    double EntertainmentBudgetMonth;
    double ClothingBudgetMonth;
    double SavingsBudgetMonth;
    double MedicalDentalBudgetMonth;
    double InvestBudgetMonth;
    double MiscBudgetMonth;
    
    
    cout<<"\nEnter the Gross Income for the family: "<<endl;
    cin>>GrossIncome;
    
    cout<< "\nEnter the actual monthly costs for the following categeories: "<<endl;
    
    cout<< "Housing: ";
    cin>>housing;
    TotalActual+=housing;
    
    cout<< "Food: ";
    cin>>food;
    TotalActual+=food;
    
    cout<< "Automobile: ";
    cin>>automobile;
    TotalActual+=automobile;
    
    cout<< "Insurance: ";
    cin>>insurance;
    TotalActual+=insurance;
    
    cout<< "Debt: ";
    cin>>debt;
    TotalActual+=debt;
    
    cout<< "Entertainment: ";
    cin>>entertainment;
    TotalActual+=entertainment;
    
    cout<< "Clothing: ";
    cin>>clothing;
    TotalActual+=clothing;
    
    cout<< "Savings: ";
    cin>>savings;
    TotalActual+=savings;
    
    cout<< "Medical/Dental: ";
    cin>>MedicalDental;
    TotalActual+=MedicalDental;
    
    cout<< "Investments: ";
    cin>>investments;
    TotalActual+=investments;
    
    cout<< "Miscellaneous: ";
    cin>>miscellaneous;
    TotalActual+=miscellaneous;
                
    NetSpendableYearly=NetIncome(GrossIncome);
    NetSpendableMonthly=NetSpendableYearly/12;
    
    cout<<"\nThe Net Spendable for $"<<fixed<<setprecision(2)<<GrossIncome<<" is $"<<fixed
        <<setprecision(2)<<NetSpendableYearly<<" a year and $"<<fixed<<setprecision(2)<<NetSpendableMonthly
        <<" a month."<<endl;
        
    cout<<"\nThe budgetted spendings costs are: "<<endl<<endl;
    cout<<left<<setw(13)<<"Category:"<<setw(13)<<"Suggested"<<setw(14)<<"Yearly"<<setw(13)<<"Monthly"<<setw(13)
        <<"Actual"<<setw(13)<<"Difference"<<endl<<endl;
    
    
    
        
    HouseBudget=0.32*NetSpendableYearly;
    
    FoodBudget=0.13*NetSpendableYearly;
    
    AutoBudget=0.13* NetSpendableYearly;
    
    InsuranceBudget=0.05*NetSpendableYearly;
    
    DebtBudget=0.05*NetSpendableYearly;
    
    EntertainmentBudget=0.06*NetSpendableYearly;
    
    ClothingBudget=0.05*NetSpendableYearly;
    
    SavingsBudget=0.05*NetSpendableYearly;
    
    MedicalDentalBudget=0.04*NetSpendableYearly;
    
    InvestBudget=0.05*NetSpendableYearly;
    
    MiscBudget=0.07*NetSpendableYearly;
    
    
    HouseBudgetMonth=0.32*NetSpendableMonthly;
    
    FoodBudgetMonth=0.13*NetSpendableMonthly;
    
    AutoBudgetMonth=0.13*NetSpendableMonthly;
    
    InsuranceBudgetMonth=0.05*NetSpendableMonthly;
    
    DebtBudgetMonth=0.05*NetSpendableMonthly;
    
    EntertainmentBudgetMonth=0.06*NetSpendableMonthly;
    
    ClothingBudgetMonth=0.05*NetSpendableMonthly;
    
    SavingsBudgetMonth=0.05*NetSpendableMonthly;
    
    MedicalDentalBudgetMonth=0.04*NetSpendableMonthly;
    
    InvestBudgetMonth=0.05*NetSpendableMonthly;
    
    MiscBudgetMonth=0.07*NetSpendableMonthly;
    
    
    Balance=NetSpendableMonthly-TotalActual;
    
    cout<<fixed<<setprecision(2)<<left;
    cout<<setw(13)<<"Housing:"<<setw(13)<<"32%"<<"$"<<setw(13)<<HouseBudget
        <<"$"<<setw(13)<<HouseBudgetMonth<<"$"<<setw(13)<<housing
        <<"$"<<setw(13)<<(HouseBudgetMonth-housing)<<endl;
    
    cout<<setw(13)<<"Food:"<<setw(13)<<"13%"<<"$"<<setw(13)<<FoodBudget
        <<"$"<<setw(13)<<FoodBudgetMonth<<"$"<<setw(13)<<food
        <<"$"<<setw(13)<<(FoodBudgetMonth-food)<<endl;
        
    cout<<setw(13)<<"Auto:"<<setw(13)<<"13%"<<"$"<<setw(13)<<AutoBudget
        <<"$"<<setw(13)<<AutoBudgetMonth<<"$"<<setw(13)<<automobile
        <<"$"<<setw(13)<<(AutoBudgetMonth-automobile)<<endl;
        
    cout<<setw(13)<<"Insurance:"<<setw(13)<<"5%"<<"$"<<setw(13)<<InsuranceBudget
        <<"$"<<setw(13)<<InsuranceBudgetMonth<<"$"<<setw(13)<<insurance
        <<"$"<<setw(13)<<(InsuranceBudgetMonth-insurance)<<endl;
        
    cout<<setw(13)<<"Debt:"<<setw(13)<<"5%"<<"$"<<setw(13)<<DebtBudget
        <<"$"<<setw(13)<<DebtBudgetMonth<<"$"<<setw(13)<<debt
        <<"$"<<setw(13)<<(DebtBudgetMonth-debt)<<endl;
        
    cout<<setw(13)<<"Entertain/Rec:"<<setw(13)<<"6%"<<"$"<<setw(13)<<EntertainmentBudget
        <<"$"<<setw(13)<<EntertainmentBudgetMonth<<"$"<<setw(13)<<entertainment
        <<"$"<<setw(13)<<(EntertainmentBudgetMonth-entertainment)<<endl;
        
    cout<<setw(13)<<"Clothing:"<<setw(13)<<"5%"<<"$"<<setw(13)<<ClothingBudget
        <<"$"<<setw(13)<<ClothingBudgetMonth<<"$"<<setw(13)<<clothing
        <<"$"<<setw(13)<<(ClothingBudgetMonth-clothing)<<endl;
        
    cout<<setw(13)<<"Savings:"<<setw(13)<<"5%"<<"$"<<setw(13)<<SavingsBudget
        <<"$"<<setw(13)<<SavingsBudgetMonth<<"$"<<setw(13)<<savings
        <<"$"<<setw(13)<<(SavingsBudgetMonth-savings)<<endl; 
        
    cout<<setw(13)<<"Med/Dental:"<<setw(13)<<"4%"<<"$"<<setw(13)<<MedicalDentalBudget
        <<"$"<<setw(13)<<MedicalDentalBudgetMonth<<"$"<<setw(13)<<MedicalDental
        <<"$"<<setw(13)<<(MedicalDentalBudgetMonth-MedicalDental)<<endl;
        
    cout<<setw(13)<<"Investments:"<<setw(13)<<"5%"<<"$"<<setw(13)<<InvestBudget
        <<"$"<<setw(13)<<InvestBudgetMonth<<"$"<<setw(13)<<investments
        <<"$"<<setw(13)<<(InvestBudgetMonth-investments)<<endl;
    
    cout<<setw(13)<<"misc:"<<setw(13)<<"7%"<<"$"<<setw(13)<<MiscBudget
        <<"$"<<setw(13)<<MiscBudgetMonth<<"$"<<setw(13)<<miscellaneous
        <<"$"<<setw(13)<<(MiscBudgetMonth-miscellaneous)<<endl;
    
    cout<<setw(13)<<"Total:"<<setw(13)<<"100%"<<"$"<<setw(13)<<NetSpendableYearly
        <<"$"<<setw(13)<<NetSpendableMonthly<<"$"<<setw(13)<<TotalActual
        <<"$"<<setw(13)<<(Balance)<<endl;
     
    BalanceCheck(Balance);
    
    
    
    cin.get();  // make DOS window stay open
    cin.get();
          
    return 0;
    
    
    }
    
    
    double NetIncome(double number)
    {
    
    double value;
    
    if      (number<=7550)
        value=number-(0.10*number)-(0.10*number);
    
    else if (number<=30650)
        value=number-(0.10*number)-(0.15*number);
    
    else if (number<=74200)
        value=number-(0.10*number)-(0.25*number);
        
    else if (number<=154800)
        value=number-(0.10*number)-(0.28*number);
        
    else if (number<=336550)
        value=number-(0.10*number)-(0.33*number);
        
    else
        value=number-(0.10*number)-(0.35*number);
        
    return value;
    }
    
    
    void BalanceCheck(double number)
    {
    
    if       (number<0)
       cout<<"This budget has a deficit of $"<<setprecision(2)<<number<<" a month."<<endl<<endl;
       
    else if (number==0)
       cout<<"This budget has a zero balance of $"<<setprecision(2)<<number<<" a month."<<endl<<endl;
       
    else
       cout<<"This budget has a surplus of $"<<setprecision(2)<<number<<" a month."<<endl<<endl;
    }
    
     
    Last edited by a moderator: Jul 13, 2009
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    Attachment moved into post
     

Share This Page