EDN Admin
Well-known member
Working a homework problem and I realize it is pretty basic code. I am using a WHILE loop to calculate pay. The rough code I have written works but day 1 should be 0.01 then it doubles after that. My code outputs at 0.02. here is my code:
<pre lang="x-cpp # include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main ()
{
int count=0,
days;
double pennies=0.01,
total=0.0;
cout<<"This program will calculate your pay if you are paidn";
cout<<"ta penny and it doubles after each day.nn";
cout<<"Please enter the number of days that you worked to get your wages: ";
cin>>days;
while (days <= 0) //input validation
{
cout<<"Number can not be less than 1.nn";
cout<<"Try again but this time enter a nummber larger then zero: ";
cin>>days;
}
cout<<setw(5)<<"Days"<<"tt"<<"salary"<<endl;
cout<<setw(5)<<"-----------------------nn";
while (count <days)
{
pennies*=2;
total+=pennies;
++count;
cout<<setw(5)<<count<<"tt"<<pennies<<endl;
}
cout<<setprecision(2)<<fixed<<endl;
cout<<"Here is your total salary $ "<<total<<endl;
return 0;
}
[/code]
View the full article
<pre lang="x-cpp # include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main ()
{
int count=0,
days;
double pennies=0.01,
total=0.0;
cout<<"This program will calculate your pay if you are paidn";
cout<<"ta penny and it doubles after each day.nn";
cout<<"Please enter the number of days that you worked to get your wages: ";
cin>>days;
while (days <= 0) //input validation
{
cout<<"Number can not be less than 1.nn";
cout<<"Try again but this time enter a nummber larger then zero: ";
cin>>days;
}
cout<<setw(5)<<"Days"<<"tt"<<"salary"<<endl;
cout<<setw(5)<<"-----------------------nn";
while (count <days)
{
pennies*=2;
total+=pennies;
++count;
cout<<setw(5)<<count<<"tt"<<pennies<<endl;
}
cout<<setprecision(2)<<fixed<<endl;
cout<<"Here is your total salary $ "<<total<<endl;
return 0;
}
[/code]
View the full article