### Input validation problems

Payroll program:
input pay rate/hours worked
validate input so pay rate is in range of \$7.50 - \$18.25 and hours 0-40
display employees gross pay

[code]
int main()
{
Real payrate,hours, gross;

cout << "Enter the employees hourly pay rate. \n";
cin >> payrate;

cout<< "Enter the total hours worked by employee. \n";
cin >> hours;

while InvalidPayRate(payrate)
{
cout << "The valid value for pay rate is in the range. \n";
cout << " of \$7.50 through \$ 18.25. \n";
cout << "Enter a valid value for pay rate. \n";
cin >> payrate;
}

while InvalidHours(hours)
{
cout << "Please enter hours worked between 0 and 40. \n ";
cin >> hours;
}
gross = payrate * hours;
cout << "The gross pay of the employee is \$ " << gross;

}

ERRORS:

F:\CH7Q1Payroll.cpp||In function 'int main()':|
F:\CH7Q1Payroll.cpp|34|error: expected '(' before 'InvalidPayRate'|
F:\CH7Q1Payroll.cpp|34|error: 'InvalidPayRate' was not declared in this scope|
F:\CH7Q1Payroll.cpp|35|error: expected ')' before '{' token|
F:\CH7Q1Payroll.cpp|42|error: expected '(' before 'InvalidHours'|
F:\CH7Q1Payroll.cpp|42|error: 'InvalidHours' was not declared in this scope|
F:\CH7Q1Payroll.cpp|43|error: expected ')' before '{' token|

InvalidPayRate and InvalidHours are not declared anywhere so you cannot use them. While statements always need to be
 ``1234`` ``````while (boolean) { //code }``````

Have you defined the variable type "Real" somewhere else? If not, those should all be type double.

It should look more like
 ``123456789101112131415161718192021222324252627`` ``````int main() { double payrate, hours, gross; cout << "Enter the employees hourly pay rate. \n"; cin >> payrate; cout<< "Enter the total hours worked by employee. \n"; cin >> hours; while (payrate > 18.25 || payrate < 7.5) { cout << "The valid value for pay rate is in the range. \n"; cout << " of \$7.50 through \$ 18.25. \n"; cout << "Enter a valid value for pay rate. \n"; cin >> payrate; } while (hours > 40 || hours < 0) { cout << "Please enter hours worked between 0 and 40. \n "; cin >> hours; } gross = payrate * hours; cout << "The gross pay of the employee is \$ " << gross; }``````
Last edited on
I declared Real payrate, hours, gross... can not use double. Has not been "taught" to the class yet.
But "Real" is not an integer type in the first place, so it is not usable at all. You must be leaving out some of the code. Otherwise try "float" instead of double? You must have learned one of those just to be able to complete this.
Nope. No "float". No "double". Guess how much fun it is to ask your teacher why your program will not compile and their response is "mmm... I do not know. We will need to look it up."

The "Real" did not change anything when it compiled but I did not know it was something that was not usable.

Thanks for your help.
No problem. Are you sure you are using c++? Does it have #include anything at the top? Is there more that you haven't entered here?
If your professor is telling you not to use floats and doubles, then he's obviously not qualified to tell you to write a program that can only operate using floats and doubles.

Mabey you could write your own floating point number class, or "have classes not been taught" yet? ;)

In any case, get points docked off your project: use a double. Your professor is wrong for thinking you can perform finance calculations on integers, lol.
This is what we copy into each program before we start:

// add the following lines at the beginning of the source file
#include <iostream>

using namespace std;

// create translation from generic keywords to C++
#define Declare
#define ByRef ref
#define Call
#define Constant const
#define Declare
#define Display cout <<
#define Eof cin.eof()
#define Function
#define Input cin >>
#define InputLine(x) getline(cin, x)
#define Integer int
#define Module
#define Newline endl
#define Real double
#define String string
#define Set

Ha! I see now the Real = double

Haha there it is, although all of that is pretty weird to be adding in as it teaches improper syntax... but I suppose your teacher must know what (s)he is doing? Good luck.
squirrel27:
I highly advise you not use those. They don't actually make any difference in how the code is executed (absolutely none, they are only there for asthetic purposes), and you will never see them in the field (if you do, it will be rare).

That said, if you get docked points for useing `double` instead of Real, then you should tell your proffessor there is no difference between a double, and a preprocessor directive that replaces "Real" with double.
Topic archived. No new replies allowed.