SOLVED: Problems with incrementing

Ask for help with your homework/assignments in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

SOLVED: Problems with incrementing

Postby thisguy » Mon Mar 16, 2009 10:14 am

I have a homework assignment that reads as follows:

Pennies for Pay:
Write a program that calculates how much a person earns in a month if the salary is one penny the first day, two pennies the second day, four pennies the third day, and so on with the daily pay doubling each day the employee works. The program should ask the user for the number of days the employee worked during the month and should display a table showing how much the salary was for each day worked, as well as the total pay earned for the month. The output should be displayed in dollars with two decimal points, not in pennies.

The program I have written is below:

Code: Select all
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
   int numDays;
   double total = 0.0;

   cout << "How many days have you worked this month? ";
   cin >> numDays;
   cout << "Day         " << "Pay For That Day\n";
   cout << "-------------------------------------\n\n";


   for (int day = 1; day <= numDays; day++)
   {
      
      double daysPay = .01;
      
         
      total += daysPay;
      daysPay = daysPay * 2;
      
      cout << fixed << showpoint << setprecision(2);      
      cout << day << "         $" << daysPay << endl;
   }
   cout << fixed << showpoint << setprecision(2);
   cout << "TOTAL         $" << total << endl;
   
   return 0;
}




No matter what number of days I enter (between 1 and 31) when asked how many days worked this month, every day is displayed with a pay of .02 and the total always comes out as the number I entered divided by 100. Any help with this is much appreciated. It's driving me insane.
Last edited by thisguy on Mon Mar 16, 2009 12:24 pm, edited 1 time in total.
thisguy
 
Posts: 43
Joined: Mon Feb 23, 2009 3:31 pm

Re: Problems with incrementing

Postby Alvaro » Mon Mar 16, 2009 11:39 am

The problem is that you defined daysPay inside the loop, so in every iteration it is set to 0.01 again. You should simply place the line `double daysPay = .01;' before the loop starts. Well, that fixes most of the problems you are having. Then you have to fix the fact that you are displaying daysPay after you multiply it by 2, which is wrong. But again, you can easily fix that by moving the line `daysPay = daysPay * 2;' to the end of the loop.
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Re: Problems with incrementing

Postby thisguy » Mon Mar 16, 2009 12:23 pm

Thank you so much. Once again, stupid mistakes. I looked over that so many times and just didn't catch either of those mistakes. I really appreciate the help.
thisguy
 
Posts: 43
Joined: Mon Feb 23, 2009 3:31 pm

Re: SOLVED: Problems with incrementing

Postby ventsyv » Tue Mar 17, 2009 10:16 am

Here is my attempt ;-) I haven't tested it but I think it will work.
Code: Select all
double CalcSalary(int d)
{d > 0 ? return  (((double)(pow(2,d-1)%100))/100) + pow(2, d-1)/100) : return 0;}
User avatar
ventsyv
 
Posts: 2810
Joined: Mon Sep 22, 2003 5:25 pm
Location: MD USA

Re: SOLVED: Problems with incrementing

Postby Alvaro » Tue Mar 17, 2009 11:05 am

ventsyv wrote:Here is my attempt ;-) I haven't tested it but I think it will work.
Code: Select all
double CalcSalary(int d)
{d > 0 ? return  (((double)(pow(2,d-1)%100))/100) + pow(2, d-1)/100) : return 0;}


Man, that was awful. It doesn't compile for at least three different reasons, and after fixing them, you get a value that is incorrect anyway.

Code: Select all
double CalcSalary(int d) {
  return ((1u<<d)-1)/100.0;
}
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA


Return to Homeworks

Who is online

Users browsing this forum: No registered users and 1 guest