Displaying a table

This is a discussion on Displaying a table within the C++ Programming forums, part of the General Programming Boards category; This assignment asked me to calculate the salary, and I have done the most of it. However, the last part ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    2

    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";
    		
    		}
    	}
    	
    	
    }
    Last edited by hkguy; 03-29-2004 at 01:00 AM. Reason: removing attachment

  2. #2
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    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.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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()
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Displaying a table of arbitrary values?
    By cbmurdock in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:59 AM
  2. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 06:06 PM
  3. extra word printing
    By kashifk in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2003, 05:03 PM
  4. inputting words from a file
    By kashifk in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2003, 08:18 AM
  5. displaying the contents of an html file in a table
    By confuted in forum Tech Board
    Replies: 3
    Last Post: 08-02-2003, 08:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21