Как генерировать вывод, как указано ниже? - PullRequest
0 голосов
/ 15 апреля 2019

Я написал код ниже. Сначала он проверяет ввод пользователя, но снова не спрашивает «Введите код сотрудника (1,2,3,4,5)», если условие ложно.

Как я могу сгенерировать вывод как изображение ниже?

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);
  }

Выходное ожидание:

Требуемый выход

Как я могу проверить все данные, введенные пользователем, номер погоды или символ?

1 Ответ

0 голосов
/ 04 мая 2019

Просто не проверяйте, является ли ввод int.Затем в переключателе выполните: (обратите внимание на кавычки для символов)

    switch '1':
    switch 'a': /* Or whatever char input you want to work lke 1*/
      /* Do stuff */
      break;/* important */
    switch '2':
    /* continue */
Это будет делать то же самое с 'a', как с '1'.
...