Need some output formatting help!
Mar 13, 2017 at 7:02am UTC
I just need some quick help on my formatting. I've been spending way too much time and not getting the result that I want. Here is my code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#include <iostream>
#include <string>
#include <iomanip>
#include "Inventory.h"
using namespace std;
int main ()
{
int NewProducts;
Inventory *stock = nullptr ;
int ItemNum, quantity;
double cost;
cout << "This program was written by: MisterTams ©" << endl;
cout << "____________________________________________" << endl;
cout << "At the end of the daily warehouse operations" << endl;
cout << "enter any new items that need to be added to" << endl;
cout << "our in-stock inventory if they were out-of- " << endl;
cout << "stock as of earlier today or before then....\n" << endl;
cout << "How many new different product types were added" << endl;
cout << "during receiving operations?" << endl;
cout << "\nEnter: " ;
cin >> NewProducts;
stock = new Inventory [NewProducts]; //dynamically allocating an array using a pointer
for (int x = 0; x < NewProducts; x++)
{
cout << "\nNewly Stocked Item #" << x + 1 << ":" << endl;
cout << "Enter Item Stock Number: " ;
cin >> ItemNum;
cout << "Enter Item Quantity: " ;
cin >> quantity;
cout << "Enter Sale Price: $" ;
cin >> cost;
stock[x] = Inventory (ItemNum, quantity, cost);
}
cout << "\nNewly Stock Inventory in Warehouse" << endl;
cout << "___________________________________________________________________" << endl;
cout << "ITEM STOCK #" << setw(16) << "QUANTITY" << setw(17) << "COST" << setw(19) << "TOTAL COST" << endl;
for (int y = 0; y < NewProducts; y++)
{
cout << left << setw(15) << stock[y].getItemNumber();
cout << setw(13) << right << stock[y].getQuantity();
cout << fixed << setprecision(2);
cout << setw(12) << "$" << stock[y].getCost() << " " ;
cout << setw(17) << "$" << stock[y].getTotalCost() << " " ;
cout << endl;
}
delete [] stock;
cout << "\nMemory Address of instance of stock object before nullptr operator: " << stock << endl;
stock = nullptr ;
cout << "Memory Address of instance of stock object after nullptr operator: " << stock << endl; //experimenting how nullptr works
system("pause" );
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include <string>
using namespace std;
#ifndef INVENTORY_H
#define INVENTORY_H
class Inventory
{
private :
int itemNumber, quantity;
double cost, totalCost;
public :
Inventory ()
{
itemNumber = 0, quantity = 0, cost = 0, totalCost = 0;
}
Inventory (int ItemNum, int Quantity, double COST)
{
itemNumber = ItemNum;
quantity = Quantity;
cost = COST;
}
void setItemNumber (int ItemNum)
{ itemNumber = ItemNum; }
void setQuantity (int Quantity)
{ quantity = Quantity; }
void setCost (double COST)
{ cost = COST; }
void setTotalCost ()
{ totalCost = cost * quantity; }
int getItemNumber ()
{ return itemNumber; }
int getQuantity ()
{ return quantity; }
double getCost ()
{ return cost; }
double getTotalCost ()
{ return totalCost = cost * quantity; }
};
#endif
Here is part of the output that needs work. Here is an example.
1 2 3 4 5 6
Newly Stock Inventory in Warehouse
___________________________________________________________________
ITEM STOCK # QUANTITY COST TOTAL COST
3123 34 $15.99 $543.66
245421 25 $92.05 $2301.25
133742069 240 $96.00 $23040.00
How I want it to look similar to this, each and every time...
1 2 3 4 5 6
Newly Stock Inventory in Warehouse
___________________________________________________________________
ITEM STOCK # QUANTITY COST TOTAL COST
3123 34 $15.99 $543.66
245421 25 $92.05 $2301.25
133742069 240 $96.00 $23040.00
Please help me achieve this.
Thank You!
Last edited on Mar 13, 2017 at 7:05am UTC
Mar 13, 2017 at 11:01am UTC
setw(19) << "TOTAL COST"
try increasing this number from 19 to shift "TOTAL COST" header further to the right, or ...
setw(17) << "$" << stock[y].getTotalCost()
try reducing this numbers from 17 to shift stock[y].getTotalCost() number to the left
Topic archived. No new replies allowed.