Click here to Skip to main content
15,910,661 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am writing a program to calculate the amount of employees needed per hour based on the amount of customers. There must be at least 3 employees at all times, but also 1 additional employee per 20 customers. I wrote this with arrays and for loops, but after I enter all my values of customers per hour (I have just been using 9, 10, 19, 20, 29, 30.... for number customers) and try to print the results it breaks. I am not sure why.... please help. THANK YOU


C++
#include <iostream>
#include <iomanip>
using namespace std;


int main()
{

// Program to calculate how many employees are needed during the day based on customers

string hours[24] = {"0000-0100", "0100-0200", "0200-0300", "0300-0400", "0400-0500", "0500-0600", "0600-0700", "0700-0800", "0800-0900", "0900-1000", "1000-1100", "1100-1200", "1200-1300", "1300-1400", "1400-1500", "1500-1600", "1600-1700", "1700-1800", "1800-1900", "1900-2000", "2000-2100", "2100-2200", "2200-2300", "2300-2400"};

	int customers[24], employees[24],i;
	
	cout << "Workforce Calculator" << endl << endl;
	
	//for loop to gather information and calculate employees
	for (i=0; i <= 24; i++)
	{

cout << "Please enter the amount of customers for the time period of " << hours[i] << ":" << endl;
		cin >> customers[i];
		employees[i] = 3 + customers[i] / 20;

	}


	//for loop to print results
	for (i=0; i <= 24; i++)
	{

cout << "The amount of employees needed to cover certain time periods is as fallows:"<< endl << endl;
cout << "For the timme period of "<< hours[i] << " you will need " << employees[i] << " employees." << endl;

	}

}
Posted
Updated 4-Jun-12 8:16am
v3

1 solution

Both of your loop run to 24 inclusive, but should run only to 23.

for (i=0; i < 24; i++)
 
Share this answer
 
Comments
BillW33 5-Jun-12 9:52am    
Good work spotting the error, +5 :)
nv3 5-Jun-12 10:03am    
Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900