Please help with my homework, having trouble with validating information

So I written this code for an assignment with some restrictions which also offered a template, but im having trouble with the validation. Specifically the void functions validateHoursWorked and validatePayRate. Problem is that I have to write code that prevents anything other than numbers from being entered or any blanks. Any tips on how to work on this?

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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

// Constant for the array size.
const int EMPLOYEE_SIZE = 7;

// Function Prototypes
void getEmployeeInfo(int empId[], int hours[], double payRate[], double wages[], int EMPLOYEE_SIZE);
void displayWages(int empId[], double wages[], int EMPLOYEE_SIZE);
void validateHoursWorked(string hoursWorked, int& userNumber);
void validatePayRate(string payRate, double& userNumber);

int main()
{


	// declare Array of employee ID numbers
	
                             
	// declare Array to hold the hours worked for each employee
	
   
	// declare Array to hold the hourly pay rate for each employee
	
   
	// declare Array to hold the gross wages for each employee
	

	// Get the employee payroll information and store it in the arrays.
	// call the function getEmployeeInfo
	
   
	// Display the payroll information.
	// call the function displayWages

	system("pause");
	return 0;
}

//function defitions

// ********************************************************
// The getEmployeeInfo function receives four parallel    *
// arrays as arguments. The 1st array contains employee   *
// IDs to be displayed in prompts. It asks for input and  *
// stores hours worked and pay rate information in the    *
// 2nd and 3rd arrays. This information is used to        *
// calculate gross pay, which it stores in the 4th array. *
// ********************************************************
void getEmployeeInfo(int empId[], int hours[], double payRate[], double wages[], int EMPLOYEE_SIZE)
{
	
}

// ********************************************************
// The displayWages function receives 2 parallel arrays.  *
// The first holds employee IDs and the second holds      *
// employee gross pay. The function displays this         *
// information for each employee.                         *
// ********************************************************
void displayWages(int empId[], double wages[], int EMPLOYEE_SIZE)
{
	
}

// ********************************************************
//The validateHoursWorked fuction receives a string and   *
//returns an integer.  The hours worked must be greater   *
//than 0 but less than or equal to 40                     *
// ********************************************************
//=================================================
void validateHoursWorked(string hoursWorked, int& userNumber)
{
	
}

// ********************************************************
//The validatePayRate fuction receives a string and   	  *
//returns a double.  The pay rate must be greater   	  *
//than or equal to 5 but less than or equal to 15         *
// ********************************************************
void validatePayRate(string payRate, double& userNumber)
{
//hint: just as there is an stoi, theres is an stof (string to float) and an stod (string to double)

}
Perhaps something like this.
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
$ cat main.cpp
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;

int main ( ) {
    string input;
    while ( getline(cin,input) ) {
        cout << "Entered: " << input << endl;
        istringstream is(input);
        int value;
        if ( is >> value ) {
            if ( value >= 0 && value <= 10 ) {
                cout << "Good integer" << endl;
            } else {
                cout << "Out of range" << endl;
            }
        } else {
            cout << "Garbage" << endl;
        }
    }
}
$ ./a.out 
hello
Entered: hello
Garbage
123
Entered: 123
Out of range
2
Entered: 2
Good integer
       4
Entered:        4
Good integer

Note that leading spaces are still considered valid using this approach.
Which may or may not be a problem for you.

Here another possibility, which outsources the code to a function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int get_number()
{
    string tmp;
    int num;
    while(getline(cin, tmp))
    {
        try{
            num = stoi(tmp);
            break;
        }
        catch (invalid_argument & )
        {
            continue;
        }
    }
    return num;
}

I hope you will be versed for changing this code piece to your needs (if you want only positive numbers or a distinct set of numbers reading from other than cin).
Last edited on
Topic archived. No new replies allowed.