-
November 11th, 2013, 11:11 PM
#1
need help with my source code but it wont complie and giving me errors
i tring to write a employee payroll code with functions and read data from the txt. file and also need help with bubble sort that sorts and displays starting from employees last name and calculations.
//txt file//
Hours Pay rate empID first name last name
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
and here is 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();
int regular();
int overtime();
int 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;
numemp = readData(firstname, lastname, empids, hours, rates);
for (i = 0; i < numemp; i++)
{
cout << firstname[i] << lastname[i] << empids[i] << hours[i]
<< rates[i] << endl;
}
bubbleSort();
outputScr();
return 0;
} //end main
/* bubbleSort()
* sorts employees with theor last name snd displays in
* screen and txt file
*/
int bubbleSort()
{
cout << "sorting golfer\n";
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);
}
/* outputScr()
* displays the employee payroll function to the screen and txt file
*/
int outputScr()
{
cout <<"First Last Employee Hours Rate Regular Overtime Gross\n";
cout <<"Name Name Number Worked of Pay Pay Pay Pay\n";
cout <<"===================================================================\n";
for ( i=0; i < numemp; i++)
{
cout<< setw(7) << firstn[i] << setw(13) << lastn[i];
cout<< setw(12) << empID[i] << " " << setw(10) << hrs[i] << " ";
cout<< setw(11) << rate[i] << " " << setw(11) << regular << " ";
cout<< setw(11) << overtime << " " << setw(11) << grossPay << " \n";
if (!screenonly)
{
outputFile << setw(7) << left << firstn[i] << " ";
outputFile << setw(7) << left << lastn[i] << " ";
outputFile << setw(4) << fixed << right << empID << " ";
outputFile << setw(4) << fixed << right << hrs[i] << " ";
outputFile << setw(4) << fixed << right << rate[i] << " \n";
}
}
if (!screenonly)
{
outputFile.close(); cout << "Output file closed\n\n";}
} // end else
/* rellUser(0
* tells about the program to the user
*/
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 from last name.\n";
cout<<"output is written to the screen. \n\n"; //tell user what program does
}
/* regular()
* calculates employees regular pay
*/
int regular()
{
double hours, rate;
if(hours <= 40)
grossPay = hours * rate;
}
/* overtime()
* calculates employees overtime pay
*/
int overtime()
{
double hours, rate;
// const int emp_hrs = 40;
// const double emp_over = 1.5;
if (hours >= 40)
overtime = (hours - 40) * rate * 1.5;
}
/* grossPay()
* calculates employees regular + overtime pay
*/
int grossPay(double &)
{
double hours, rate;
if (hours <= 40)
grossPay = (hours * rate);
else
grossPay = ((hours - 40) * rate * 1.5);
}
/**************************************************
* readData
* firstname , lastname, empID, hours, rate of pay
*/
int readData(string firstn[], string lastn[], string empID[], double hrs[], double rate[])
{
int numemp;
ifstream inputFile;
int i = 0;
tellUser();
// open file and read inputs from employees.txt
inputFile.open("employees.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 ****************//
cout << "input file closed\n\n";
cout << "Payroll being written to file payroll.txt\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";
}
}
}
here are the errors the are giving
Code:
payroll.cpp: In function ‘int bubbleSort()’:
payroll.cpp:52:8: error: ‘lastpos’ was not declared in this scope
payroll.cpp:52:18: error: ‘numemp’ was not declared in this scope
payroll.cpp:56:10: error: ‘swapmade’ was not declared in this scope
payroll.cpp:57:16: error: ‘i’ was not declared in this scope
payroll.cpp:59:18: error: ‘firstn’ was not declared in this scope
payroll.cpp:60:18: error: ‘lastn’ was not declared in this scope
payroll.cpp:61:18: error: ‘empID’ was not declared in this scope
payroll.cpp:62:18: error: ‘hrs’ was not declared in this scope
payroll.cpp:63:18: error: ‘rate’ was not declared in this scope
payroll.cpp:66:16: error: ‘swapmade’ was not declared in this scope
payroll.cpp: In function ‘int outputScr()’:
payroll.cpp:77:13: error: ‘i’ was not declared in this scope
payroll.cpp:77:22: error: ‘numemp’ was not declared in this scope
payroll.cpp:79:30: error: ‘firstn’ was not declared in this scope
payroll.cpp:79:55: error: ‘lastn’ was not declared in this scope
payroll.cpp:80:30: error: ‘empID’ was not declared in this scope
payroll.cpp:80:61: error: ‘hrs’ was not declared in this scope
payroll.cpp:81:30: error: ‘rate’ was not declared in this scope
payroll.cpp:83:16: error: ‘screenonly’ was not declared in this scope
payroll.cpp:85:15: error: ‘outputFile’ was not declared in this scope
payroll.cpp:92:12: error: ‘screenonly’ was not declared in this scope
payroll.cpp:94:10: error: ‘outputFile’ was not declared in this scope
payroll.cpp: In function ‘int regular()’:
payroll.cpp:114:26: error: assignment of function ‘int grossPay()’
payroll.cpp:114:26: error: cannot convert ‘double’ to ‘int()’ in assignment
payroll.cpp: In function ‘int overtime()’:
payroll.cpp:125:39: error: assignment of function ‘int overtime()’
payroll.cpp:125:39: error: cannot convert ‘double’ to ‘int()’ in assignment
payroll.cpp: In function ‘int grossPay(double&)’:
payroll.cpp:134:30: error: overloaded function with no contextual type information
payroll.cpp:136:43: error: overloaded function with no contextual type information
payroll.cpp: In function ‘int readData(std::string*, std::string*, std::string*, double*, double*)’:
payroll.cpp:171:9: error: ‘outputFile’ was not declared in this scope
payroll.cpp:174:12: error: ‘screenonly’ was not declared in this scope
-
November 12th, 2013, 02:21 AM
#2
Re: need help with my source code but it wont complie and giving me errors
Well, error "'something' was not declared in this scope" is very clear: all variables must be declared before using them, so do it!
Victor Nijegorodov
-
November 12th, 2013, 02:29 AM
#3
Re: need help with my source code but it wont complie and giving me errors
Can you tell which one because I check everything and it seems to be in order and I'm just a beginner in C++
-
November 12th, 2013, 02:53 AM
#4
Re: need help with my source code but it wont complie and giving me errors
So I have do declare the same variable both places: one in int main function and the another one at each function where the variable is used
-
November 12th, 2013, 03:44 AM
#5
Re: need help with my source code but it wont complie and giving me errors
Yes, variables declared locally withing some function cannot be visible from outside this function.
Read about Scope in MSDN.
Victor Nijegorodov
-
November 12th, 2013, 08:11 AM
#6
Re: need help with my source code but it wont complie and giving me errors
Originally Posted by Krrish1
So I have do declare the same variable both places: one in int main function and the another one at each function where the variable is used
Yes/no/maybe, depends what you're trying to do. In the case of the first error at least, lastpos isn't declared anywhere. Keep in mind that variables declared in different scopes are different variables, even if they have the same name. I'd suggest reading up on scope and how it works.
-
November 12th, 2013, 08:13 AM
#7
Re: need help with my source code but it wont complie and giving me errors
Originally Posted by Krrish1
So I have do declare the same variable both places: one in int main function and the another one at each function where the variable is used
A variable must be within scope before it can be used. See
http://msdn.microsoft.com/en-us/libr...vs.110%29.aspx
Simplistically, a variable is within scope in the block of code following the line in which it is defined in the block. As functions (including main) are separate 'blocks' a variable defined in one function is not accessible to another function. If variable var1 is defined in main() and also defined in func1(), the variable var1 in main() is a different variable to var1 in func1(). An exception to this are global variables which are usually defined at the start of a program outside of a block and are available to all functions.
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 12th, 2013, 01:13 PM
#8
Re: need help with my source code but it wont complie and giving me errors
here is the code i have corrected and updated:
BUT also need help with bubble sort that sorts and diaplsys starting from emoloyees last name and calculations.
//you can get the text file from above post
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();
int regular();
int overtime();
int 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;
numemp = readData(firstname, lastname, empids, hours, rates);
for (i = 0; i < numemp; i++)
{
cout << firstname[i] << lastname[i] << empids[i] << hours[i]
<< rates[i] << endl;
}
bubbleSort();
outputScr();
return 0;
} //end main
/* bubbleSort()
* sorts employees with theor last name snd displays in
* screen and txt file
*/
int bubbleSort()
{
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;
cout << "sorting golfer\n";
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);
}
/* outputScr()
* displays the employee payroll function to the screen and txt file
*/
int outputScr()
{
string firstn[SZ], lastn[SZ];
string empID[SZ];
double hrs[SZ];
double rate[SZ];
int i, numemp, lastpos;
bool screenonly = false;
ofstream outputFile;
cout << "Payroll being written to file payroll.txt\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";
for ( i=0; i < numemp; i++)
{
cout<< setw(7) << firstn[i] << setw(13) << lastn[i];
cout<< setw(12) << empID[i] << " " << setw(10) << hrs[i] << " ";
cout<< setw(11) << rate[i] << " " << setw(11) << regular << " ";
cout<< setw(11) << overtime << " " << setw(11) << grossPay << " \n";
if (!screenonly)
{
outputFile << setw(7) << left << firstn[i] << " ";
outputFile << setw(7) << left << lastn[i] << " ";
outputFile << setw(4) << fixed << right << empID << " ";
outputFile << setw(4) << fixed << right << hrs[i] << " ";
outputFile << setw(4) << fixed << right << rate[i] << " \n";
}
}
if (!screenonly)
{
outputFile.close(); cout << "Output file closed\n\n";}
} // end else
/* rellUser(0
* tells about the program to the user
*/
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 from last name.\n";
cout<<"output is written to the screen. \n\n"; //tell user what program does
}
/* regular()
* calculates employees regular pay
*/
int regular()
{
double hours, rate;
if(hours <= 40)
grossPay = hours * rate;
}
/* overtime()
* calculates employees overtime pay
*/
int overtime()
{
double hours, rate;
// const int emp_hrs = 40;
// const double emp_over = 1.5;
if (hours >= 40)
overtime = (hours - 40) * rate * 1.5;
}
/* grossPay()
* calculates employees regular + overtime pay
*/
int grossPay(double &)
{
double hours, rate;
if (hours <= 40)
grossPay = (hours * rate);
else
grossPay = ((hours - 40) * rate * 1.5);
}
/**************************************************
* readData
* firstname , lastname, empID, hours, rate of pay
*/
int readData(string firstn[], string lastn[], string empID[], double hrs[], double rate[])
{
int numemp;
ifstream inputFile;
int i = 0;
tellUser();
// open file and read inputs from employees.txt
inputFile.open("employees.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 ****************//
cout << "input file closed\n\n";
}
}
NOW THE THE ERROR IS:
Code:
payroll.cpp: In function ‘int regular()’:
payroll.cpp:137:26: error: assignment of function ‘int grossPay()’
payroll.cpp:137:26: error: cannot convert ‘double’ to ‘int()’ in assignment
payroll.cpp: In function ‘int overtime()’:
payroll.cpp:148:39: error: assignment of function ‘int overtime()’
payroll.cpp:148:39: error: cannot convert ‘double’ to ‘int()’ in assignment
payroll.cpp: In function ‘int grossPay(double&)’:
payroll.cpp:157:30: error: overloaded function with no contextual type information
payroll.cpp:159:43: error: overloaded function with no contextual type information
-
November 12th, 2013, 01:28 PM
#9
Re: need help with my source code but it wont complie and giving me errors
I get different errors, but these lines aren't valid in C++.
Code:
int grossPay(double &)
{
double hours, rate;
if (hours <= 40)
grossPay = (hours * rate);
else
grossPay = ((hours - 40) * rate * 1.5);
}
grossPay would need to be a variable if you're assigning a value to it, and the function needs to return a value. I've used languages where you return function values like that, but C++ doesn't work that way.
There are a lot of things wrong with just that function.
1) You're passing in an argument you can't get to because it isn't named.
2) hours and rate are unitialized. You have no idea what values they contain. Could be anything.
3) You're returning an int, but the value could contain a fractional component.
4) As mentioned, grossPay isn't defined.
In general, you still don't seem to grasp the concept of scope. For example, everywhere you have a statement such as "double hrs[SZ];", you're defining a new variable called hrs, that is completely unrelated to do with the previous definitions. They're not the same variable, even if they have the same name.
C++ doesn't lend itself to guessing. Go back and reread the book or whatever you're learning from. Don't try to write a whole program and compile it all at once. Write a few lines. Compile and test them. When you're sure they're working, move on.
-
November 12th, 2013, 01:29 PM
#10
Re: need help with my source code but it wont complie and giving me errors
Originally Posted by Krrish1
[B]here is the code i have corrected and updated:
Code:
/* regular()
* calculates employees regular pay
*/
int regular()
{
double hours, rate;
if(hours <= 40)
grossPay = hours * rate;
}
/* overtime()
* calculates employees overtime pay
*/
int overtime()
{
double hours, rate;
// const int emp_hrs = 40;
// const double emp_over = 1.5;
if (hours >= 40)
overtime = (hours - 40) * rate * 1.5;
}
/* grossPay()
* calculates employees regular + overtime pay
*/
int grossPay(double &)
{
double hours, rate;
if (hours <= 40)
grossPay = (hours * rate);
else
grossPay = ((hours - 40) * rate * 1.5);
}
What could this assignemt in int regular() function mean:
Code:
grossPay = hours * rate;
The same question - about
Code:
overtime = (hours - 40) * rate * 1.5;
in int overtime () function and
Code:
grossPay = ((hours - 40) * rate * 1.5);
in int grossPay(double &) function.
Where is the double & parameter you were going to pass in grossPay() function?
Last edited by VictorN; November 12th, 2013 at 01:41 PM.
Reason: typos
Victor Nijegorodov
-
November 12th, 2013, 01:38 PM
#11
Re: need help with my source code but it wont complie and giving me errors
Could you correct and show the correct format of the int regular function code
So I can fix the grossPay and overtime
-
November 12th, 2013, 01:48 PM
#12
Re: need help with my source code but it wont complie and giving me errors
Originally Posted by Krrish1
Could you correct and show the correct format of the int regular function code
So I can fix the grossPay and overtime
your function
Code:
int regular()
{
double hours, rate;
if(hours <= 40)
grossPay = hours * rate;
}
does not make any sense, so I cannot "correct" it.
You declare two double variables but do not initialize them. Therefore neither any comparison with 40 nor the multiplication the two uninitialized variables makes sense.
Besides, you do not return any value from this function.
I guess you should begin to learn the basics of C++: how to implement and use functions in it.
Victor Nijegorodov
-
November 12th, 2013, 01:53 PM
#13
Re: need help with my source code but it wont complie and giving me errors
int regular()
{
double hours, rate; // THE DATA FOR HOURS AND RATE SHOULD BE READ FROM TXT SHOWN BELOW
if(hours <= 40)
grossPay = hours * rate;
}
//txt file//
Hours Pay rate empID first name last name
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
-
November 12th, 2013, 02:05 PM
#14
Re: need help with my source code but it wont complie and giving me errors
Originally Posted by Krrish1
int regular()
{
double hours, rate; // THE DATA FOR HOURS AND RATE SHOULD BE READ FROM TXT SHOWN BELOW
if(hours <= 40)
grossPay = hours * rate;
}
//txt file//
Hours Pay rate empID first name last name
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
They are in main, but as has been mentioned several times, the hours in main and the hours in regular are separate, unrelated variables. You're being told the problem, but you're not taking any action to correct it. Your code is beyond correcting. It needs a rewrite, and nobody here, with one possible exception, is going to do that for you. Start with a couple of lines. Use the debugger to makes sure it works, then move on to the next few lines.
-
November 12th, 2013, 02:09 PM
#15
Re: need help with my source code but it wont complie and giving me errors
so how can we use the same variable in the calculations
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
|