C++ code

Write a program that calculates the amount a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should display a table showing the salary 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.

Need help with the code

Do you have an algorithm in mind? How would you solve the problem on paper?
#include <iostream>
#include <iomanip>

using namespace std;
int numDays = 1;

int main ()
{
int numDays;
double money, total, daypay;

int numDays=1;
const double money=1.0;
const double total=0.0;
const double daypay=0.0;

cout << "How many days did you work? ";
cin >> numDays;

while (numDays<1)
{
cout << "Please enter the number of days you worked \n";
cin >> numDays;
}

for (int i = 1; 1 <= numDays; i++)

daypay = money/100;
cout << " Day " << 1 << " You earned $" << daypay << "\n";
total += daypay;
money += 2;
}
cout << "total earning are $" << total << endl;




return 0;
}


the cout parts are not working
Your for loop is an infinite loop and your braces aren't balanced. This code doesn't even compile.

First, use good indentation so that it is easy to see where your braces should line up:
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
#include <iostream>
#include <iomanip>

using namespace std;
int numDays = 1;

int main ()
{
    int numDays;
    double money, total, daypay;

    int numDays=1;
    const double money=1.0;
    const double total=0.0;
    const double daypay=0.0;

    cout << "How many days did you work? ";
    cin >> numDays;

    while (numDays<1)
    {
        cout << "Please enter the number of days you worked \n";
        cin >> numDays;
    }

    for (int i = 1; 1 <= numDays; i++)
    {  // This brace was missing before
        daypay = money/100;
        cout << " Day " << 1 << " You earned $" << daypay << "\n";
        total += daypay;
        money += 2;
    }
    cout << "total earning are $" << total << endl;

    return 0;
}

Problems:
Line 12 shadows the variable declared on line 5. Remove line 5. Prefer to keep variables as local as possible.

Lines 13,14,15: Don't declare these variables as const. You're going to be changing their values.

Line 26: You've created an infinite loop because your loop condition is incorrect. The condition in your code is
1 <= numDays; This will never be true. You mean to say i instead of 1.
Last edited on
Topic archived. No new replies allowed.