Warning

 

Close
Confirm Action

Are you sure you wish to do this?

Cancel Confirm
AR15.COM
2/24/2005 6:22:52 PM EDT
I'm trying to write a program that shows the salary a person would make, if on the first day he made $.01 (one cent), second day - $.02, third day $.04, and doubled every day thereafter. The user will input the number of days to be calculated. The program will then display in a table form the pay for every day, and then show the total of all the days work. Output should look like:

Please enter the number of days worked: 6

Day #   Pay
Day 1   $0.01
Day 2   $0.02
Day 3   $0.04
Day 4   $0.08
Day 5   $0.16
Day 6   $0.32
------------------------
Total:  $x.xx

END of PROGRAM


The problem I'm having is figuring out how to keep a running total within the while loop. Here's my code so far:

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
  int days=0, count=1;
  float pay=.01, total=0.0;
 
  //Ask for input.
  cout << "\nPlease enter the number of days worked: ";
  cin >> days;
 
  //Validate input.
  while (days <= 0)
  {
     cout << "\nPlease enter the number of days worked.\n";
     cout << "Value must be an integer greater than 0: ";
     cin >> days;
  }

  cout << endl;
  cout << "Day #" << "   Pay" << endl;  
 
  //Calculate daily pay.
  while (count <= days)
    {
       cout << "Day " << count << "\t$"
       << setprecision(2) << fixed
       << showpoint << pay << endl;
       pay = pay * 2.0;        
       count++;
    }
 
  cout << "------------------------\n";
  cout << "Total:  $" << total;  
 
  cout << "\n\nEND of PROGRAM\n\n";
  return 0;
}
2/24/2005 6:31:38 PM EDT
[#1]
I don't see where your total variable is updated.    Try  something like this :



#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
  int days=0, count=1;
  float pay=.01, total=0.0;
 
  //Ask for input.
  cout << "\nPlease enter the number of days worked: ";
  cin >> days;
 
  //Validate input.
  while (days <= 0)
  {
     cout << "\nPlease enter the number of days worked.\n";
     cout << "Value must be an integer greater than 0: ";
     cin >> days;
  }

  cout << endl;
  cout << "Day #" << "   Pay" << endl;  
 
  //Calculate daily pay.
  while (count <= days)
    {
       cout << "Day " << count << "\t$"
       << setprecision(2) << fixed
       << showpoint << pay << endl;
       total += pay;
       pay = pay * 2.0;        
       count++;
    }
 
  cout << "------------------------\n";
  cout << "Total:  $" << total;  
 
  cout << "\n\nEND of PROGRAM\n\n";
  return 0;
}




Good luck!
2/24/2005 6:39:57 PM EDT
[#2]

Quoted:
I don't see where your total variable is updated.    Try  something like this :



total += pay;




Good luck!


Got it thanks.
2/24/2005 6:42:01 PM EDT
[#3]
You know, in the real world no one uses iostreams.
2/24/2005 6:45:06 PM EDT
[#4]
After a month he gets a lot.
2/24/2005 7:17:07 PM EDT
[#5]
You know you could save a lot of cycles (not that it's critical in this app) by using bitshifting instead of multiplying by two.


pay = pay << 1;
total += pay;



ETA: that may not work with floats .. my bad
2/24/2005 7:30:17 PM EDT
[#6]
Its just an intro C++ class I'm taking. We haven't learned bitshifting yet.
2/24/2005 7:31:03 PM EDT
[#7]
try this:


while(1)
{
fork();
}


I dare you.