I have searched for a solution and understand that dislike of the "gets" command; however, I have to do this for a homework assignment.
My issue is that I have to write a program that ask users for name of employee as well as hourly rate and hours worked, then calculate using functions a number of items. I have all of this done except that I can't figure out the user input for FIRST employee.
I had a solution using scanf (" %[^\n]", emp_name[e]); that worked but is not allowed. I fixed what I originally did by adding a getchar() to end of my for loop and that will give me the separate lines for employee name and hourly rate for the 2nd - 10th employee.
So I just don't know what do for the 1st employee - I tried adding fflush statements before, adding space after scanf statement for # of employees.
It will work for the other employees - it just skips that first employee.
Enter the number of salaries to post (1-10): 2
Enter the name of employee 1: Enter the number of hours for : 10
Enter the hourly rate for : 10
Enter the name of employee 2: Debbie
Enter the number of hours for Debbie: 10
Enter the hourly rate for Debbie: 10
I attached the portion of my file that is regards to this problem, but here's the code as well.
Code:
do
{
printf("Enter the number of salaries to post (1-10): ");
scanf("%i" , &numberEmployees);
fflush(stdin);
if( numberEmployees < 1 )
printf("ERROR! - must post at least 1 salary!\n\n");
if( numberEmployees > 10 )
printf("ERROR! - cannot post more than max (10)!\n\n");
} while( numberEmployees < 1 || numberEmployees > 10 );
printf ("\n");
for (e = 0; e < numberEmployees; ++e)
{
printf("Enter the name of employee %i: ",e+1);
gets(emp_name[e]);
/* Input for hours must be greater than 0 */
do
{
printf("Enter the number of hours for %s: ", emp_name[e]);
scanf("%f", &hours_worked);
fflush(stdin);
if ( hours_worked < 1 )
printf ("ERROR! - number of hours must be greater than 0\n");
} while ( hours_worked < 1 );
/* Input for hourly rate must be greater than 0 */
do
{
printf("Enter the hourly rate for %s: ", emp_name[e]);
scanf("%f", &hourly_rate);
fflush(stdin);
if( hourly_rate < 1 )
printf ("ERROR! - hourly rate must be greater than 0\n");
} while( hourly_rate < 1 );
getchar();
}
Thanks for any suggestions!