-
November 14th, 2013, 01:18 AM
#1
need help with calculations but it wont complie and giving me errors
ok i have correct and update my code and it does work with no errors expect it doesnt do calculations and bubblesort with employees last name
// here is the txt file//
Code:
40.0 10.00 A1234 Jane Adams
50.0 10.00 L8765 Mary Lincoln
25.5 10.85 W7654 Martha Washington
52.0 15.75 A9876 John Adams
45.0 25.00 W1235 George Washington
40.25 55.00 L9087 Abraham Lincoln
30.0 9.75 T9876 William Tell
42.5 12.50 M7654 Missy Muffett
30.0 10.00 P8765 Peter Piper
//heres my code//
Code:
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int SZ = 55;
void tellUser();
int readData( string [], string [], string [], double [], double []);
int bubbleSort();
int outputScr();
void regular();
void overtime();
void grossPay();
int main()
{
string firstname[SZ], lastname[SZ];
string empids[SZ];
double hours[SZ];
double rates[SZ];
ofstream outputFile;
bool swapmade = false;
bool screenonly = false;
char yesno;
int i, numemp, lastpos;
tellUser(); // tells the user about the program
bubbleSort(); // sorts the employees from their last , first name
outputScr(); // display employee payroll information to screen and txt file
return 0;
} //end main
/* tellUser()
* tells about the program to the user
*/
void tellUser() // tellUser Function
{
cout <<"\nThis program reads a file called employees.txt,\n";
cout <<"and it calculates the regular pay, overtime pay\n";
cout <<"and grosspay and total for grosspay for each employee and\n";
cout <<"sorts the from last name and output is written to the screen. \n\n"; //tell user what program does
} //end tellUser Function
/*
* readData
* firstname , lastname, empID, hours, rate of pay
*/
int readData(string firstn[], string lastn[], string empID[], double hrs[], double rate[]) //readData Function
{
int numemp;
ifstream inputFile;
int i = 0;
// tellUser();
// open file and read inputs from employees.txt
inputFile.open("employees.txt"); //open employees.txt file
if (inputFile.fail()) // employee.txt fails to open
{
cout << "Error opening file employees.txt \n\n";
cout << "end of program\n\n";
}
else
{
while ((inputFile >> hrs[i]) && (i < SZ))
{
inputFile >> rate[i];
inputFile >> empID[i];
inputFile >> firstn[i];
inputFile >> lastn[i];
i++;
} //end while
// cout << "There were " << i << " employess\n\n";
// numemp = i;
inputFile.close();
//************* close input file ****************//
}
} //en dof readData Function
/* bubbleSort()
* sorts employees with theor last name snd displays in
* screen and txt file
*/
int bubbleSort() //bubbleSort Function
{
string firstn[SZ], lastn[SZ];
string empID[SZ];
double hrs[SZ];
double rate[SZ];
int i, numemp, lastpos;
bool screenonly = false;
ofstream outputFile;
bool swapmade = false;
lastpos = numemp;
do
{
lastpos--;
swapmade = false;
for ( i = 0; i < lastpos; i++)
{
swap(firstn[i], firstn[i+1]);
swap(lastn[i], lastn[i+1]);
// swap(empID[i], empID[i+1]);
// swap(hrs[i], hrs[i+1]);
// swap(rate[i], rate[i+1]);
swapmade - true;
}
} while(swapmade);
} //end of bubbleSort Function
/* regular()
* calculates employees regular pay
*/
void regular()
{
double grossPay;
double hours, rates;
if(hours <= 40)
grossPay = hours * rates;
}
/* overtime()
* calculates employees overtime pay
*/
void overtime()
{
double hours, rate, overtime;
if (hours >= 40)
overtime = (hours - 40) * rate * 1.5;
}
/* grossPay()
* calculates employees regular + overtime pay
*/
void grossPay()
{
double hours, rate, grossPay;
if (hours <= 40)
grossPay = (hours * rate);
else
grossPay = ((hours - 40) * rate * 1.5);
}
/* outputScr()
* displays the employee payroll function to the screen and txt file
*/
int outputScr()
{
string firstname[SZ], lastname[SZ];
string empids[SZ];
double hours[SZ];
double rates[SZ];
int i, numemp, lastpos;
bool screenonly = false;
ofstream outputFile;
cout << "Payroll being written to file payroll.txt\n\n"; //output function
outputFile.open("payroll.txt"); // output file
if (outputFile.fail())
{
screenonly = true;
cout <<" output file did not open\n";
cout <<" output file will only be sent to the screen\n";
}
cout <<" First Last Employee Hours Rate Regular Overtime Gross\n";
cout <<" Name Name Number Worked of Pay Pay Pay Pay\n";
cout <<"============================================================================================\n";
numemp = readData(firstname, lastname, empids, hours, rates);
for (i = 0; i < numemp; i++)
{
cout << setw(7) << firstname[i] << setw(12) << lastname[i];
cout << setw(11) << empids[i] << " " << setw(12) << fixed << setprecision(2) << hours[i] << " ";
cout << setw(11) << rates[i] << " " << setw(7) << fixed << setprecision(2) << regular << " ";
cout << setw(9) << overtime << " " << setw(12) << fixed << setprecision(2) << grossPay << " " << endl;
if (!screenonly)
{
outputFile << setw(7) << left << firstname[i] << " ";
outputFile << setw(12) << left << lastname[i] << " ";
outputFile << setw(11) << fixed << right << empids << " ";
outputFile << setw(12) << fixed << right << hours[i] << " ";
outputFile << setw(11) << fixed << right << rates[i] << " \n";
}
}
cout <<"============================================================================================\n";
cout <<"\t\t\tTotal Gross Pay \t\t\t\t\t" << fixed << setprecision(2) << grossPay << " \n";
if (!screenonly)
{
outputFile.close();
cout << "inpur file closed\n\n";}
} // end main
//results i get after compiling the program *** CALCULATIONS AND BUBVLE SORT (arrange from from employees last name***//
Code:
First Last Employee Hours Rate Regular Overtime Gross
Name Name Number Worked of Pay Pay Pay Pay
======================================================
Jane Adams A1234 40.00 10.00 1 1 1
Mary Lincoln L8765 50.00 10.00 1 1 1
Martha Washington W7654 25.50 10.85 1 1 1
John Adams A9876 52.00 15.75 1 1 1
George Washington W1235 45.00 25.00 1 1 1
Abraham Lincoln L9087 40.25 55.00 1 1 1
William Tell T9876 30.00 9.75 1 1 1
Missy Muffett M7654 42.50 12.50 1 1 1
Peter Piper P8765 30.00 10.00 1 1 1
===================================================
Total Gross Pay 1
-
November 14th, 2013, 03:58 AM
#2
Re: need help with calculations but it wont complie and giving me errors
-
November 14th, 2013, 04:12 AM
#3
Re: need help with calculations but it wont complie and giving me errors
You have to debug your code step-by-step to see what goes wrong, where and why!
Victor Nijegorodov
-
November 14th, 2013, 04:16 AM
#4
Re: need help with calculations but it wont complie and giving me errors
You should first read your data from the file, then bubble sort it, then output it.
Your order of operations is not correct.
Also, which data is bubble sort using? It seems to be using local arrays which are empty...
-
November 14th, 2013, 05:07 AM
#5
Re: need help with calculations but it wont complie and giving me errors
i need help on bubble sort and calculations
-
November 14th, 2013, 05:16 AM
#6
Re: need help with calculations but it wont complie and giving me errors
Originally Posted by Krrish1
i need help on bubble sort and calculations
What kind of "help" do you need?
Did you fix the problems Marc pointed to in the post#4?
Victor Nijegorodov
-
November 14th, 2013, 06:44 AM
#7
Re: need help with calculations but it wont complie and giving me errors
Your main program logic should be similar to
telluser()
readdata()
bubblesort()
outputscr()
Unless you pass firstn, lastn, empid, hrs and rate as parameters to readdata(), bubblesort() and outputscr(), you need them to be global variables so that they can be accessed by the functions. See my example of global varaibles in previous post.
The firstn and lastn variable defines in bubblesort() are different variables to those with the same name defined as parameters to readdata() - again see my previous example.
Instead of using separate arrays for firstn, lastn, empid etc, better practice would be to define a struct containing the data for one employee and have an array of that struct (or better a vector of that struct).
In bubble sort you need to swap the contents of all of the arrays and not just firstn and lastn
Code:
struct Employee {
string firstn;
string lastn;
string empid;
double hrs;
double rate;
};
Employee empdata[SZ];
You're also got problems with your regular, overtime and grosspay functions. They take no parameters, return no value and within the functions use un-initialised variables! They do absolutely nothing!
Last edited by 2kaud; November 14th, 2013 at 06:51 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 14th, 2013, 06:55 AM
#8
Re: need help with calculations but it wont complie and giving me errors
Don't start a second post asking the same question, especially when you're ignoring the replies in the first post. The problems we pointed out are still there, and it's never going to work until you understand them and fix them. You don't understand variables and scope and you absolutely need to.
http://forums.codeguru.com/showthrea...ving-me-errors
Last edited by GCDEF; November 14th, 2013 at 06:57 AM.
-
November 14th, 2013, 02:23 PM
#9
Re: need help with calculations but it wont complie and giving me errors
-
November 15th, 2013, 05:31 PM
#10
Re: need help with calculations but it wont complie and giving me errors
Just to let you see what a c++ program looks like, this is one possible way of coding the program. As this uses c++ constructs you won't yet have covered, I won't copy it for my assignment! - but it will give you an idea of the power and flexibility of c++.
Code:
#include <iostream>
#include <algorithm>
#include <fstream>
#include <string>
#include <iomanip>
#include <vector>
using namespace std;
void tellUser();
class Payroll {
private:
const static double ovrRate;
struct Employee {
string firstn;
string lastn;
string empid;
double hrs;
double rate;
};
typedef vector<Employee> vEmp;
typedef vEmp::const_iterator vEmpci;
vEmp empData;
ifstream inputFile;
mutable ofstream outputFile;
static double regular(double hours, double rate);
static double overtime(double hours, double rate);
static bool nametest(const Employee& emp1, const Employee& emp2);
Payroll(const Payroll&) {};
operator =(const Payroll&) {};
public:
Payroll(const string& infile, const string& outfile);
~Payroll();
bool readData();
void outputScr() const;
void sort();
friend istream& operator >>(istream& is, Employee& emp);
};
const double Payroll::ovrRate = 1.5;
int main()
{
const string ifile = "employees.txt";
const string ofile = "payroll.txt";
Payroll payroll(ifile, ofile);
tellUser();
if (!payroll.readData()) {
cerr << "Could not open input file " << ifile << endl;
return 1;
}
payroll.sort();
payroll.outputScr();
return 0;
}
istream& operator >>(istream& is, Payroll::Employee& emp)
{
is >> emp.hrs;
is >> emp.rate;
is >> emp.empid;
is >> emp.firstn;
is >> emp.lastn;
return is;
}
Payroll::Payroll(const string& infile, const string& outfile)
{
inputFile.open(infile.c_str());
outputFile.open(outfile.c_str());
}
Payroll::~Payroll()
{
inputFile.close();
outputFile.close();
}
void Payroll::sort()
{
std::sort(empData.begin(), empData.end(), nametest);
}
bool Payroll::readData()
{
if (!inputFile.is_open())
return false;
Employee emp;
while (inputFile >> emp)
empData.push_back(emp);
return true;
}
void Payroll::outputScr() const
{
bool screenonly = !outputFile.is_open();
double grossPay = 0;
cout << "Payroll being written to file payroll.txt\n\n"; //output function
if (screenonly)
cerr <<" output file did not open" << endl << "output file will only be sent to the screen\n";
cout <<" First Last Employee Hours Rate Regular Overtime Gross\n";
cout <<" Name Name Number Worked of Pay Pay Pay Pay\n";
cout <<"============================================================================================\n";
for (vEmpci ci = empData.begin(); ci != empData.end(); ++ci) {
const double reg = ((int)(regular(ci->hrs, ci->rate) * 100.0)) /100.0;
const double over = ((int)(overtime(ci->hrs, ci->rate) * 100.0)) / 100.0;
grossPay += reg + over;
cout << setw(7) << ci->firstn << setw(12) << ci->lastn;
cout << setw(11) << ci->empid << " " << setw(12) << fixed << setprecision(2) << ci->hrs << " ";
cout << setw(11) << ci->rate << " " << setw(11) << fixed << setprecision(2) << reg << " ";
cout << setw(9) << over << " " << setw(10) << fixed << setprecision(2) << reg + over << " " << endl;
if (!screenonly) {
outputFile << setw(7) << left << ci->firstn << " ";
outputFile << setw(12) << left << ci->lastn << " ";
outputFile << setw(11) << fixed << right << ci->empid << " ";
outputFile << setw(12) << fixed << right << setprecision(2) << ci->hrs << " ";
outputFile << setw(11) << fixed << right << setprecision(2) << ci->rate << " \n";
}
}
cout <<"============================================================================================\n";
cout <<"\t\t\t\t\t\t\t\tTotal Gross Pay " << fixed << setprecision(2) << grossPay << " \n";
}
bool Payroll::nametest(const Employee& emp1, const Employee& emp2)
{
if (emp1.lastn == emp2.lastn)
return (emp1.firstn < emp2.firstn);
return (emp1.lastn < emp2.lastn);
}
double Payroll::regular(double hours, double rate)
{
if (hours > 40)
hours = 40;
return hours * rate;
}
double Payroll::overtime(double hours, double rate)
{
return (hours > 40) ? (hours - 40) * rate * ovrRate : 0;
}
void tellUser()
{
cout << "\nThis program reads a file called employees.txt\n";
cout << "and it calculates the regular pay, overtime pay\n";
cout << "and grosspay and total for grosspay for each employee and\n";
cout << "sorts the input by last name and output is written to the screen.\n\n";
}
It produces as output
Code:
First Last Employee Hours Rate Regular Overtime Gross
Name Name Number Worked of Pay Pay Pay Pay
=========================================================================================
Jane Adams A1234 40.00 10.00 400.00 0.00 400.00
John Adams A9876 52.00 15.75 630.00 283.50 913.50
Abraham Lincoln L9087 40.25 55.00 2200.00 20.62 2220.62
Mary Lincoln L8765 50.00 10.00 400.00 150.00 550.00
Missy Muffett M7654 42.50 12.50 500.00 46.87 546.87
Peter Piper P8765 30.00 10.00 300.00 0.00 300.00
William Tell T9876 30.00 9.75 292.50 0.00 292.50
George Washington W1235 45.00 25.00 1000.00 187.50 1187.50
Martha Washington W7654 25.50 10.85 276.67 0.00 276.67
=========================================================================================
Total Gross Pay 6687.66
Have fun
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|