-4

I have written the code below. It first checks the user's input, but again it doesn't ask "Enter employee's number code(1,2,3,4,5)" if condition is false.

How can I generate output as the below image?

Expected output

 #include <stdio.h>
 #include <ctype.h>
 #include <string.h>
 #include <stdlib.h>

int main (void)
{ 

char  code;


    printf ("\nEnter employee's number code (1,2,3,4,5): ");
    scanf ("%c", &code);

 //Check whether a Character Entered by User is Alphabet or not

if (isalpha(code) == 0)
    {
    //printf("%c is an number.\n", code);
    int x = code - '0'; // convert alpha char to number
    fun(x);
    //printf("%d is an number.\n", x);
    }
else
     printf( "Unrecognised paycode. Please only enter  '1' for manager, '2' hourly, '3'  commission, '4' Pieceworker or '' \n");

return 0;
  }

void fun(int code) 
   { 
         float total = 0;
       float pay, sales, hours, salary, hourlyTotal, comm_total, pice_total;
     float item_a, item_b, item_c;
     float item_1, item_2, item_3;
    char s;

while (code != -1)
{

    switch (code)
    {
        case 1:  

            printf ("Enter the manager's paycode: ");
            scanf ("%f", &pay);


            printf ("Manager salary of:$%.2f\n\n", pay);


            total += pay;

            salary = total;
            break;

        case 2: 
            printf ("Enter hourly worker's pay rate: ");
            scanf ("%f", &pay);


            printf ("Enter the number of hours worked: ");
            scanf ("%f", &hours);


            if (hours > 40)
                pay = (pay * 40) + ((hours - 40) * (pay * 1.5));
            else
                pay = pay * hours;


            printf ("Wages are :$%.2f  \t <$%.2f regular and $0.00 overtime> \n\n", pay, pay);


            total += pay;

    hourlyTotal = pay;

            break;

        case 3: 
            printf ("Enter commission employee's gross item_a sales: ");
            scanf ("%f", &item_a);
            item_a = item_a * .057;

            printf ("Enter commission employee's gross item_b sales: ");
            scanf ("%f", &item_b);
            item_b = item_b * .064;


            printf ("Enter commission employee's gross item_c sales: ");
            scanf ("%f", &item_c);
            item_c = item_c * .072;


            sales = item_a +item_b+item_c;


            pay = 250 + sales;


            printf ("Commsion wage is:$%.2f  <$250.00 base + $%.2f commission <$%.2f item A, $%.2f item B, $%.2f item C> \n\n", pay, sales, item_a, item_b, item_c);


            total += pay;

    comm_total = pay;

            break;

        case 4: 
            printf ("\nEnter the number of item_1 completed: ");
            scanf ("%f", &item_1);
            item_1 = item_1*22.50;

            printf ("\nEnter the number of item_2 completed: ");
            scanf ("%f", &item_2);
            item_2 = item_2*24.50;



            printf ("\nEnter the number of item_3 completed: ");
            scanf("%cf", &item_3);
            //if (isdigit(s)) 
            //printf("%c number \n", s);
                //item_3 = s;
            item_3 = item_3*26.00;





            pay = item_1 + item_2 + item_3;


            printf ("Pieceworker wage is:$%.2f <item_1 $%.2f, item_2 $%.2f, item_3 $%.2f> \n\n", pay,item_1, item_2,item_3);


            total += pay;

    pice_total = pay;

            break;

  case 5: 
            printf ("\n Manager: Employees : 1 Total wages:$%.2f ", salary);
    printf ("\n Hourly: Employees : 1 Total wages:$%.2f ", hourlyTotal);
    printf ("\n Commission: Employees : 1 Total wages:$%.2f ", comm_total);
    printf ("\n Pieceworker: Employees : 1 Total wages:$%.2f ", pice_total);
    printf ("\n Net Total: Employees : 4 Total wages:$%.2f ", total);

            break;

        default: 
            printf ("You have entered an invalid code.\n");
    }


    printf ("\nEnter employee's number code (1,2,3,4,5): ");
    scanf ("%d", &code);
}


printf ("\nThe total payroll for the week is: %.2f\n", total);
  }

Output Expectation:

Required Output

How can I check all user input data weather number or char?

Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
  • 3
    This code is really hard to follow. Can you give your functions meaningful names? "fun" is not exactly self-explanatory. You also need to break this up more into smaller, more easily understood components. – tadman Apr 15 '19 at 16:15
  • 2
    Instead of linking screenshot images you should show the expected and actual output as text. – Bodo Apr 15 '19 at 16:28
  • yes, but little help make me motivate.. – Sital Mandal Apr 15 '19 at 16:37
  • 1
    There is no bug that ought to be causing you not to get another "Enter employee's number code" prompt. The only bug I can see is in your "scanf("%cf", &item_3)" line, which should just be "%f". You have a logic error in that you don't verify that the input employee's number is a digit after the first time. You would be much better served to prompt and get information inside a loop in fun() than doing it twice. – One Guy Hacking Apr 15 '19 at 18:42

1 Answers1

0

Simply do not check if input is int. Then, in switch, do: (note the quotes for chars)

    switch '1':
    switch 'a': /* Or whatever char input you want to work lke 1*/
      /* Do stuff */
      break;/* important */
    switch '2':
    /* continue */
This will do the same stuff with 'a' as with '1'.
AP3X
  • 1
  • 1