Domanda

I have a function that iterates through some parallel arrays and prints the contents at the loop control variable. I have another that is used throughout my program to print the names associated with the ID at value of loop control variable. Here are the 2 functions

void PrintName(int ID)
{
    switch (ID)
    {
    case 0: cout << "computer(s)";
        break;
    case 1: cout << "pencil(s)";
        break;
    case 2: cout << "pen(s)";
        break;
    case 3: cout << "book(s)";
        break;
    case 4: cout << "beer(s)";
        break;
    case 5: cout << "ruler(s)";
        break;
    case 6: cout << "stereo(s)";
        break;
    case 7: cout << "refrigerator(s)";
        break;
    case 8: cout << "desk(s)";
        break;
    case 9: cout << "backpack(s)";
        break;
    }
}

void PrintSummary(InvIDList invIDs, ItemQuantity itemQuant, int totalWeight, int totalPurch, int budget)
{
    cout << "ID      NAME      QUANTITY" <<endl;
    cout << "--------------------------" <<endl;
    for(int i = 0; i < 10; i++){
        cout << invIDs[i];
        PrintName(i);
        cout << itemQuant[i] <<endl;
    }
    cout << "\n\nTotal Amount Spent: $" << totalPurch <<endl;
    cout << "Amount of Budget Remaining: $" << budget <<endl;
    cout << "Total Weight of Items Purchased: " << totalWeight <<endl;
}

I want the output to align nicely under ID, NAME, and QUANTITY, with ID being leftmost, QUANTITY being the rightmost, and NAME centered. Unfortunately I cannot figure out how to achieve this after hours of tinkering with the code, mainly getting QUANTITY aligned all the way to the right.

È stato utile?

Soluzione

Instead of using the PrintName function, you want to return a string object to use with std::cout. In order to correctly align the columns you may want to look on the <iomanip> header and especially the std::setw(n) manipulator.

Here is an example, take note that while I set the column widths to 16 characters, what you should do is calculate the maximum number of characters for each category and change the widths accordingly. What is nice about this is that you can specify a different width for each column.

string ReturnName(int ID)
{
    switch (ID)
    {
    case 0: return "computer(s)";
        break;
    case 1: return "pencil(s)";
        break;
    case 2: return "pen(s)";
        break;
    case 3: return "book(s)";
        break;
    case 4: return "beer(s)";
        break;
    case 5: return "ruler(s)";
        break;
    case 6: return "stereo(s)";
        break;
    case 7: return "refrigerator(s)";
        break;
    case 8: return "desk(s)";
        break;
    case 9: return "backpack(s)";
        break;
    }
}

void PrintSummary(InvIDList invIDs, ItemQuantity itemQuant, int totalWeight, int totalPurch, int budget)
{
cout << left << setw(16) << "ID";
cout << left << setw(16) << "NAME";
cout << left << setw(16) << "QUANTITY" << endl;
cout << "--------------------------------------" << endl;
for(int i = 0; i < 10; i++){

    cout << left << setw(16) << invIDs[i];
    cout << left << setw(16) << ReturnName(i);
    cout << left << setw(16) << itemQuant[i] << endl;
}
cout << "\n\nTotal Amount Spent: $" << totalPurch <<endl;
cout << "Amount of Budget Remaining: $" << budget <<endl;
cout << "Total Weight of Items Purchased: " << totalWeight <<endl;
}

Altri suggerimenti

Don't print the name in PrintName, but rather just return the name as a string. Then in your loop you have all the information you need to properly align the columns.

Oh, and you might want to change the name of PrintName.

U have printed the heading as

cout << "ID      NAME      QUANTITY" <<endl;

U are assuming the spaces as given above but you never know how much space

invIDs[i]

and

 PrintName(i);

take.So you can take care of the maximum length of each of these.For e.g. know what is the maximum length any of the output from switch can take etc.Then modify your heading print statement including that many spaces.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top