2

Use for loops to process the data for 5 employees.
One loop to load the data
One loop to print the output.
In for loops, if any of the entered fields are –1, break out of the loop immediately after getting -1
Update the output as shown in the sample data.
Use arrays to store the user input.

I have a problem with breaking out of the loop. I think the system is reading it as an integer, not a character.

I also have a problem with looping the output. It is not looping the number of times the user has entered the info.

#include <stdio.h>

int main()
{
   char name[5][10];
   float hourswork[10];
   float hourspay[10];
   float grosspay[10];
   float taxes[10];
   float netpay[10];
   int i = 0;

   for (i = 0; i < 5; i++)
   {
      printf("Hello, enter your name: \n");
      scanf("%s", &name[i][10]);

      printf("Enter your hourly wage: \n");
      scanf("%f", &hourspay[i]);

      printf("Enter the hours you have worked \n");
      scanf("%f", &hourswork[i]);

      if (hourswork[i] <= 40)
      {
         grosspay[i] = hourspay[i] * hourswork[i];
      }
      else if (hourswork[i] > 40)
      {
         grosspay[i] = (40 * hourspay[i]) + (((hourswork[i] - 40) * 1.5) * hourspay[i]);
      }

      taxes[i] = grosspay[i] * 0.2;
    
      if (name[i][10] == -1||hourspay[i] == -1||hourswork[i] == -1)
      {
         break;
      }
   }
    
   for (i = 0; i < 5; i++)
   {
      printf("\n%c's Pay:\n", name[i][10]);

      printf("Hourly Rate: %.2f\n", hourspay[i]);

      printf("Hours Worked: %.2f\n", hourswork[i]);
      printf("Gross Pay: %.2f\n", grosspay[i]);
      printf("Taxes: %.2f\n", taxes[i]);

      netpay[i] = grosspay[i] - taxes[i];

      printf("Net Pay: %.2f\n\n", netpay[i]);
   }
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    You are going out of bounds with your `name` array with `name[i][10]`. The 2nd dimension's index value must be _less than_ 10. – jkb Jul 14 '22 at 23:01
  • 1
    Nothing about this code is C++, only C, so I have removed the `c++` tag. Also, see [How do I ask and answer homework questions?](https://meta.stackoverflow.com/questions/334822/) – Remy Lebeau Jul 14 '22 at 23:09
  • 1
    `if (scanf ("%9s", name[i]) != 1) { /* handle error */ }`?? You must use the *field-width* modifier when reading into a character array to protect your array bounds (including space for the nul-terminating character). Otherwise, your use of `scanf()` is no safer than `gets()`. See [Why gets() is so dangerous it should never be used!](https://stackoverflow.com/q/1694036/3422102) You must validate the return of `scanf()` to ensure the input succeeded (especially where numeric conversions are involved and a *matching failure* is possible) – David C. Rankin Jul 15 '22 at 03:37

0 Answers0