I have this code to execute a payroll program and everytime i put it into dec c+
ID: 3681029 • Letter: I
Question
I have this code to execute a payroll program and everytime i put it into dec c++ or codeblocks the out put looks like this:
please help me figure out whats wrong here
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
//calculating pay
double calcpay(double rate, int hours){
double totalPay = 0;
if(hours>40){ //if overtimr duty dine
totalPay = 40 * rate;
totalPay = totalPay + (hours-40)*rate*1.5;
}
else{ //if not over time. just normal pay
totalPay = hours * rate;
}
return totalPay;
}
int main()
{
std::ifstream readfile("employees.txt");//reding employees data from text file
std::string eachline;
int count=0;
double payRoll = 0;
while (std::getline(readfile, eachline))
{
std::istringstream iss(eachline);
string name;
double rate;
int hours;
while (iss >> name >> rate >> hours)//reading line data from file
{
double pay = calcpay(rate,hours);
//prinitng each employee details
cout<<" Name: "<<name<<" Rate: "<<rate<<" Hours: "<<hours<<" pay: "<<pay;
payRoll = payRoll + pay;
count++;
}
}
//printing results
cout<<" Total Employees: "<<count;
cout<<" Total payRoll: "<<payRoll;
return 0;
}
Explanation / Answer
If it is showing initialized 0 as value for both the count and payRoll, then the file reading part has having some problem. check whether while (iss >> name >> rate >> hours) is getting true and getting inside the while loop. As the input file format is not given, I can't debud it from here. Its most probably it may not getting into the while loop so as to update the values.