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.
//This program calculates the days worked and doubles the pay by .20 cents
// then displays the total for all days.
#include <iostream>
#include <iomanip>
usingnamespace 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;
}