hi, i've got a bit of a roadbump in my work.

I need to set all of the money values to have two decimal points on the end, but my current program only adds two places if there are more than one.
right now, i am multiplying the value by 100 and truncating with an int and dividing by 100.

for example, the number 387.3532 would become 387.35, but the number 440 would stay at 440 (instead of what i want: 440.00)

anyone have any ideas?

here is my current code:
sample inputs: 44, 10, 1, 1, 44, 10, 0, 1, 33, 10, 0.

// This is the main project file for VC++ application project 
// generated using an Application Wizard.

#include "stdafx.h"
#include <math.h>
#using <mscorlib.dll>
  using namespace System;
#include <iostream>
  using namespace std;

int _tmain(){
	cout<<" ************************************************************\n";
	cout<<"** Welcome to the Weekly Payroll Program Thing!             **\n";
	cout<<"**                                                          **\n";
	cout<<"**       Written by Sam Lehman                              **\n";
	cout<<" ************************************************************\n";
	cout<<"\n";

	int empCounter=1, anotherRound, exempt;
	double rate, moneyCounter=0, hours, empMoney, otHours, otPay, scale = pow(10.0, 2);;


	while (true){
		// input and such...
		hours = 0; rate = 0; exempt=-1, otHours=0, otPay=0;
		cout<<" How many hours did this the employee work? \n  ";
		cin>>hours;
		cout<<" What is this employee's hourly rate? \n  ";
		cin>>rate;
		if (hours > 40){
            cout<<" Is this employee exempt? (press 1 for yes or 0 for no)\n  ";
			cin>>exempt;
			if (exempt != 1){
				otHours = hours - 40;
				otPay = otHours * rate * 1.5;
			}else{
				otHours = hours - 40;
				otPay = otHours * rate * 1;
			}
		}

		// calculations and shit..
		empMoney = ((hours - otHours)*rate) + otPay;
		moneyCounter+=empMoney;

		
        rate = (int)(rate * scale) / scale;
        otPay = (int)(otPay * scale) / scale;
        empMoney = (int)(empMoney * scale) / scale;
        

        // output the current employee's info.
		cout<<"\n  Employee Number: "<<empCounter;
		cout<<"\n  Hours: "<<hours;
		cout<<"\n  Rate: $"<<rate;
		if (exempt != -1)	cout<<"\n  Exempt: "<<exempt;
		else				cout<<"\n  Exempt: ";
		
		if (otPay != 0)		cout<<"\n  OT Pay: $"<<otPay;
		else				cout<<"\n  OT Pay: ";
		cout<<"\n  Gross Pay: $"<<empMoney<<"\n";

		//process another?
		cout<<"\n Would you like to calculate another employee? \n    (press 1 for yes or 0 for no)\n  ";
		cin>>anotherRound;
		if (anotherRound == 0){
			moneyCounter = (int)(moneyCounter * scale) / scale;
			cout<<"Total pay for "<<empCounter<<" employees is $"<<moneyCounter<<endl;
			break;
		} else				empCounter++;
	}
	return 0;
}

If it were me, I'd use sprintf() or one of its variants (_snprintf, say).

char display[30];
_snprintf( display, sizeof(display), "%.2f", empMoney );

And forget about scaleing it manually.

Member Avatar for Siersan

Is there something wrong with this?

#include <iomanip>
#include <iostream>

using namespace std;

int main()
{
  double a = 440;
  double b = 387.3532;

  cout.setf(ios::fixed);
  cout<< setprecision(2) << a <<'\n';
  cout<< setprecision(2) << b <<endl;
}
commented: gave very clean and clear solution +1

Is there something wrong with this?

#include <iomanip>
#include <iostream>

using namespace std;

int main()
{
  double a = 440;
  double b = 387.3532;

  cout.setf(ios::fixed);
  cout<< setprecision(2) << a <<'\n';
  cout<< setprecision(2) << b <<endl;
}

thanks a lot, that works great.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.