Hello all,
I am trying to convert my weekly gross pay to a monthly pay in C++. I have everything in place but I need the code to put in to make week 4 turn to monthly pay. This is my code:
#include <iostream>
#include <iomanip>
constdouble over = 1.5;
int main()
{
usingnamespace std;
double hours = 0, hwage = 0, gpay = 0, mpay = 0, total = 0;
int week = 0;
char repeat = 'q';
cout << "CalcPay" << endl;
do {
week++;
cout << "Week " << week << endl;
cout << "Please enter the number of hours worked in a week or q to quit: ";
cin >> hours;
cin >> repeat;
cout << "Please enter the hourly wage: ";
cin >> hwage;
if (hours <= 40)
gpay = hours * hwage;
else
gpay = (40 * hwage) + ((hours - 40) * hwage * over);
cout << fixed;
cout << std::setprecision(2); // two numbers after the decimal point
cout << "Your weekly gross pay is: " << gpay << "\n";
mpay = gpay * 4, week % 4 == 0;
cout << "Your total monthly pay is: " << week << endl;
total += gpay;
cout << "Your total pay for the year so far is: " << total << endl;
}
while(week <= 52);
return 0;
}
As you can see my code is a little lack luster but it will get the job done. Where I am having trouble is, I don't want the mpay to be there i just want to take out the whole line
mpay = gpay * 4, week % 4 == 0;
cout << "Your total monthly pay is: " << week << endl;
and just have it show up in week 4. I know I'm on the right track but I need a little bit of help with the code. Any help would be greatly appreciated. Thank you and have a great night.
#include <iostream>
#include <iomanip>
int main() {
constdouble over = 1.5;
std::cout << "CalcPay" << '\n';
double hwage = 0, mpay = 0, total = 0;
// may be do this just once instead of once every week? (if not move it inside the loop)
std::cout << "Please enter the hourly wage: ";
std::cin >> hwage;
// we need to do this only once
std::cout << std::fixed << std::setprecision(2); // two numbers after the decimal point
for( int week = 1 ; week < 53 ; ++week ) { // for each week from 1 to 52
std::cout << "Week " << week << '\n';
std::cout << "Please enter the number of hours worked in a week or q to quit: ";
// read in the next character
char c ;
std::cin >> c ;
if( c == 'q' || c == 'Q' ) break ; // we are done if the user entered 'q'
else std::cin.putback(c) ; // otherwise, put the extracted character back into the stream
double hours ;
std::cin >> hours ;
double gpay = hours * hwage;
// add overtime if applicable
if ( hours > 40 ) gpay += (hours-40) * hwage * (over-1) ;
std::cout << "Your weekly gross pay is: " << gpay << "\n";
if( week%4 == 0 ) { // end of the month
std::cout << "Your total monthly pay is: " << mpay << '\n';
total += mpay ;
std::cout << "Your total pay for the year so far is: " << total << '\n';
mpay = 0 ;
}
else mpay += gpay ;
}
}
> Puts the character ch back to the input stream so the next extracted character will be ch.
Puts a character back to the input stream so the next extracted character will be that one.
For an input-only stream (like std::cin), the character that is put back must be the last one that was extracted. http://en.cppreference.com/w/cpp/io/basic_istream/putback