-
November 25th, 2013, 09:15 PM
#1
need help the program is showing zero in output section need help
need help the program is showing zero in output section need help cant figure how what im doing wrong
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 []);
void bubbleSort( string [], string [], string [], double [], double [], int);
void findregularhours(double[], int[], int);
void findregularpay(int[], double[], double[], int);
void findovertimehours(double[], double[], int);
void findovertimepay(double[], double[], double[], int);
void findgrosspay(double[], double[], double[], int);
int outputScr(double[], double[], int[]);
int main() //begin main
{
string firstname[SZ], lastname[SZ];
string empids[SZ];
double hoursworked[SZ], overtimehours[SZ];
int regularhours[SZ];
double hourlyrate[SZ], regularpay[SZ], overtimepay[SZ], grosspay[SZ];
int n, numemp;
ofstream outputFile;
bool swapmade = false;
bool screenonly = false;
tellUser();
//readData(firstn, lastn, empID, hrs, rate);
bubbleSort(firstname, lastname, empids, hoursworked, hourlyrate, numemp);
findregularhours(hoursworked, regularhours, n);
findovertimehours(hoursworked, overtimehours, n);
findovertimepay(overtimehours, hourlyrate, overtimepay, n);
findgrosspay(regularpay, overtimepay, grosspay, n);
outputScr(hoursworked, hourlyrate, regularhours);
return 0;
} //end main
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";
} //end tellUser Function
int readData(string firstn[], string lastn[], string empID[], double hrs[], double rate[])
{
int numemp;
ifstream inputFile;
int i = 0;
// open file and read inputs from employees.txt
inputFile.open("employees2.txt");
if (inputFile.fail())
{
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 ****************//
}
} //end of readData Function
void bubbleSort(string firstn[ ],string lastn[ ],string empID[ ],double hrs[ ],double rate[ ],int numemp)
{
//string firstn[SZ], last[SZ], empID[SZ];
//double hrs[SZ], rate[SZ];
int regularhours[SZ];
int i, lastpos;
bool screenonly = false;
bool swapmade = false;
ofstream outputFile;
lastpos = numemp;
{
lastpos--;
swapmade = false;
for ( i = 0; i < lastpos; i++)
{
if (lastn[i] > lastn[i+1])
{ // swap all items here
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]);
//swap(findregularpay[i], findregularpay[i+1]);
//swap(findovertimepay[i], findovertimepay[i+1]);
//swap(findgrosspay[i], findgrosspay[i+1]);
swapmade = true;
}
} // end of for loop
} while(swapmade); // end of do loop
} //end of bubbleSort Function
void findregularhours(double hoursworked[], int regularhours[], int n)
{
for(int i=0; i<n; i++)
{
if(hoursworked[i]>40)
regularhours[i]=40;
else
regularhours[i]=hoursworked[i];
} //end for loop
} //regularhours()
void findregularpay( int regularhours[], double regularpay [], double hourlyrate[], int n)
{
for(int i=0; i<n; i++)
{
regularpay[i] = regularhours[i]*hourlyrate[i];
} //end for loop
} //regularpay
void findovertimehours(double hoursworked[], double overtimehours[], int n)
{
for(int i=0; i<n; i++)
{
if(hoursworked[i]>40)
overtimehours[i]=hoursworked[i]-40;
else
overtimehours[i]=0;
} //end for loop
} // findsovertimehours
void findovertimepay(double overtimehours[], double hourlyrate[], double overtimepay[], int n)
{
for(int i=0; i<n; i++)
{
overtimepay[i]=overtimehours[i]*hourlyrate[i]*1.5;
} //end for loop
} // findovertimepay
void findgrosspay(double regularpay[], double overtimepay[], double grosspay[], int n)
{
for(int i=0; i<n; i++)
{
grosspay[i]=regularpay[i]+overtimepay[i];
} // end for loop
} //findgrosspay
int outputScr(double hoursworked[], double hourlyrate[], int regularhours[])
{
string firstn[SZ], lastn[SZ];
string empID[SZ];
double hrs[SZ];
double rate[SZ];
double grosspay[SZ], regularpay[SZ], overtimepay[SZ];
int i, numemp, lastpos;
bool screenonly = false;
ofstream outputFile;
cout << "Payroll being written to file payroll.txt\n\n";
outputFile.open("payroll.txt"); // output file
numemp = readData(firstn, lastn, empID, hrs, rate);
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";
outputFile <<"First Last Employee Hours Rate Regular Overtime Gross\n";
outputFile <<"Name Name Number Worked of Pay Pay Pay Pay\n";
outputFile <<"=========================================================================================\n";
for (i = 0; i < numemp; i++) //screen only
{
cout << setw(15) << fixed << left << firstn[i] << setw(12) << lastn[i];
cout << setw(11) << empID[i] << " " << setw(12) << fixed << setprecision(2) << hrs[i] << " ";
cout << setw(10) << rate[i] << " " << setw(10) << fixed << setprecision(2) << regularpay[i] << " ";
cout << setw(11) << overtimepay[i] << " " << setw(12) << fixed << setprecision(2) << grosspay[i] << " " << endl;
if (!screenonly)
{
outputFile << setw(13) << fixed << left << firstn[i] << " ";
outputFile << setw(9) << fixed << lastn[i] << " ";
outputFile << setw(9) << fixed << right << empID[i] << " ";
outputFile << setw(9) << fixed << setprecision(2) << hrs[i] << " ";
outputFile << setw(12) << fixed << setprecision(2) << rate[i] << " ";
outputFile << setw(10) << fixed << setprecision(2) << regularpay[i] << " ";
outputFile << setw(11) << fixed << setprecision(2) << overtimepay[i] << " ";
outputFile << setw(10) << fixed << setprecision(2) << grosspay[i] << " " << endl;
}
}
cout <<"============================================================================================\n";
cout <<"\t\t\tTotal Gross Pay\t\t\t\t\t "<< fixed << setprecision(2) << findgrosspay << " \n";
outputFile <<"============================================================================================\n";
outputFile <<"\t\t\tTotal Gross Pay\t\t\t\t\t "<< fixed << setprecision(2) << findgrosspay << " \n";
if (!screenonly)
{
outputFile.close(); //outputFile close.
cout << "input file closed\n\n";}
} // end outputScr Function
the 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 0.00 0.00 0.00
Mary Lincoln L8765 50.00 10.00 0.00 0.00 0.00
Martha Washington W7654 25.50 10.85 0.00 0.00 0.00
John Adams A9876 52.00 15.75 0.00 0.00 0.00
George Washington W1235 45.00 25.00 0.00 0.00 0.00
Abraham Lincoln L9087 40.25 55.00 0.00 0.00 0.00
William Tell T9876 30.00 9.75 0.00 0.00 0.00
-
November 25th, 2013, 09:21 PM
#2
Re: need help the program is showing zero in output section need help
You should use the debugger. That's what it's for. Why is this line in your outputScr function.
double grosspay[SZ], regularpay[SZ], overtimepay[SZ];
-
November 25th, 2013, 09:26 PM
#3
Re: need help the program is showing zero in output section need help
if i take out this line double grosspay[SZ], regularpay[SZ], overtimepay[SZ]; the program runs and displays all 1
-
November 25th, 2013, 09:28 PM
#4
Re: need help the program is showing zero in output section need help
can you help i tried every method to calculate but it wont how, or can you fix the regular pay and i can fiz the overtime pay and grosspay with your help in regular pay
-
November 25th, 2013, 09:30 PM
#5
Re: need help the program is showing zero in output section need help
Originally Posted by Krrish1
if i take out this line double grosspay[SZ], regularpay[SZ], overtimepay[SZ]; the program runs and displays all 1
And now it displays all zeros. What you should do is use the arrays that you populated with data.
-
November 25th, 2013, 09:42 PM
#6
Re: need help the program is showing zero in output section need help
so it should be regularpay = rate[SZ] * hoursworked[SZ]
-
November 25th, 2013, 09:43 PM
#7
Re: need help the program is showing zero in output section need help
Originally Posted by Krrish1
so it should be regularpay = rate[SZ] * hoursworked[SZ]
This is the third thread you've started asking the same question, and yet you're still not following the advice you got the first two times. Stop creating duplicate threads, and pay attention to the advice you've already been given.
forums.codeguru.com/showthread.php?540953-need-help-with-my-source-code-but-it-wont-complie-and-giving-me-errors
Last edited by VictorN; November 27th, 2013 at 05:48 AM.
Reason: Link was edited
-
November 25th, 2013, 09:45 PM
#8
Re: need help the program is showing zero in output section need help
i just need help with regularpay
-
November 25th, 2013, 09:47 PM
#9
Re: need help the program is showing zero in output section need help
Originally Posted by Krrish1
i just need help with regularpay
You need to understand variable scope and passing variables to functions, just as was explained repeatedly in your first thread.
-
November 25th, 2013, 09:50 PM
#10
Re: need help the program is showing zero in output section need help
i cant use variable scope in the program
-
November 26th, 2013, 06:37 AM
#11
Re: need help the program is showing zero in output section need help
Originally Posted by Krrish1
i cant use variable scope in the program
It isn't something you use, it's a concept in how the language works. I feel like I've told you to read up on it a dozen times already. Your lack of understanding of it has been the central theme in all three of your threads.
-
November 26th, 2013, 07:11 AM
#12
Re: need help the program is showing zero in output section need help
Originally Posted by Krrish1
i just need help with regularpay
No, you just want us to fix your program for you so you don't have to take the trouble to learn how to write properly c++ programs. As GCDEF has stated in post #7, you have not followed or understood the help and advice provided in your previous posts.
Short of providing you with the correct code, which we are not going to do, there is really not much additional help and guidance we can give until you understand what has already been said.
Your first problem is that you are reading the data using readData() in outputScr() rather than in main(). You're done this because you aren't passing parameters correctly between functions as we have said previously. Forget about sorting, calculations etc and have your main program as
tell(...)
readData(....)
outputScr(...)
when you have got this simple code to work, then add code for the calculations, sorting etc.
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 26th, 2013, 01:08 PM
#13
Re: need help the program is showing zero in output section need help
Now I got it instead of the read data function to to read in Main, I'm directly reading it in output screen. Now I got that's y it could read from same function. Thanks for the help. I'll correct my code and post it
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
|