-1

After the first loop, the iteration ignores the "Name of employee #: " and jumps to "Employee's hourly rate: " and "Hours worked: " for the rest of the loop until it finishes. The problem started after I added the "Float" arrays in the "For Loop".

This is the output I am getting:

Name of Employee 1: Alex Employee's hourly rate: 9.00 Hours Worked: 8

Name of employee 2: Employee's hourly rate:9.50 Hours worked: 8

Name of employee 3: Employee's hourly rate:10.00 Hours worked: 8

etc...

    #include <stdio.h>

    int main()
    {   int i;
        char empNames[5][32];
        float empRates[5][10];
        float empHours[5][10];

        for (i = 0; i < 5; i++)
        {
            printf("Name of employee %d: ", i+1);
            gets(empNames[i]);

            printf("Employee's hourly rate: ");
            scanf_s("%f", &empRates);//squiggly green line

            printf("Hours Worked: ");
            scanf_s("%f", &empHours);//squiggly green line
        }
      }

Errors: - Warning C4477 'scanf_s' : format string '%f' requires an argument of type 'float ', but variadic argument 1 has type 'float ()[5][10]'.

-Warning C6272 Non-float passed as argument '2' when float is required in call to 'scanf_s' Actual type: 'float [5][10]'.

-Warning C4013 'gets' undefined; assuming extern returning int.

1 Answers1

0

First : gets is dangerous to use you can use fgets instead check this for more informations :Why is the gets function so dangerous that it should not be used?

Second : empRate[5][10] is a 2D array (Matrix) and you need only a one dimensional array to save your floats each float can fit inside empRate[i](same for empHours)

#include <stdio.h>

int main()
{  
int i;
char empNames[5][32];
float empRates[5];
float empHours[5];

for (i = 0; i < 5; i++)
{
    printf("Name of employee %d: ", i+1);
    scanf("%32[^\n]s",empNames[i]);
    printf("Employee's hourly rate: ");
    scanf("%f", &empRates[i]);

    printf("Hours Worked: ");
    scanf("%f", &empHours[i]);
    fgetc(stdin); // clear the buffer from the new line character
    }
 }
Spinkoo
  • 2,080
  • 1
  • 7
  • 23
  • @spinkoo- Thank you for the help! as for the first line I switched to "scanf_s" instead and it fixed the issue/bugs. printf("Name of employee: "); scanf_s(" %s", &empNames[i], 32); – chessnotcheckers Jul 13 '19 at 23:35