Question

Why is my code not calculating the pay and not printing entire data at the end? // // main.cpp // Project 14 // // Created by Esmeralda Martinez on 5/13/19. // Copyright © 2019 Esmeralda Martinez. All...

Why is my code not calculating the pay and not printing entire data at the end?

//

// main.cpp

// Project 14

//

// Created by Esmeralda Martinez on 5/13/19.

// Copyright © 2019 Esmeralda Martinez. All rights reserved.

//

#include<iostream>

#include<fstream>

#include<cstdlib>

#include<regex>

#include <iomanip>

using namespace std;

float grossPay(float hrsWorked, float payrate);

float grossPay(float hrsWorked, float payrate){

return hrsWorked*payrate;

  

  

}

int main(){

  

//opening an output file

ifstream outFile("Employee.txt", ios::in);

//Read variables

string depId,emp_num,firstName,lastName,email,hrs_worked,pay_rate,ch="y";

  

//Regex patterns

regex integerPattern("(\\+|-)?[[:digit:]]+");

regex stringPattern("^[a-zA-Z]{1,20}$");

regex emailPattern("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+");

regex floatPattern("[0-9]*\\.[0-9]+|[0-9]+");

  

cout << "Please select 'y' to begin, anything else to exit"<<endl;

cin >> ch;

  

while(ch=="y" || ch=="Y"){

  

cout << "Enter department ID number: ";

cin >> depId;

while (!regex_match(depId, integerPattern)) {

cout << "Department ID is incorrect!!Only numbers allowed" << endl;

cout << "Enter department number: ";

cin >> depId;

}

//Prompt for employee number and error check

cout << "Enter employee number: ";

cin >> emp_num;

while (!regex_match(emp_num,integerPattern)) {

cout << "Employee ID is incorrect!!Only numbers allowed" << endl;

cout << "Enter employee number: ";

cin >> emp_num;

}

//Prompt for first name and error check

cout << "Enter first name: ";

cin>>firstName;

if (!regex_match(firstName, stringPattern)) {

firstName = firstName.substr(0, 20);

}

//Prompt for first name and error check

cout << "Enter last name: ";

cin >> lastName;

if (!regex_match(lastName, stringPattern)) {

lastName = lastName.substr(0, 20);

}

//Prompt for email and error check

cout << "Enter email address: ";

cin >> email;

while (!regex_match(email, emailPattern) ){

cout << " Please enter valid email address" << endl;

cout << "Enter email address: ";

cin >> email;

}

//Promtp for hours worked and error check

cout << "Enter hours worked:";

cin >> hrs_worked;

while (!regex_match(hrs_worked, floatPattern)) {

cout << "Incorrect value, please re enter " << endl;

cout << "Enter hours worked: ";

cin >> hrs_worked;

  

}

  

//Promtp for pay rate and error check

cout << "Enter hourly pay rate:";

cin >>pay_rate;

while (!regex_match(pay_rate, floatPattern)) {

cout << "Incorrect value, please re enter" << endl;

cout << "Enter hours pay rate:";

cin >> pay_rate;

}

}

//convert into fload

float payrate = stof(pay_rate);

float hrsWorked = stof(hrs_worked);

  

//call to function

grossPay(hrsWorked, payrate);

if(hrsWorked>40){

return (payrate*40)+(hrsWorked-40)*payrate*1.5;

  

  

  

  

  

//display menu

cout<<"Employee ID: "<<emp_num<<" Employee's Name: "<<lastName<<" , "<<firstName<<" Email Address: "<<email<<" Regular hours worked: "<<hrs_worked<<" Overtime hours worked: "<<hrsWorked-40<<" Current Pay Rate "<<&rand<<" GrossPay: "<<&grossPay<<endl;

  

//Prompt to continue or end

cout<<endl<<endl<<" Would you like to review more files? (y/n) ";

cin>>ch;

  

}

return 0;

}


My program is supposed to calculate pay and then print out the calculation along with the user input. At this moment, however, my program ends after gathering the data.
How can I fix that?
0 0
Add a comment Improve this question Transcribed image text
Answer #1

if(hrsWorked>40){

return (payrate*40)+(hrsWorked-40)*payrate*1.5;

There is a problem with the above return statement. I also suppose there must be compilation errors in this program too as the brace of if condition isn't closed.

Anyways I see that you have already defined the function grossPay right in the beginning. Modify it in the following way..

float grossPay(float hrsWorked, float payrate){

if(hrsWorked>40){

return (payrate*40)+(hrsWorked-40)*payrate*1.5;

}

else {

return hrsWorked*payrate;

}

Remove the below piece of code.

if(hrsWorked>40){

return (payrate*40)+(hrsWorked-40)*payrate*1.5;

Because when you call the grossPay function, it checks for the hrsWorked>40. If it is true, then, it returns (payrate*40)+(hrsWorked-40)*payrate*1.5; otherwise it returns hrsWorked*payrate;

Do get in touch after making the above modifications. For some reasons I couldn't compile the code. Probably the above change should fix the issue.

Add a comment
Know the answer?
Add Answer to:
Why is my code not calculating the pay and not printing entire data at the end? // // main.cpp // Project 14 // // Created by Esmeralda Martinez on 5/13/19. // Copyright © 2019 Esmeralda Martinez. All...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • i am having trouble displaying results and displaying them evenly it is suppose to look like...

    i am having trouble displaying results and displaying them evenly it is suppose to look like this Enter the following data for employee 1: Employee ID: 1298 Hours worked: 35.8 Pay rate (per hour): 23.45 Enter the following data for employee 2: Employee ID: 1899 Hours worked: 34.5 Pay rate (per hour): 19.5 Enter the following data for employee 3: Employee ID: 4435 Hours worked: 30.5 Pay rate (per hour): 20.75 Enter the following data for employee 4: Employee ID:...

  • Hey, i was just wondering how i would calculate over time pay in c++ visual studio...

    Hey, i was just wondering how i would calculate over time pay in c++ visual studio 2017? what i have right now is either returning 0 for some reason or being skipped over? im also wondering about the federal tax as well because that also doesn't seem to work, any help is greatly appreciated! these are my variables char chChoice = ' '; int intempID = 0; int intHours = 0; int intOThours = 0; float flOTrate = 0; float...

  • I need to get this two last parts of my project done by tonight. If you...

    I need to get this two last parts of my project done by tonight. If you see something wrong with the current code feel free to fix it. Thank you! Project 3 Description In this project, use project 1 and 2 as a starting point and complete the following tasks. Create a C++ project for a daycare. In this project, create a class called child which is defined as follows: private members: string First name string Last name integer Child...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT