Loop error

Hello I am having difficulty with my loop. I am very new 5 weeks into class. This program asks a users amount of days worked, and doubles the pay starting with cents. It displays a chart. The chart displays, and I initialized the variables, they just dont go into the loop and display the sum is 0. Hints would be appreciated with my error/errors.

//This program calculates the days worked and doubles the pay by .20 cents
// then displays the total for all days.
#include <iostream>
#include <iomanip>
using namespace std;

int main()

{
int daysworked; //Loop counter variable
int day; //Day variable
double pay; //Pay to display
double sum; //Totals

//Get the amount of days worked
cout << "How many days did you work?";
cin >> daysworked;

//Display the table
cout << "\nDay Pay\n";
cout << "====================\n";

day = 1;
pay = 0.1;
sum = 0.00;

//Display the Day and Pay

for (day=1; day>daysworked; day++)

{

//Calculate
pay = pay + pay;
//Calcuate totals
sum = sum + pay;
cout << day << "\t\t" << endl;
}
//Display the total of all pay
cout << setprecision(3);
cout << "Your total pay is $"<<+sum << endl;
system("pause");
return 0;
}
Hello, there is indeed a problem with your for loop.
for (day=1; day>daysworked; day++)

Hint: You only enter/stay inside the loop only IF the condition of the loop is not met.
In other words, your code right now always skips the for loop if daysworked>1.
Last edited on
Surrounding the initial codes in tags for better readability for all who reads this thread.
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
//This program calculates the days worked and doubles the pay by .20 cents
// then displays the total for all days.
#include <iostream>
#include <iomanip>
using namespace std;

int main()

{
    int daysworked;	//Loop counter variable
    int day;	//Day variable
    double pay;	//Pay to display
    double sum;	//Totals

    //Get the amount of days worked
    cout << "How many days did you work?";
    cin >> daysworked;

    //Display the table
    cout << "\nDay Pay\n";
    cout << "====================\n";

    day = 1;
    pay = 0.1;
    sum = 0.00;

    //Display the Day and Pay

    for (day=1; day>daysworked; day++)
    {

        //Calculate
        pay = pay + pay;
        //Calcuate totals
        sum = sum + pay;
        cout << day << "\t\t" << endl;
    }
    
    //Display the total of all pay
    cout << setprecision(3);
    cout << "Your total pay is $"<<+sum << endl;
    system("pause");
    return 0;
}
Topic archived. No new replies allowed.