2

◾Use output statements to display input data and calculation results; display decimal values formatted to two decimal places. This is where I am having difficulties! Everything else has been done for this particular project so for example when u use 4.4444 for pay rate and 10 for hours it should print out that regular pay is 44.44! not 44.444!

{
// declare variables
int Id = 0;
double hours = 0.00;
double payrate = 0.00;
char ucode = 'A';
double rpay = 0.00;
double opay = 0.00;
double gpay = 0.00;
double ftax = 0.00;
double npay = 0.00;

cout <<"\nWelcome Please Enter Employee Info";

//Input Data from keyboard
cout << "\nEnter Employee's ID ";
cin >> Id;
cout << "\nEnter Employee's Hours worked ";
cin >> hours;
cout << "\nEnter Employee's Pay Rate $";
cin >> payrate;
cout << "\nEnter Employee's union code ";
cin >> ucode;
cout << "\nEmployee's Id " << Id << "\nEmployee's Hours " << hours << "\nEmployee's Payrate " << payrate << "\nEmployee's Union Code " << ucode;
//Calculate regular pay and/or overtime pay. 
if ( hours <= 40 ){
  rpay = hours * payrate;
  gpay = rpay;
  cout<< "\nGross pay is $" << gpay;}

if ( hours > 40 ){
   rpay = 40 * payrate;
   opay = payrate * (1.5 *(hours-40));
   gpay = rpay + opay;
   cout<< "\nGross pay is $" << gpay;}
//Calculate the Gross Pay.   
if ( gpay <= 1000 ) {
   ftax = .10 * gpay;
   cout << "\nThe Federal tax due is $" << ftax;
   }

 else if ( gpay > 1000 && gpay <= 2000){
   ftax = .15 * gpay;
   cout << "\nThe Federal tax due is $" << ftax; 
     }

  else if ( gpay > 2000){
   ftax = .25 * gpay;
   cout << "\nThe Federal tax due is $" << ftax;
     }
//Calulating Netpay.     
switch ( ucode )
{
       case 'A' : cout << "\nEmployee owes $25 to Union ";
                  npay = (gpay - (ftax + 25));
                  cout << "\nEmployee's Net Pay is $" << npay;
                  break;

       case 'a' : cout << "\nEmployee owes $25 to Union ";
                  npay = (gpay - (ftax + 25));
                  cout << "\nEmployee's Net Pay is $" << npay;
                  break;

       case 'B' : cout << "\nEmployee owes $50 to Union ";
                  npay = (gpay - (ftax + 50));
                  cout << "\nEmployee's Net Pay is $" << npay;
                  break;

       case 'b' : cout << "\nEmployee owes $50 to Union ";
                  npay = (gpay - (ftax + 50));
                  cout << "\nEmployee's Net Pay is $" << npay;
                  break;

       case 'C' : cout << "\nEmployee owes $75 to Union ";
                  npay = (gpay - (ftax + 75));
                  cout << "\nEmployee's Net Pay is $" << npay;
                  break;

       case 'c' : cout << "\nEmployee owes $75 to Union ";
                  npay = (gpay - (ftax + 75));
                  cout << "\nEmployee's Net Pay is $" << npay;
                  break;          
       default:
                  cout << "\nThere is no such Union Code ";
                  npay = (gpay - (ftax + 0));
                  cout << "\nEmployee's Net Pay is $ " << npay;
                  }
       cout << "\nThank You for using our software, Please Enjoy the rest of your Day! \n ";
Gabriel Ryan
  • 23
  • 1
  • 2
  • 6
  • take a look at http://stackoverflow.com/questions/4217510/how-to-cout-the-correct-number-of-decimal-places-of-a-double-value – Karthik T Sep 02 '13 at 01:26
  • I have seen that and it does not help me when I use still get an error message! – Gabriel Ryan Sep 02 '13 at 01:38
  • In that case could you tell us what message? It is pretty useless to use to say you get an error, and is prefered generally that you give as much information as possible – Karthik T Sep 02 '13 at 01:39
  • yes the error is that setprecision must be used first or the other error that setprecision must be declared first! – Gabriel Ryan Sep 02 '13 at 01:40
  • Furthermore where would I place cout << setprecision or std::setprecision so that I don't get those errors – Gabriel Ryan Sep 02 '13 at 01:42
  • what about using %.2f? – Gabriel Ryan Sep 02 '13 at 01:43
  • okay I got that part to work thank u but a question is there anyway to get rid of the notation of e to power of +002 when I do some weird work or not – Gabriel Ryan Sep 02 '13 at 02:01
  • not sure what you mean.. you can look at `printf` and the `%.2f` flag as earlier suggested.. it is a equivalent solution in the C world – Karthik T Sep 02 '13 at 02:03
  • well when I enter say 10 for pay rate and 40 for hours I now get 4.e+002 or some nonse – Gabriel Ryan Sep 02 '13 at 02:05
  • Are you sure you used it as in the example in the link given in the answer? – Karthik T Sep 02 '13 at 02:07
  • yes but it does not help me when the user is suppose to enter the info and when I have much more going on that I don't even know where to add that statement to get a correct answer? – Gabriel Ryan Sep 02 '13 at 02:09
  • am I suppose to add that statement below each variable in declaration mode or after the fact of the user entering the number? – Gabriel Ryan Sep 02 '13 at 02:11

1 Answers1

6

std::setprecision and std::fixed should do the trick.

Sounds like you need something like:

 std::cout << std::fixed << std::setprecision(2);
Drew MacInnis
  • 8,267
  • 1
  • 22
  • 18