Displaying a table

• 03-29-2004
hkguy
Displaying a table
This assignment asked me to calculate the salary, and I have done the most of it.

However, the last part of the assignment asked me to display a table of the salary of each day, the total pay at the end of the period. I have worked on this for hours, and have no idea why it doesn't show a correct table. And my text book doesn't say a damn thing about displaying the table.

The program only shows the last day.

I am using Visual C++, thanks a lot!

Code:

```#include <iostream.h> #include <math.h> #include <iomanip.h> void main(void) {         float days;         long double total = 0;                 cout << "Please input the number of day for calculation, do NOT enter a value which less than '1'!\n";         cin >> days;         cout << "\n";                 while (days < 1)         {                 cout << "Warning! You little dork please READ the instruction!\n\n";                 cout << "Now, I will give you another chance...enter the number of day again!\n" << endl;                 cin >> days;                 cout << "\n\n";         }                 float penny;         long double pay;         cout << "Please input the pay for the first day (in penny)\n";         cin >> penny;         pay = penny * 0.01;         cout << endl;                 cout << setw(6) << "Day";         cout << setw(25) << "Pay for today";         cout << setw(35) << "Total \$\$\$\$ you have made" << endl;         cout << "---------------------------------------------------------------------\n";                 if (days >= 1)         {                 for (int count= 1 ; count <= days; count++);                 {                                                 long double payperday;                         payperday = pay * (pow (2 , (count - 2)));                         total += payperday;                                                 cout << setw(5)  << days;                         cout << setw(20) << payperday;                         cout << setw(30) << total << "\n\n\n"<< endl;                                                 cout << setprecision(1) << setiosflags(ios::dec | ios::fixed) <<"The total pay is \$" << total << "\n\n";                                 }         }                 }```
• 03-29-2004
lightatdawn
You would benefit from some looping action most definatly in this application. I'll start at the top with a minor suggestion:

Instead of checking for valid days and allowing the user to enter a number again, you should just loop until valid input is given. Like so:

Code:

```do {         cout << "Please input the number of day for calculation, do NOT enter a value which less than '1'!\n";         cin >> days;         cout << "\n"; }while(days < 1);```
As for getting each day displayed you have several problems as well as several solutions to each. For starters, you're going to need to get the info for each day, not just the first one as you do here. In order to do this you're going to need an array of data. You can either set a maximum static value (like 100 or something) and have a static array length, like: long double pay[100]; or you can allocate it dynamically when "days" is defined. It would appear that the first option is more on the level of what you're doing. You'll need to loop like so:

Code:

```float penny; long double pay[100]; for(int i = 0; i < days; i ++) {         cout << "Please input the pay for the first day (in penny)\n";         cin >> penny;         pay[i] = penny * 0.01;         cout << endl; }```
After that you'd make another loop to display what you've got.

Try this out and post here again if you need more help or have further questions.
• 03-29-2004
Prelude
Code:

```#include <iostream.h> #include <math.h> #include <iomanip.h> void main(void)```
Just because the compiler allows you to write bad/non-standard code doesn't mean you should do so wantonly. A quick fix would be:
Code:

```#include <iostream> #include <cmath> #include <iomanip> using namespace std; int main()```