More payroll...

Hi all,

Sorry to be a nuisance, I find that I learn more from you than from my text (professor doesn't teach, respond to emails or grade assignments).

I have another payroll assignment, but less math-heavy. Everything is to be done with for, while, and do/while loops. Right now, I need to establish a range of valid entries for variables-, because the professor wants error messages returned for a series of bad entries for pay rate, hours worked, etc.

I know how to accomplish this with if/else statements (thanks to you) and now I need to learn to do it with loops. Example: employee ID should be between 1 and 100.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int empID;
    int nextVariable;
    
    cout << "Enter employee ID " << endl;
    cin >> empID;

    do{
        cout << "Invalid entry. Please enter employee ID " << endl;
        }while( empID >100 && empID <1 );
    do{
        cout << "Enter nextVariable " << endl;
        cin >> nextVariable;
        }while ( empID >=1 && empID <=100 );
        

                                   
                                   
                system("PAUSE");
                return 0;
}


I want this code to keep returning to ask for the employee ID if the id given is not between 1 and 100. My compiler won't let me use return==false/true to accomplish this, in a do/while statement. Suggestions appreciated, as I said, I learn more from your example codes than I do from my text.
I'd suggest to use while loop here:
1
2
3
4
5
    while( empID >100 || empID <1 ) // You need logical or ( || ) here
      {
        cout << "Invalid entry. Please enter employee ID " << endl;
        cin >> empID; // let user enter empID again
      }

modify the second loop in this way.


Last edited on
What does || do?
Thanks Null. I had an error but I realized it was because I was putting the conditions of the while statement in reverse order from the way it checks (since while checks before running the loop).
So how do I make a loop statement work for an alphabetic entry? I need the user to enter a letter (A/a) and to require reentry for all other characters... but the compiler doesn't like me using while with char. "ISO C++ forbids comparison between pointer and integer". Any thoughts?
A character is no pointer, you probably attempted to compare it with a char* instead. I'll have to ask why you'd want to compare a character and an integer in this situation though.
So how do I make a loop statement work for an alphabetic entry? I need the user to enter a letter (A/a)

Perhaps a loop such as the following:
cin >> letter
while(letter doesnt't equal a or A)
{
prompt user that there was an invalid entry
reenter letter
}
Last edited on
@Hanst

The assignment calls for the user to have to enter a payroll type, a or A. One of my declared variables is char payrollType;. The letter won't ever mean anything, but that's how it goes. For anything besides a/A there should be an error message requesting another attempt.
Here's what I've got so far:
1
2
3
4
5
6
7
8
9
10
11
12
13

    cin >> payrollType;

    while( payrollType == "H" || payrollType == "h" )
      {
        cout << "Enter hours worked " << endl;
      }  

    while ( payrollType != "H" || payrollType != "h" )
      {
        cout << "Invalid entry. Please enter payroll type " << endl;
        cin >> payrollType;
      }


It's this pointer/integer issue. *confused*
Is this a problem with how I have it declared as a variable? Any variable that requires a letter to be entered needs to be listed as char, no? Or is it that while doesn't work with char?
Last edited on
For characters, use single quotes (' '), not double quotes (" "). Double quotes are for string literals (const char*).
For characters, use single quotes (' '), not double quotes (" "). Double quotes are for string literals (const char*).
I got it to work! The single quotes got me part way, then as usual I just had to fiddle with it. DevC++ is a fussy compiler, I guess.

1
2
3
4
5
6
7
    while ( payrollType != 'h' && payrollType != 'H'){
                  cout << "Invalid entry. Please enter payroll type " << endl;
                  cin >> payrollType;
                  }
    
    cout << "Enter hours worked " << endl;
    cin >> hoursWorked;



:)
Last edited on
(Oh god, DevC++! Look up gcc, or if your using Windows, Visual Studios 2010 has a much "prettier" IDE.)
Topic archived. No new replies allowed.