Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

i get this error message when i press the enter key without any input, and when

ID: 3679046 • Letter: I

Question

i get this error message when i press the enter key without any input, and when i enter a letter or something like that twice. i need this to not crash when these things happen, but to give an error message. i need this to give an error message for anything that isnt a number between 0 and 40, and the same thing for validating the pay rate. here is the code:

#include <iostream>

#include <iomanip>

#include <string>

#include <iomanip>

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& str1, int& userNumber);

void validatePayRate(string& str1, double& userNumber);

int main()

{

// declare Array of employee ID numbers

int empID[] = {1, 2, 3, 4, 5, 6, 7};

  

// declare Array to hold the hours worked for each employee

int hours[EMPLOYEE_SIZE];

  

// declare Array to hold the hourly pay rate for each employee

double payRate[EMPLOYEE_SIZE];

  

// declare Array to hold the gross wages for each employee

double wages[EMPLOYEE_SIZE];

  

  

cout << "Enter the requasted information for each employee. ";

  

  

//DEBUG

getEmployeeInfo(empID, hours, payRate, wages, EMPLOYEE_SIZE);

displayWages(empID, wages, EMPLOYEE_SIZE);

  

  

  

  

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)

{

for(int i = 0; i < EMPLOYEE_SIZE; i++)

{

string str1; // This is what the user will input, we will convert it into an int in the function

int userNumberHours = 0;

double userNumberPay = 0;

double userNumberWages = 0;

  

cout << " Employee # " << empId[i] << endl << " Hours worked per week: ";

getline(cin, str1);

validateHoursWorked(str1, userNumberHours);

hours[i] = userNumberHours;

  

cout << " Hourly pay rate: $";

getline(cin, str1);

validatePayRate(str1, userNumberPay);

payRate[i] = userNumberPay;

  

userNumberWages = userNumberHours * userNumberPay;

wages[i] = userNumberWages;

  

}

  

}

// ********************************************************

// 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)

{

cout << "------------------------------- " << "Employee Wages" << endl << "------------------------------- ";

for(int i = 0; i < EMPLOYEE_SIZE; i++)

{

cout << "Employee #" << empId[i] << " $" << fixed << setprecision(2) << wages[i] << endl;

}

}

// ********************************************************

//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& str1, int& userNumber)

{

bool numberNotValid = true;

  

while(numberNotValid == true)

{

for(int i = 0; i < str1.length(); i++)

{

if((str1[i] != '0' && str1[i] != '1' && str1[i] != '2' && str1[i] != '3' && str1[i] != '4' && str1[i] != '5' && str1[i] != '6' && str1[i] != '7' && str1[i] != '8' && str1[i] != '9')) // FIX: EMPTY ENTER LETS IT THROUGH

{

i = 0;

cout << " Hours worked must be an integer greater than 0 and less than or equal to 40: ";

cin.clear();

getline(cin, str1);

}

}

  

// Should be a working number for stoi, may not be valid number though for validation

  

userNumber = stoi(str1);

  

if(userNumber > 40 || userNumber <= 0)

{

str1 = "Invalid.";

numberNotValid = true;

}

else

numberNotValid = false;

}

  

}

// ********************************************************

//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& str1, double& userNumber)

{

//hint: just as there is an stoi, theres is an stof (string to float) and an stod (string to double)

  

bool numberNotValid = true;

  

int periodCounter = 0;

int decimalCounter = 0;

  

while(numberNotValid == true)

{

for(int i = 0; i < str1.length(); i++) // FIX IT. BREAKS HORRIBLY IF EMPTY ENTER.

{

if(str1[i] == '.')

{

periodCounter++;

decimalCounter = str1.length() - 1 - i;

}

  

if((str1[i] < '0' && str1[i] != '.') || (str1[i] > '9' && str1[i] != '.') || periodCounter > 1 || decimalCounter > 2) // FIX: EMPTY ENTER LETS IT THROUGH

{

periodCounter = 0;

decimalCounter = 0;

i = 0;

cout << " Pay rate must be greater than $5 but less than or equal to $15: ";

cin.clear();

getline(cin, str1);

}

}

  

userNumber = stod(str1);

  

if(userNumber > 15 || userNumber < 5)

{

str1 = "Invalid.";

numberNotValid = true;

}

else

numberNotValid = false;

}

  

}

Enter the requasted information for each employee Employee # 1 Hours worked per week: q Hours worked must be an integer greater than and less than or equal to 40: libc++abi.dylib: terminating with uncaught exception of type std::invalid_argument: stoi: no conversion (lldb)

