2 Replies - 819 Views - Last Post: 05 April 2011 - 12:51 AM Rate Topic: -----

#1 kidraven   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-April 11

Error with displaying the correct decimals with calculations.

Posted 04 April 2011 - 12:29 AM

New to forum, cout<<"Hello, Dream In Code World."<<endl;, I have this lab that I have been working on and received helped from fellow peers on campus. The majority of the lab is finished just have some issues with the program truncating my calculations. I have tried using cout<<setprecision(3)<<showpoint<<endl; for the output, floating all my variables, and the output is correct in a manner of speaking but it's rounding to the nearest whole number. Any help would be great thanks all output using a float should display with xxxxx.xx.

//
//
//

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
#include <string>
using namespace std;

struct EmployeeInfo
{
    //data members
    char Name[28];
    float Payrate,Gross,Hrs;
    char ID[12];
    char Date[10];
    
    //member functions
    void ShowInfo();
	void PReport();
};

void CleanFile(EmployeeInfo &);
void Header();
float AvgGrossPay(EmployeeInfo [100], int);
float Totalpay(EmployeeInfo [100], int);
int FindOne(EmployeeInfo [100], char [12], int);

int main()
{
    //Declare variables
    EmployeeInfo BayWestIndustries[100];
    EmployeeInfo Emp;

    fstream DataIn;
    fstream BinOut;
	fstream BinIn;

    int totalItems=0;
    char NDate[10];
    char temp[7];
    string datestr;
    char temp1[10];

    //Open data file
    DataIn.open("F:\\Employees.csv");
    if(DataIn.fail())
    {
        cout << "Unable to open data file." << endl;
        system("PAUSE");
        return 999;
    }
    BinOut.open("F:\\Lab Data Files\\Employees.bin",ios::out|ios::binary);
    if(BinOut.fail())
    {
        cout << "Opening output file failed." << endl;
        return 777;
    }

    while(DataIn.eof() == false)
    {
        CleanFile(Emp);    //Wipe the member fields of the InventoryItem object
        char firstName[32], lastName[32];
        firstName[0] = '\0';
        lastName[0] = '\0';
        
        int d2=0;
        int iMonth;//to hold the first 2 digits of the date
        char  month[2];//to hold the first 2 digits of the date
        int slashIndex1, slashIndex2;//where the first and second slash goes in the date
        
        //read the data
        //**********************
        DataIn.getline(firstName,28,',');
        DataIn.getline(lastName,28,',');

        strcat(Emp.Name, firstName);
        strcat(Emp.Name, " ");
        strcat(Emp.Name, lastName);

        DataIn.getline(Emp.ID,12,',');
        DataIn.getline(Emp.Date,9,',');
        datestr=Emp.Date;

        DataIn.getline(temp,7,',');
		//cout<<"Value of temp"<<temp<<endl;
        Emp.Payrate=atof(temp);

        DataIn.getline(temp,3,'\n');
		
        Emp.Hrs=atof(temp);
        //**********************
        // format the date      
        //**********************
        //get the first 2 digits which might be the month
        for(int i=0;i<2;i++)
            month[i]=Emp.Date[i];
        //convert them to an int
        iMonth=atoi(month);
        
        if(iMonth>12 || strlen(Emp.Date)==6)
        {
            //it must be a single digit month
            slashIndex1=1;
        }
        else
        {
             //it must be a two digit month   
             slashIndex1=2;
        }
        //a year should always be 4 digits
        slashIndex2=strlen(Emp.Date)-4;

        for(int ch=0;ch<strlen(Emp.Date);ch++,d2++)
        {       
            if(ch==slashIndex1 || ch==slashIndex2)
            {
                NDate[d2++]='/';
                NDate[d2]=Emp.Date[ch];
            }
            else
            {
                NDate[d2]=Emp.Date[ch];
            }
    
        } 
        NDate[d2]='\0';
        // end format date
 
        //copy the formatted date back into the struct
        //strcpy(Emp.Date, datestr.c_str());
        strcpy(Emp.Date,NDate);
        
        if(DataIn.eof())
        {
            break;
        } 
        BinOut.write(reinterpret_cast<char *>(&Emp),sizeof(Emp));
        if(BinOut.fail())
        {
             cout << "Write to binary file failed." << endl;
             return 666;
        }
        
        Emp.Gross = Emp.Payrate * Emp.Hrs;// calculate grosspay
        BayWestIndustries[totalItems]=Emp;
        totalItems++;
    }//end of while loop

    //display the header
    Header();
    for(int j=0;j<totalItems;j++)
    {
        //display each record           
        BayWestIndustries[j].PReport();
    }
    float avgGross = AvgGrossPay(BayWestIndustries, totalItems);
    float Tgrosspay = Totalpay(BayWestIndustries, totalItems);

    cout<<setprecision(3)<<showpoint;
    cout<<"\t\t\t\t\t\t  Total Employees: "<<totalItems<<endl;
    cout<<right<<"\t\t\t\t\t\t  Total Gross Pay: "<<Tgrosspay<<endl; //calculation is wrong
    cout<<"\t\t\t\t\t\tAverage Gross Pay: "<<avgGross<<endl;
    cout<<right<<"\n\t\t\tReport Prepared By: "<<endl;
    char testID1[12] = "204-65-8006";
    char testID2[12] = "219-52-4157";
    int found1 = FindOne(BayWestIndustries, testID1, totalItems);
    int found2 = FindOne(BayWestIndustries, testID2, totalItems);

    if(found1==-1)
    {
        cout<<"\nThe First Entry ID was not found.\n";
    }
    else
    {
        cout<<"\nThe First Entry ID was found and its index is: "<<found1<<"\n";
        BayWestIndustries[found1].ShowInfo();
    }
    if(found2==-1)
    {
        cout<<"\nThe Second Entry ID was not found.";
    }
    else
    {
        cout<<"\nThe Second Entry ID was found and its index is: "<<found2<<"\n\n"<<endl;
        BayWestIndustries[found2].ShowInfo();
        
    }

    DataIn.close();
    BinOut.close();
    cout<<"\n\n";
    system("PAUSE");
    return 0;
}

