string length validation?

I'm writing a payroll program. One of the entries is an Employee ID# that should be 6 characters long (ex: ABC123). I researched online and found a strlen validation that I thought I could try, but I can't get it to work. I'm getting this: "invalid conversions from char to const char" on the compiler. Any help would be great!

Oh and yes I'm going to try to save entries to arrays and write them to files eventually.

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
28
29
30
31
32
33
34
#include <cstdlib>
#include <iostream>
#include <string>
#include <iomanip>
#include <istream>

using namespace std;

int main()
{
    const double STATETAX = .05;
    const double FEDTAX = .15;
    const double UNIONFEES = .02;
    const double OTRATE = 1.5;
    char name[45], id[10];
    int idx = 0;
    double rate[10];
    
    
    cout << "Enter the following employee info." << endl;
    cout << "First, Middle and Last name: ";
    cin.getline(name,45);
    
    do //employee ID entry with input validation
    {
    cout << "Employee ID#: ";
    cin >> id[idx];
    if (strlen(id[idx]) == 6)
         break;
    else
         cout << "Invalid employee ID#. Must be 6 characters. Try again.";
    } while (1);

Last edited on
Line 27/28:
1
2
    cin >> id;
    if (strlen(id) == 6)
id[10] is an array of characters.
cin >> id[idx] is reading a single character into the array.
strlen(id[idx]) is attempting to pass a single character to the strlen function. That doesn't make sense to the compiler. Nor to us, as we know the length of a single character character is one.

How about using a std::string for this purpose.

1
2
3
4
5
    string id;

    cin >> id;
    if (id.size()  == 6)
         break;
ok. So I can't save that input directly into an array?
ok. So I can't save that input directly into an array?
Sure you can.

But you would need an array of strings, not an array of characters.
1
2
    string arr[10];
    cin >> arr[0];
I changed it to a string array and it seems to work now. Thanks Chervil!

Now I am stuck at the do-while loop for the employee level (lines 51-57). I can't seem to get out of the loop even when I enter valid input (1, 2 or 3). Should I not use a do-while?


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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <cstdlib>
#include <iostream>
#include <string>
#include <iomanip>
#include <istream>
#include <fstream>

using namespace std;

int main()
{
    const double STATETAX = .05;
    const double FEDTAX = .15;
    const double UNIONFEES = .02;
    const double OTRATE = 1.5;
    string id[10];
    char name[45], level[10];
    int idx = 0, hours[10];
    double rate[10], myGross[10], myStateTax[10], myFedTax[10], myUnionFees[10];
    double myNet[10];
    
    ofstream outputFile;
    outputFile.open("E:\\CIS265\\PayrollProgram.txt");
    
    cout << "Enter the following employee info." << endl;
    cout << "First, Middle and Last name: ";
    cin.getline(name,45);
    
    do //employee ID entry with input validation
    {
    cout << "Employee ID#: ";
    cin >> id[idx];
    if (id[idx].size() == 6)
         break;
    else
         cout << "Invalid employee ID#. Must be 6 characters. Try again.\n";
    } while (1);
    
    do //get pay rate with input validation
    {
    cout << "Hourly Rate: $";
    cin >> rate[idx];
    if (rate[idx] > 0)
         break;
    else
         cout << "Invalid pay rate. Must be > $0.00. Try again.\n ";
    } while (1);
    
    do  //enter Level with validation
    {
    cout << "Employee Level (1, 2 or 3): ";
    cin >> level[idx];
    if (level[idx] >= 1 && level[idx] <=3)
         break;
    else
         cout << "Invalid Entry. Level must be 1, 2, or 3. Try again." << endl; 
    } while (1);


    /*
    while (level[idx] == 1 || level ==2)
    {
         cout << "Total Hours Worked: ";
         cin >> hours[idx];
         if (level[idx] == 1)
         {
              if (hours[idx] >= 0 && hours[idx] <= 60)
                   break;
              else if (hours[idx] > 60)
                   cout << "You cannot work more than 60 hours at Level 1." << endl;
              else 
                   cout << "Invalid entry. Must be between 0 - 60.";
         }
         else if (level[idx] == 2)
         {
              if (hours[idx] >= 0 && hours[idx] <= 55)
                   break;
              else if (hours[idx] > 55)
                   cout << "You cannot work more than 55 hours at Level 2." << endl;
              else 
                   cout << "Invalid entry. Must be between 0 - 55.";
         }
    }
    */
    myGross[idx] = rate[idx] * 40 + (hours[idx] - 40) * 1.5;
    myStateTax[idx] = myGross[idx] * STATETAX;
    myFedTax[idx] = myGross[idx] * FEDTAX;
    myUnionFees[idx] = myGross[idx] * UNIONFEES;
    myNet[idx] = myGross[idx] - (myStateTax[idx] + myFedTax[idx] + myUnionFees[idx]);
    
    

    
    system("PAUSE");
    return EXIT_SUCCESS;
}
    
Topic archived. No new replies allowed.