Explanation / Answer

Hi, I have fixed your code.

Please let me know in case of any issue.


#include <iostream>

#include <iomanip>

#include <string>

#include <iomanip>

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& str1, int& userNumber);

void validatePayRate(string& str1, double& userNumber);

int main()

{

    // declare Array of employee ID numbers

    int empID[] = {1, 2, 3, 4, 5, 6, 7};

  

    // declare Array to hold the hours worked for each employee

    int hours[EMPLOYEE_SIZE];

  

    // declare Array to hold the hourly pay rate for each employee

    double payRate[EMPLOYEE_SIZE];

  

    // declare Array to hold the gross wages for each employee

    double wages[EMPLOYEE_SIZE];

  

  

cout << "Enter the requasted information for each employee. ";

  

  

    //DEBUG

    getEmployeeInfo(empID, hours, payRate, wages, EMPLOYEE_SIZE);

    displayWages(empID, wages, EMPLOYEE_SIZE);

  

  

  

  

    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)

{

    for(int i = 0; i < EMPLOYEE_SIZE; i++)

    {

        string str1; // This is what the user will input, we will convert it into an int in the function

        int userNumberHours = 0;

        double userNumberPay = 0;

        double userNumberWages = 0;

      

        cout << " Employee # " << empId[i] << endl << "     Hours worked per week: ";

        getline(cin, str1);

        validateHoursWorked(str1, userNumberHours);

        hours[i] = userNumberHours;

      

        cout << "     Hourly pay rate: $";

        getline(cin, str1);

        validatePayRate(str1, userNumberPay);

        payRate[i] = userNumberPay;

      

        userNumberWages = userNumberHours * userNumberPay;

        wages[i] = userNumberWages;

      

    }

  

}

// ********************************************************

// 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)

{

    cout << "------------------------------- " << "Employee                  Wages" << endl << "------------------------------- ";

    for(int i = 0; i < EMPLOYEE_SIZE; i++)

    {

        cout << "Employee #" << empId[i] << "          $" << fixed << setprecision(2) << wages[i] << endl;

    }

}

// ********************************************************

//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& str1, int& userNumber)

{

    bool numberNotValid = true;

  

    while(numberNotValid == true)

    {

        for(int i = 0; i < str1.length();)

        {

            if((str1[i] != '0' && str1[i] != '1' && str1[i] != '2' && str1[i] != '3' && str1[i] != '4' && str1[i] != '5' && str1[i] != '6' && str1[i] != '7' && str1[i] != '8' && str1[i] != '9')) // FIX: EMPTY ENTER LETS IT THROUGH

            {

                i = 0;

                cout << " Hours worked must be an integer greater than 0 and less than or equal to 40: ";

                cin.clear();

                getline(cin, str1);
                continue;

            }
          
               i++;
        }

  

        // Should be a working number for stoi, may not be valid number though for validation

  

        userNumber = stoi(str1);

  

        if(userNumber > 40 || userNumber <= 0)

        {

            str1 = "Invalid.";

            numberNotValid = true;

        }

        else

            numberNotValid = false;

    }

  

}

// ********************************************************

//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& str1, double& userNumber)

{

    //hint: just as there is an stoi, theres is an stof (string to float) and an stod (string to double)

  

    bool numberNotValid = true;

  

    int periodCounter = 0;

    int decimalCounter = 0;

  

    while(numberNotValid == true)

    {

        for(int i = 0; i < str1.length();) // FIX IT. BREAKS HORRIBLY IF EMPTY ENTER.

        {

            if(str1[i] == '.')

            {

                periodCounter++;

                decimalCounter = str1.length() - 1 - i;

            }

          

            if((str1[i] < '0' && str1[i] != '.') || (str1[i] > '9' && str1[i] != '.') || periodCounter > 1 || decimalCounter > 2) // FIX: EMPTY ENTER LETS IT THROUGH

            {

                periodCounter = 0;

                decimalCounter = 0;

                i = 0;

                cout << " Pay rate must be greater than $5 but less than or equal to $15: ";

                cin.clear();

                getline(cin, str1);
                continue;

            }
            i++;

        }

      

        userNumber = stod(str1);

      

        if(userNumber > 15 || userNumber < 5)

        {

            str1 = "Invalid.";

            numberNotValid = true;

        }

        else

            numberNotValid = false;

    }

  

}