### Loop Help.

This is my first time writing a loop for a program and I cannot get an output I want so that my following function works. I have to write a loop that reads the amount of hours an employee works each day for five days. I am able to run the loop, have the inputs put in, and it gives a total. However, when I go to test the gross payment, it says that the total gross pay is "Inf". I don't really know how to fix it, even after all the research I did.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150`` ``````#include #include using namespace std; string readEmployeesName() { //Declarations string employeesName; //Intializations //Input cout << "Please enter the employee's name: "; cin >> employeesName; //Output //Prologue return employeesName; } double readHourlyPayRate() { //Declaration double hourlyPayRate; //Intializations //Input cout << "Please enter the hourly pay rate of employee: "; cin >> hourlyPayRate; //Process/Calculations //Output //Prologue return hourlyPayRate; } double readHoursWorked() { //Declaration double hoursWorked; double employeesName; double days; double totalHoursWorked; //Intializations //Input while(days <= 5) { cout << "Please enter the amount of hours worked by employee on day" << days << ":" << endl; cin >> hoursWorked; days++; totalHoursWorked = totalHoursWorked+hoursWorked; } //Process/Calculations //Output //Prologue return totalHoursWorked; } double calculateGrossPay() { //Declaration double grossPay; double totalHoursWorked; double hourlyPayRate; //Initializations grossPay = 0.0; //Input //Process/Calculations grossPay = hourlyPayRate*totalHoursWorked; //Output cout << "Total Gross Pay: " << grossPay << endl; //Prologue return grossPay; } int main() { //Declarations string employeesName; double hourlyPayRate; double hours; double hoursWorked; double grossPay; double stateTax; double federalTax; double FICA; double totalWithholding; double netPay; //Initializations hourlyPayRate = 0.0; hours = 0.0; hoursWorked = 0.0; grossPay = 0.0; stateTax = 0.0; federalTax = 0.0; FICA = 0.0; totalWithholding = 0.0; netPay = 0.0; //Input employeesName = readEmployeesName(); while (employeesName!="done") { hourlyPayRate = readHourlyPayRate(); hoursWorked = readHoursWorked(); cout << "In main before calculateGrossPay: " << grossPay << endl; grossPay = calculateGrossPay(); cout << "In main after calculateGrossPay: " << grossPay << endl; stateTax = calculateStateTax(); federalTax = calculateFederalTax(); FICA = calculateFICA(); totalWithholding = calculateTotalWithholding(); netPay = calculateNetPay(); displayResults(); employeesName = readEmployeesName(); } //Output //Prologue return 0; } ``````
grossPay is declared locally inside calculateGrossPay(), so you cant cout it in main().

try cout<<calculateGrossPay, I think this is what you want.

Otherwise, you could declare grossPay globally and change calulateGrossPay to a void function which assigns grossPay inside.
Well, you can re-write calculateGrossPay() to take two values, one for hourlyPayRate and one for hoursWorked.
Topic archived. No new replies allowed.