Problems with For loop

This program is a practice program out of the book for my class. The code compiles displays with the proper formatting. The issue I am having is the output. I should get, as an output for salary, $0.01, $0.02, $0.04, $0.08, and so on. Every way I have tried to write this, I get either $0.02, $0.04, $0.06, $0.08, or $0.01, $0.02, $0.03, $0.04, $0.05. I'm not sure if the problem lies in the loop that I am using or if it lies in my math. Thank you in advance for any possible help!


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
50
51
52
53
/* Pennies for Pay
Write a program that calculates how much a person would earn over a period of time if his or her salary 
is one penny the first day and two pennies the second day, and continues to double each day.The program 
 should ask the user for the number of days.Display a table showing how much the salary was for each day, 
and then show the total pay at the end of the period.The output should be displayed in a dollar amount,
not the number of pennies.
Input Validation : Do not accept a number less than 1 for the number of days worked. */

#include "pch.h"
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	// variables
	int days;
	double salary;
	double starting = 0.01;
	double sum = 0;

	// ask the user to input the number of days
	cout << "What is the number of days that you have worked?\n";
	cin >> days;

	// validation 
	if (days < 1)
	{
		cout << "Sorry, you can not work less than 1 day.";
		cout << "Enter the number of days that you have worked.\n";
		cin >> days;
	}

	// display table title with formating 
	cout << setprecision(2) << fixed << endl;
	cout << "Daily Salary\n";

	// loops to find salary
	for (int count = 1; count <= days; count++)
	{
		// calculate the salary
		salary = (starting *= 2) ;
		sum += salary;


		// display the salary
		cout << count << " " << " " << "$" << sum << endl;
	}

	// terminate program
	return 0;
}
Last edited on
Not sure I understand what you mean by the loop has not been declared properly.
Note that the answer is just 2n-1 cents, where 'n' is the number of days. I realize this is likely not the method you're meant to use to find the answer, but you can use this formula for checking your program's output.

The cumulative pay would be .01, .03, .07, .15, etc. The salary for the individual days is .01, .02, .04, .08, etc.

Initialize your daily salary to .01. You don't need the 'starting' variable. At every iteration of the loop add the salary to the sum, then double the salary so that it's correct for the next day.

salary = (starting *= 2) is not doing what you think it's doing. Just salary *= 2 will double the salary.
Thank you, I understood where I was wrong.
While I have you here, I am just now getting into the While, For, and Do While loops. Im wondering if you have any advice on how to better understand them and apply them in programs? They are the only thing I have struggled with so far! Thanks in advance
Let's see if I can explain effectively without being too wordy.

A loop is appropriate to use whenever you want to execute the same or very similar code more than once.

The simplest type of loop is the 'while' loop. The syntax is

1
2
3
4
while (condition)
{
    //do stuff
}


At the start of the loop it checks if 'condition' is true. If it is true the code within the brackets is executed before coming back to check the condition again and starting over. If the condition is false the code in the brackets is skipped and the loop is over.

do while is very similar. The only difference is that the condition is checked at the end of the loop. This ensures that the code within the loop is always executed at least once. If the condition is true then the program goes back to the beginning of the loop.

1
2
3
4
do
{
    //do stuff
} while (condition);


A for loop is a little bit more complex.
for (do this once at the beginning; condition; do this)
The first part of the for loop is executed only once before the loop starts. Before the code in the brackets is executed, the condition is checked. If it's true the code is executed, otherwise the loop breaks. The third part of the loop is executed at the end of every iteration. The loop:
1
2
3
4
for (int i = 0; i < n; i++)
{
    //do stuff
}

...is actually the same as:
1
2
3
4
5
6
7
8
{
    int i = 0;
    while (i < n)
    {
        //do stuff
        i++;
    }
}


The keywords 'break' and 'continue' can respectively be used to break the loop immediately, or to skip the remaining code in the brackets and go back to the beginning of the loop.
Thank you so much
Topic archived. No new replies allowed.