Thank you very much. My program works fine and I understand what the problem was. Thank you also for introducing me to the ignore function.
Just one last question:
I now have three "tables". Problem is, they look like a mess. How do I line them up into neat columns, so all the state names are in one line, as well as the other pieces of information.
I tried using setw but it does not seem to work. For example, I used setw(5) for the state names but that just moved all the states over to the right. It didn't line them up.
Below is my complete 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
//delcaring the file
ifstream customers;
class cust{
public:
string first, last, state;
double sales[3];
int units, totalSales;
};
// Prototype Functions
void printcust(cust customer[], int aSize);
void sortname(cust customer[], int aSize);
void sortsales(cust customer[], int aSize);
int main()
{
// opening and checking the file
/*************************************/
customers.open("customers.txt");
if(!customers.is_open())
{
cout << "FILE NOT OPENED";
return 0;
}
/*************************************/
cout.setf(ios::fixed, ios::floatfield);
// declaring object
cust customer[10];
string first, last, state;
double sales[3];
int units, totalSales;
for(int i=0; i<5; i++) // reads 5 lines from file
{
// reads string text using tab deliminator
getline(customers, customer[i].first, '\t');
getline(customers, customer[i].last, '\t');
getline(customers, customer[i].state, '\t');
// reads int text from file
customers >> customer[i].sales[0] >> customer[i].sales[1] >> customer[i].sales[2] >> customer[i].units;
customers.ignore(); // discard newline character left behind by extraction operator
customer[i].totalSales = 0; // set total sales to 0 before adding to it
for(int n=0; n<3; n++) // finds sum of all three years of sales
{
customer[i].totalSales+=customer[i].sales[n];
}
}
cout << "ORIGINAL DATA:" << endl << endl;
printcust(customer, 5);
cout << "SORTED ALPHABETICALLY BY FIRST NAME:" << endl << endl;
sortname(customer, 5);
printcust(customer, 5);
cout << "SORTED IN DESCENDING ORDER BY TOTAL SALES" << endl << endl;
sortsales(customer, 5);
printcust(customer, 5);
cin.get();
}
// Prints object array
void printcust(cust customer[], int aSize) // prints all data
{
cout << "First\tLast\tStates\tSales-1\tSales-2\tSales-3\tUnits\tTotal Sales" << "\n\n";
for(int i=0; i<aSize; i++)
{
cout << customer[i].first << "\t" << customer[i].last << "\t" << setw(5) << customer[i].state << "\t"
<< setprecision(0) << setw(5) << customer[i].sales[0] << " " << setprecision(0) << setw(6) << customer[i].sales[1]
<< " " << setprecision(0) << setw(7) << customer[i].sales[2] << "\t" << customer[i].units
<< "\t" << customer[i].totalSales << endl;
}
cout << "\n\n\n";
return;
}
// Bubble sorts alphabetically by first name
void sortname(cust customer[], int aSize)
{
cust temp;
for(int pass=1; pass<aSize; pass++)
for(int i=0; i<aSize-1; i++)
if(customer[i].first > customer[i+1].first)
{
temp = customer[i];
customer[i] = customer[i+1];
customer[i+1] = temp;
}
return;
}
// Bubble sorts by descending order of sales
void sortsales(cust customer[], int aSize)
{
cust temp;
for(int pass=1; pass<aSize; pass++)
for(int i=0; i<aSize-1; i++)
if(customer[i].totalSales < customer[i+1].totalSales)
{
temp = customer[i];
customer[i] = customer[i+1];
customer[i+1] = temp;
}
return;
}
|
And here is my output:
ORIGINAL DATA:
First Last States Sales-1 Sales-2 Sales-3 Units Total Sales
Barack Obama DC 100 50 300 2000 450
Bill De Blasio NY 250 100 80 5000 430
Kirsten Gillibrand NY 50 100 200 1000 350
Chris Chrystie CT 100 200 300 5000 600
Loretta Lynch DC 50 80 200 1000 330
SORTED ALPHABETICALLY BY FIRST NAME:
First Last States Sales-1 Sales-2 Sales-3 Units Total Sales
Barack Obama DC 100 50 300 2000 450
Bill De Blasio NY 250 100 80 5000 430
Chris Chrystie CT 100 200 300 5000 600
Kirsten Gillibrand NY 50 100 200 1000 350
Loretta Lynch DC 50 80 200 1000 330
SORTED IN DESCENDING ORDER BY TOTAL SALES
First Last States Sales-1 Sales-2 Sales-3 Units Total Sales
Chris Chrystie CT 100 200 300 5000 600
Barack Obama DC 100 50 300 2000 450
Bill De Blasio NY 250 100 80 5000 430
Kirsten Gillibrand NY 50 100 200 1000 350
Loretta Lynch DC 50 80 200 1000 330
Its not shown here cause the formatting doesn't get copied, but I think you get the idea.
Some states are lines up, others are not. Some sales are lined up, other are not. Sames with untis and Total Sales. Setw moves that entire set of data to the right, instead of lining up each individual piece of info. How do I get around this and line up my tables and make them look neat and easy to read?