Help with Program Output?

Discussion in 'Mac Programming' started by bravens52, Mar 26, 2010.

  1. bravens52 macrumors regular

    Joined:
    Jul 16, 2009
    #1
    hey guys i just finished my cs program and i need help formatting it i need to get it to look like this:

    http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm7.htm

    (GO TO PART THAT SAYS VOID ARRAYS)!!!!




    but i am getting this:


    Annual Garage Sale
    Family Item Id # Sale Price Quantity Sold Sale Amount
    -----------------------------------------------------------------------------
    Corinthos 12345 15.95 1 15.95
    Morgan 67800 50.00 3 150.00
    Webber 32145 4.50 2 9.00
    Davis 98765 75.00 1 75.00
    Spencer 44496 11.30 2 22.60
    Drake 67356 3.50 4 14.00
    Quartermaine 54862 52.50 1 52.50


    This is the part in my program that does this information (Im using my setw()'s but i still dont know how to get it formatted to it can be neat and straight.

    void printArrays(int family[], int item[], int quantity[], double price[], int numSales)
    {



    cout << " Annual Garage Sale \n";
    cout << "Family Item Id # Sale Price Quantity Sold Sale Amount" << endl;
    cout << "------------------------------------------------------------------------------------------------------" <<endl;



    for (int i=0; i < numSales; i++)
    {

    cout << setprecision(2) << fixed << showpoint << familyName(family) << setw(20) << item << setw(23) << price << setw(23) << quantity << setw(20) << price * quantity << endl;

    }

    }

    sorry about long post but if someone could be of some help that would be great.
     
  2. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #2
    Wasn't this due Wednesday? I see it's due by 11:59 PM this evening now.

    How about something along the line of:

    Code:
                // Item Id #
                std::cout << std::right << std::setw(9);
                std::cout << item[i];
    
                // Sale Price
                std::cout.precision(2);
                std::cout.setf(std::ios::fixed,std::ios::floatfield);
                std::cout << std::right << std::setw(15);
                std::cout << price[i];
    
     
  3. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #3
    I see that your assignments are available well before their due dates and that your next one, Assignment 8, is due "Friday, April 2 in the Blackboard Assignment Dropbox by 11:59 PM". If I were you (and I'm not) I would've started it by now.

    It says here that Assignment 7 is due by "Friday, March 26 in the Blackboard Assignment Dropbox by 11:59 PM".

    Anyway since Assignment 7 is past due I thought I'd throw this one out here for you to look over:

    Code:
    // <http://forums.macrumors.com/showthread.php?p=9494254>
    //
    // Assignment description:
    // <http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm8.htm>
    
    
    // MARK: Header Files
    
    #include <fstream>
    #include <iomanip>
    #include <ios>
    #include <iostream>
    #include <string>
    
    
    // MARK: -
    // MARK: Subroutines
    std::string familyName(int familyCode)
    {
        const char* names[] = {
              "Drake"           // 0
            , "Spencer"         // 1
            , "Morgan"          // 2
            , "Corinthos"       // 3
            , "Webber"          // 4
            , "Quartermaine"    // 5
            , "Davis"           // 6
        };
        
        return names[familyCode];
    }
    
    
    // !!!!: Since I wasn't in your lecture I can't be certain of the form of the presented 'selection sort'
    
    void sortArrays(int family[], int item[], int quantity[], double price[], int numSales)
    {
        #define DEBUG_ARRAY { for (int i_ = 0; i_ < numSales; i_++) std::cout << family[i_] << ", "; std::cout << "\n"; }
        #define MIN_ENTRY(ARRAY_, LHS_, RHS_) ((ARRAY_[(LHS_)] < ARRAY_[(RHS_)]) ? (LHS_) : (RHS_))
    
    //  DEBUG_ARRAY
        for ( int iii = 0; iii < numSales-1; iii++ )
        {
            int i = iii;
            for ( int ii = iii+1; ii < numSales; ii++ )
            {
                i = MIN_ENTRY(family, i, ii);
    //          i = MIN_ENTRY(price, i, ii);
            }
            
            // swap entry 'i' with 'iii' in all four arrays
            int     ti;
            ti  = family[iii];      family[iii]     = family[i];        family[i]   = ti;
            ti  = item[iii];        item[iii]       = item[i];          item[i]     = ti;
            ti  = quantity[iii];    quantity[iii]   = quantity[i];      quantity[i] = ti;
            
            double  tf;
            tf  = price[iii];       price[iii]      = price[i];         price[i]    = tf;
    //      DEBUG_ARRAY
        }
        
        #ifdef DEBUG_ARRAY
        #undef DEBUG_ARRAY
        #endif
            
        #ifdef MIN_ENTRY
        #undef MIN_ENTRY
        #endif
    }
    
    
    void printArrays(int family[], int item[], int quantity[], double price[], int numSales)
    {
        if ( numSales )
        {
            std::cout << "                             Annual Garage Sale                               " << std::endl;
            std::cout << std::endl;
            std::cout << "Family              Item Id #     Sale Price     Quantity Sold     Sale Amount" << std::endl;
            std::cout << "------------------------------------------------------------------------------" << std::endl;
            std::string  strFamilyName;
            
            for ( int i = 0; i < numSales; i++)
            {
                strFamilyName = familyName(family[i]);
                
                // Family
                std::cout << std::left << std::setw(20);
                std::cout << strFamilyName;
    
                // Item Id #
                std::cout << std::right << std::setw(9);
                std::cout << item[i];
    
                // Sale Price
                std::cout.precision(2);
                std::cout.setf(std::ios::fixed,std::ios::floatfield);
                std::cout << std::right << std::setw(15);
                std::cout << price[i];
    
                // Quantity Sold
                std::cout << std::right << std::setw(18);
                std::cout << quantity[i];
    
                // Sale Amount
                std::cout.precision(2);
                std::cout.setf(std::ios::fixed,std::ios::floatfield);
                std::cout << std::right << std::setw(16);
                std::cout << price[i] * quantity[i];
                std::cout << "\n";
            }
        }
    }
    
    
    int buildArrays(int family[], int item[], int quantity[], double price[])
    {
        std::ifstream   ifs("data7.txt");
        if ( ifs.is_open() )
        {
            int index = -1;
            do
            {
                index++;
                
                if ( ifs ) ifs >> family[index];
                if ( ifs ) ifs >> item[index];
                if ( ifs ) ifs >> quantity[index];
                if ( ifs ) ifs >> price[index];
            } while ( ifs );
            
            // 'ifs.close()' not really needed as file will be closed when object 'ifs' goes out of scope ...
            // ... uncomment if instructor truely 'REQUIRES' that if be called!
    //      ifs.close();
            
            return index;
        }
    
        // --- error - file must not've been opened
        std::cout << "input file did not open";
        exit(EXIT_SUCCESS);
    }
    
    
    // MARK: -
    // MARK: Program Entry
    
    int main(int argc, const char* argv[])
    {
        const int   MAX_ENTRIES = 30;
    
        int     family[MAX_ENTRIES];
        int     item[MAX_ENTRIES];
        int     quantity[MAX_ENTRIES];
        double  price[MAX_ENTRIES];
        
        int num_entries = buildArrays(family, item, quantity, price);
        printArrays(family, item, quantity, price, num_entries);
    
        std::cout << std::endl << std::endl;
    
        sortArrays(family, item, quantity, price, num_entries);
        printArrays(family, item, quantity, price, num_entries);
        
        return 0;
    }
    
     

Share This Page