//Function definitions
void EmployeeInfo::ShowInfo()
{
    cout<<left<<setprecision(3)<<showpoint;
	cout<<"\n"<<Name<<endl
		<<" "<<Date<<endl
		<<" "<<ID<<endl
		<<" "<<Hrs <<" @ " << Payrate<<endl;


}
void CleanFile(EmployeeInfo & Report)
{

    strncpy(Report.Name," ",28);
    strncpy(Report.Date," ",9);
    strncpy(Report.ID," ",12);
    Report.Hrs=0.00f;
    Report.Payrate=0.00f;
    Report.Gross=0.00f;
}
void Header()
{
    cout<<left<<setprecision(3)<<showpoint;
    cout<<"\n\n\t\t\t\tBay West Industries"<<endl;
    cout<<"\n\t\t\t\t   Payroll Report"<<endl;
    cout<<left<<setw(27)<<"\n\n Employee"<<setw(12)<<"Date Hired"<<setw(14)<<"ID"<<setw(7)
        <<"Hours"<<setw(7)<<"Rate"<<setw(9)<<"Gross Pay"<<endl;
    cout<<"---------------------------------------------------------------------------"<<endl;
}
void EmployeeInfo::PReport()
{
    cout<<left<<setw(25)<<Name<<setw(12)<<Date<<setw(14)<<ID<<setw(7)<<Hrs<<setw(7)<<Payrate<<setw(7)<<Gross<<endl;
    cout<<"---------------------------------------------------------------------------"<<endl;
}
float AvgGrossPay(EmployeeInfo emps[100], int count)
{
    float retval=0.00f;

    for(int i=0;i<count;i++)
    {
		//cout<<"Gross Pay is: "<<emps[i].Gross<<endl;
        retval += float(emps[i].Gross);
    }
    cout<<setprecision(3)<<showpoint;
    return float(retval / count);
}
float Totalpay(EmployeeInfo emps[100], int count)
{
    float totalgross=0.00f;
    
    for(int t=0;t<count;t++)
    {
        totalgross += float(emps[t].Gross);
    }
    return totalgross;
}
int FindOne(EmployeeInfo emps[100], char searchID[12], int count)
{
    bool match=true;
    int i;

    for(i=0;i<count;i++)
    {
        for(int j=0;j<strlen(searchID);j++)
        {
            if(emps[i].ID[j]==searchID[j])
            {
                match=true;
            }
            else
            {
                match=false;
                break;
            }
        }
        if(match==true)
            break;
    }
    if(match==true)
        return i;
    else
        return -1;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Error with displaying the correct decimals with calculations.

#2 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Error with displaying the correct decimals with calculations.

Posted 04 April 2011 - 03:38 AM

Welcome to DIC!

Please give us some more details of your problem.
( a ) Does your code compile?
( b ) Any errors or warnings? If there are then share them with us.
Copy and paste the errors exactly as they are.
( c ) Is the program producing any output?
( d ) How is the actual output different to what you want / expect?
Give details and, ideally, examples.
Show us an example of output you receive and contrast it with the output you want to receive.
If you provided inputs to the program tell us what they were.
( e ) What have you already tried to fix it?

This post has been edited by janotte: 04 April 2011 - 03:40 AM

Was This Post Helpful? 1
  • +
  • -

#3 kidraven   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-April 11

Re: Error with displaying the correct decimals with calculations.

Posted 05 April 2011 - 12:51 AM

Hey thanks for the reply. While I had posted this topic I navigated through the website and asked other people on campus and my problem was in the way I was displaying my output. Solution was solved after reading your response I was looking over my code which had me cout certain info to see how it was being stored which led to a solution, thanks. Here is what the output displays: jpeg attached.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1