CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2009
    Posts
    3

    Setw help? Plus other stuff

    I'm having some trouble making my program's output look exactly like this:

    ***********************************************************

    Catoosa Utility District

    ***********************************************************
    Account Number:
    Account Name:

    Electric Consumption: xxx.xx Electric Charge: $ xxx.xx
    Surcharge: $ xx.xx

    Water Consumption: xxxx.xx Water Charge: $ xxx.xx

    Total Charge: $ xxxx.xx
    ************************************************************

    That is, all the $ lining up, and the numbers for everything. Here's the code:

    /*********************************************
    * Program 1, calculates charges *
    * for utility consumption of *
    * different categories, as ^
    * well as surcharges, then ^
    * makes it all look *
    * very pretty. *
    * *
    * Author: *
    * Date: September 21, 2009 *
    **********************************************/


    #include <iostream>
    #include <cctype>
    #include <iomanip>
    #include <cmath>
    using namespace std;

    int main ()

    {
    int acctNumber, totalCustomer;
    double kwhConsumed, waterConsumed, electricCharge;
    double waterCharge, totalCharge, surcharge;
    bool validAcctNumber, validKwh, validWater;
    bool validServiceCategory, goAgain;
    char acctName [26];
    char serviceCategory;
    totalCustomer = 0;
    char response;

    goAgain = true;
    validAcctNumber = false;
    while (goAgain == true)

    {
    while (validAcctNumber == false)
    {
    cout << "Enter 4 digit account number: ";
    cin >> acctNumber;
    cout << endl;
    if (acctNumber >= 1000 && acctNumber <= 9999)
    validAcctNumber = true;
    else
    cout << "Re-enter account number, must be between 1000 and 9999." <<endl;
    } // End accout validation loop.

    cin.ignore (80, '\n');
    cout << "Enter account name: ";
    cin.getline(acctName, 25);
    cout << endl;

    validKwh = false;
    while (validKwh == false)
    {
    cout << "Enter Kwh consumption: ";
    cin >> kwhConsumed;
    cout << endl;
    if (kwhConsumed >= 0)
    validKwh = true;
    else
    cout << "Re-enter Kwh consumption, cannot be a negative number." << endl << endl;
    } // End Kwn validation loop.

    validWater = false;
    while (validWater == false)
    {
    cout << "Enter water consumption: ";
    cin >> waterConsumed;
    cout << endl;
    if (waterConsumed >= 0)
    validWater = true;
    else
    cout << "Re-enter water consumption, cannot be a negative number." << endl << endl;
    } // End water validation loop.

    cin.ignore (80, '\n');
    validServiceCategory = false;
    while (validServiceCategory);
    {
    cout << "Enter service category, R(esidential), C(ommmercial), or I(ndustrial): ";
    cin >> serviceCategory;
    serviceCategory = toupper (serviceCategory);

    if
    (serviceCategory == 'R' ||
    serviceCategory == 'C' ||
    serviceCategory == 'I') validServiceCategory = true;
    else

    {
    cout << "Invalid service category." << endl << endl;
    cin.ignore (80, '\n');
    }
    } // End service category loop.

    cin.ignore(80, '\n');



    if (serviceCategory == 'I')
    surcharge = 10.00;
    else if (serviceCategory == 'R')
    surcharge = 5.00;
    else if (serviceCategory == 'C')
    surcharge = 25.00;
    cout << endl << endl;



    if (serviceCategory == 'R')
    if (kwhConsumed <= 500)
    electricCharge = kwhConsumed * .050;
    else if
    (kwhConsumed > 500)
    electricCharge = ((kwhConsumed - 500) * .025) + (kwhConsumed * .050);
    else if
    (kwhConsumed > 1000)
    electricCharge = ((kwhConsumed - 500) * .025) + (kwhConsumed * .050) + surcharge;
    if (waterConsumed <= 30)
    waterCharge = waterConsumed * .20;
    else if (waterConsumed > 30)
    waterCharge = ((waterConsumed - 30) * .10) + (waterConsumed * .20);
    if (kwhConsumed <= 40)
    electricCharge = 2.00;
    if (waterConsumed <= 6)
    waterCharge = 2.00;




    else if (serviceCategory == 'C')
    if (kwhConsumed <= 600)
    electricCharge = kwhConsumed * .080;
    else if
    (kwhConsumed > 600)
    electricCharge = ((kwhConsumed - 600) * .050) + (kwhConsumed * .080);
    else if
    (kwhConsumed > 1500)
    electricCharge = ((kwhConsumed - 600) * .050) + (kwhConsumed * .080) + surcharge;
    if (waterConsumed <= 30)
    waterCharge = waterConsumed * .20;
    else if (waterConsumed > 30)
    waterCharge = ((waterConsumed - 30) * .15) + (waterConsumed * .20);
    if (kwhConsumed <= 48)
    electricCharge = 2.00;
    if (waterConsumed <= 6)
    waterCharge = 2.00;



    else if (serviceCategory == 'I')
    if (kwhConsumed <= 2500)
    electricCharge = kwhConsumed * .060;
    else if
    (kwhConsumed > 2500)
    electricCharge = ((kwhConsumed - 2500) * .015) + (kwhConsumed * .060);
    else if
    (kwhConsumed > 1500)
    electricCharge = ((kwhConsumed - 600) * .015) + (kwhConsumed * .060) + surcharge;
    if (waterConsumed <= 5000)
    waterCharge = waterConsumed * .20;
    else if (waterConsumed > 5000)
    waterCharge = ((waterConsumed - 5000) * .02) + (waterConsumed * .10);
    if (kwhConsumed <= 150)
    electricCharge = 2.00;
    if (waterConsumed <= 500)
    waterCharge = 2.00;


    {

    cout << "***************************************************************" << endl << endl;
    cout << " Catoosa Utility District" << endl << endl;
    cout << "***************************************************************" << endl;
    cout << "Account Number: " << acctNumber << endl;
    cout << "Accout Name:\t " << acctName << endl << endl;


    if (serviceCategory == 'I')
    cout << "Service category: Industrial";
    else if (serviceCategory == 'R')
    cout << "Service category: Residential";
    else if (serviceCategory == 'C')
    cout << "Service category: Commercial";
    cout << endl << endl;

    cout << fixed << setprecision(2) << right;
    cout << "Electric Consumption: " << kwhConsumed << setw(8) << left;
    cout << "\t\tElectric charge: $ " << setw(8) << electricCharge << right;

    cout << "\t\t\tSurcharge: $ " << setw(8)<< surcharge << right;
    cout << endl << endl;
    cout << "Water Consumption: " << setw(8) << waterConsumed << left;
    cout << "\t\tWater charge: $ " << setw(8) << waterCharge << right << endl;
    cout << endl << endl;

    totalCharge = electricCharge + waterCharge + surcharge;
    cout << "\t\t\tTotal charge: $ " << setw(8) << totalCharge << right;
    cout << endl << endl;
    cout << "***************************************************************" << endl << endl;



    cout << "Go Again? Y/N: ";
    cin >> response;
    totalCustomer++;
    if (response == 'n' || response == 'N')
    goAgain = false;
    cout << endl << endl;
    cout << "Total customers: " << totalCustomer;

    }
    getchar();
    }

    return 0;
    }


    My apologies if the spacing isn't right, I'll go back and edit it to look normal. I posted the entire program because, as the ttile indicates, I'm having trouble with some other things as well. The totalCustomer++ only works when I say Yes to go again, and when I say No, it just immediately exits the program. I need to know how to get it to not do that. Also the math worries me a bit. I'll give more info on that later; the main problem is making the output look good.

  2. #2
    Join Date
    Sep 2009
    Posts
    3

    Re: Setw help? Plus other stuff

    It would seem I can't edit posts. This poses a bit of a problem.

  3. #3
    Join Date
    Sep 2009
    Posts
    3

    Re: Setw help? Plus other stuff

    I fixed the output to look decent enough, but now the running total and the bool goAgain are messing up. I need the program to stop when I say no to go again, and I don't know how to make it not say "Total Customers" when I say yes. Also when I say yes it doesn't ask for the account number again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured