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
#include <iostream> #include <iomanip> using namespace std; void main () { int qProd1,qProd2,qProd3,qProd4,qProd5; double priceProd1,priceProd2,priceProd3,priceProd4,priceProd5; cout << "\tThis program creates a nicely formatted table" << endl; cout << "\t\t Sample solution by" << endl; cout << "\t \n\n" << endl; cout << "Quanity of product #1: "; cin >> qProd1; cout << "Price of product #1: "; cin >> priceProd1; cout << "\nQuanity of product #2: "; cin >> qProd2; cout << "Price of product #2: "; cin >> priceProd2; cout << "\nQuanity of product #3: "; cin >> qProd3; cout << "Price of product #3: "; cin >> priceProd3; cout << "\nQuanity of product #4: "; cin >> qProd4; cout << "Price of product #4: "; cin >> priceProd4; cout << "\nQuanity of product #5: "; cin >> qProd5; cout << "Price of product #5: "; cin >> priceProd5; cout.setf(ios::fixed); cout << "\n\n\n\t" << "Product#" << setw(14) << "Quantity" << setw(14) << "Price" << setw(14) << "Total" << endl; cout << "\t=====================================================" << endl; cout << "\t" << setw(4) << "1" << setw(15) << qProd1 << setw(9) << "$" << right << setw(8) << showpoint << setprecision(2) << priceProd1 << setw(7) << "$" << right << setw(10) << qProd1*priceProd1 << endl; cout << "\t" << setw(4) << "2" << setw(15) << qProd2 << setw(9) << "$" << right << setw(8) << showpoint << setprecision(2) << priceProd2 << setw(7) << "$" << right << setw(10) << qProd2*priceProd2 << endl; cout << "\t" << setw(4) << "3" << setw(15) << qProd3 << setw(9) << "$" << right << setw(8) << showpoint << setprecision(2) << priceProd3 << setw(7) << "$" << right << setw(10) << qProd3*priceProd3 << endl; cout << "\t" << setw(4) << "4" << setw(15) << qProd4 << setw(9) << "$" << right << setw(8) << showpoint << setprecision(2) << priceProd4 << setw(7) << "$" << right << setw(10) << qProd4*priceProd4 << endl; cout << "\t" << setw(4) << "5" << setw(15) << qProd5 << setw(9) << "$" << right << setw(8) << showpoint << setprecision(2) << priceProd5 << setw(7) << "$" << right << setw(10) << qProd5*priceProd5 << endl; cout << "\t" << setw(6) << "Total" << setw(13) << qProd1 + qProd2 + qProd3 + qProd4 + qProd5 << setw(9) << "$" << right << showpoint<< setprecision(2) << setw(8) << (priceProd1 + priceProd2 + priceProd3 + priceProd4 + priceProd5)/5 << setw(7) << "$" << right << setw(10) << (qProd1*priceProd1) + (qProd2*priceProd2) + (qProd3*priceProd3) + (qProd4*priceProd4) + (qProd5*priceProd5) << endl; cout << "\n\n\nPlease hit ENTER to terminate the program."; cin.ignore(); cin.ignore(); }
Refactorings
No refactoring yet !
Patch
September 24, 2008, September 24, 2008 01:39, permalink
Let loops do the hard work for you! Makes it easier to read and modify, certainly.
There's really no getting around how nasty those couts look. I'm not sure if there's a prettier way to do it, I've never used all that formatting stuff in C++ (only in C with printf). Turning some of them into slightly generalized functions would certainly help readability (maybe just make something that generates the entire table given the two arrays qProd and priceProd along with the number of elements, or something along those lines), but it's hard[ish] to do that (except for the aforementioned) in their current state with all the variability between lines.
Another option could be to #define a few macros that does all that gibberish for formatting. Depends on how evangelical you are about macros though. :X
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
#include <iostream> #include <iomanip> #define NUM 5 using namespace std; void main() { int qProd[NUM], qSum = 0; double priceProd[NUM], pSum = 0, product = 0; cout << "\tThis program creates a nicely formatted table" << endl; cout << "\t\t Sample solution by" << endl; cout << "\t \n\n" << endl; for(int i = 0; i < NUM; ++i) { cout << "Quantity of product #" << i+1 << ": "; cin >> qProd[i]; cout << "Price of product #" << i+1 << ": "; cin >> priceProd[i]; } cout.setf(ios::fixed); cout << "\n\n\n\t" << "Product#" << setw(14) << "Quantity" << setw(14) << "Price" << setw(14) << "Total" << endl << "\t=====================================================" << endl; for(int i = 0; i < NUM; ++i) { cout << "\t" << setw(4) << i+1 << setw(15) << qProd[i] << setw(9) << "$" << right << setw(8) << showpoint << setprecision(2) << priceProd[i] << setw(7) << "$" << right << setw(10) << qProd[i] * priceProd[i] << endl; qSum += qProd[i]; pSum += priceProd[i]; product += qProd[i] * priceProd[i]; } cout << "\t" << setw(6) << "Total" << setw(13) << qSum << setw(9) << "$" << right << showpoint << setprecision(2) << setw(8) << (pSum)/5 << setw(7) << "$" << right << setw(10) << product << endl; cout << "\n\n\nPlease hit ENTER to terminate the program."; cin.ignore(); cin.ignore(); }
Patch
September 24, 2008, September 24, 2008 01:46, permalink
I forgot to add the change in, but I'm reasonably sure that the ANSI standard doesn't allow the use of "void" as a return type for main (must be int). Although you can actually still omit the return at the end of the program.
kewl mcgregor
September 30, 2008, September 30, 2008 23:31, permalink
* Don't use #define when you can avoid it!
* void main is wrong wrong wrong. It is alway int main!
* declare variables as late as possible!
* use std::size_t and not int when it is about size or array indices!
* use unsigned when a value can't be <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
#include <iostream> #include <iomanip> enum { NUM = 5 }; using namespace std; int main() { cout << "\tThis program creates a nicely formatted table" << endl; cout << "\t\t Sample solution by" << endl; cout << "\t \n\n" << endl; unsigned qProd[NUM]; double priceProd[NUM]; for(std::size_t i = 0; i < NUM; ++i) { cout << "Quantity of product #" << i+1 << ": "; cin >> qProd[i]; cout << "Price of product #" << i+1 << ": "; cin >> priceProd[i]; } cout.setf(ios::fixed); cout << "\n\n\n\t" << "Product#" << setw(14) << "Quantity" << setw(14) << "Price" << setw(14) << "Total" << endl << "\t=====================================================" << endl; unsigned qSum = 0; double pSum = 0.0; double product = 0.0; for(std::size_t i = 0; i < NUM; ++i) { cout << "\t" << setw(4) << i+1 << setw(15) << qProd[i] << setw(9) << "$" << right << setw(8) << showpoint << setprecision(2) << priceProd[i] << setw(7) << "$" << right << setw(10) << qProd[i] * priceProd[i] << endl; qSum += qProd[i]; pSum += priceProd[i]; product += qProd[i] * priceProd[i]; } cout << "\t" << setw(6) << "Total" << setw(13) << qSum << setw(9) << "$" << right << showpoint << setprecision(2) << setw(8) << pSum / 5 << setw(7) << "$" << right << setw(10) << product << endl; cout << "\n\n\nPlease hit ENTER to terminate the program."; cin.ignore(); cin.ignore(); }
Clueless
October 1, 2008, October 01, 2008 00:02, permalink
Using the whole std namespace seems a bit self destructive for further use.
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
#include <iostream> #include <iomanip> enum { NUM = 5 }; using std::cout; using std::cin; using std::setw; int main() { cout << "\tThis program creates a nicely formatted table" << endl; cout << "\t\t Sample solution by" << endl; cout << "\t \n\n" << endl; unsigned qProd[NUM]; double priceProd[NUM]; for(std::size_t i = 0; i < NUM; ++i) { cout << "Quantity of product #" << i+1 << ": "; cin >> qProd[i]; cout << "Price of product #" << i+1 << ": "; cin >> priceProd[i]; } cout.setf(std::ios::fixed); cout << "\n\n\n\t" << "Product#" << setw(14) << "Quantity" << setw(14) << "Price" << setw(14) << "Total" << endl << "\t=====================================================" << endl; unsigned qSum = 0; double pSum = 0.0; double product = 0.0; for(std::size_t i = 0; i < NUM; ++i) { cout << "\t" << setw(4) << i+1 << setw(15) << qProd[i] << setw(9) << "$" << right << setw(8) << std::showpoint << std::setprecision(2) << priceProd[i] << setw(7) << "$" << right << setw(10) << qProd[i] * priceProd[i] << endl; qSum += qProd[i]; pSum += priceProd[i]; product += qProd[i] * priceProd[i]; } cout << "\t" << setw(6) << "Total" << setw(13) << qSum << setw(9) << "$" << right << std::showpoint << std::setprecision(2) << setw(8) << pSum / 5 << setw(7) << "$" << right << setw(10) << product << endl; cout << "\n\n\nPlease hit ENTER to terminate the program."; cin.ignore(); cin.ignore(); }
Is there a nicer way of formatting the following code?