Hi all.
I have written a program to calculate the wage for 4 classes of workers. One on salary, hourly rate, sales Commission, and the final on amount of product made. Each having the corresponding switch input of 1,2,3,4 and Q to display total pay off all workers within each working class. After Q and totals are display the program closes. Now if any other characters are entered the program displays a code and says invalid input please enter 1,2,3,4,Q.... ect. This works when the wrong singular character is entered although If i enter say '11' into switch the second 1 carry into the first statement in the body of case '1' in switch. I logical understand this because double 1 still satisfy s the switch of '1' to case '1' sort of except there are two 1s so it carry's the second into the first input in the switch statement. does anyone have any idea how i can stop this so that it says it is eg 11, or 22, or 3333 ect is invalid entry please try again using 1,2,3,4,Q.
anyways heres the code i have written an im satisfied with it all apart from the above issue
// Including required standard librarys
#include <stdio.h>
//Declaring functions
float manager(float s);
float hworker(float w, float h);
float cworker(float A, float B, float C);
float pworker(float D, float E, float F);
// Main funtion
int main(void)
{
// declaring variables
char paycode=0; // Variable for paycode
float MTMPS = 0; // Manager salary temperary
float MTS = 0; // Total salary of all managers
int MC = 0; // Manager count
float WE = 0; //hourly wage temperary value
float HWT = 0; //Hours worked for employee temperary value
int WC = 0; //Wage worker count
float WTS = 0; //worker total salary
float itemA = 0;// Item A value temperary
float itemB = 0;// Item B value temperary
float itemC = 0;// Item C value temperary
int CWC = 0;//Comision worker count
float CWT = 0;//Commision worker total pay
int itemAP = 0;// Number of item A produced
int itemBP = 0;// Number of item A produced
int itemCP = 0;// Number of item A produced
int PWC = 0;//peiceworker count
float PWT = 0;//peiceworker total pay
while (paycode != EOF) // While statment to keep program runing until EOF is returned
{
printf("Enter employee paycode<1,2,3,4,Q>:");
paycode = getchar();// Asking for paycode value to enter into switch
switch (paycode)// Determin Employee type
{
case '1':// Manager case
{
printf("Enter weekly salary for manager:$");
scanf_s("%f", &MTMPS);// Asking for manager salary
MTS += manager(MTMPS);//calling manager function and inputing the manager salary value. returning manager salary and adding it to total salary of all managers
MC += 1;//increasing manager count by 1
}
break;
case '2': //Hour worker case
{
printf("Enter hourly wage of employee:$");
scanf_s("%f", &WE);// asking for input of hourly wage
printf("Enter hours worked by employee:");
scanf_s("%f", &HWT);// asking for input of hours worked
WTS += hworker(WE, HWT);//calling hourly worker function and inputing hours worked and hourly wage. Returning total wage for worker and adding it to total of all hourly wage workers
WC += 1;// increasing hourly wage worker count by 1
}
break;
case '3':// Commision worker case
{
printf("Enter sales value of item A:$");//Enter value of item A
scanf_s("%f", &itemA);
printf("Enter sales value of item B:$");//Enter value of item B
scanf_s("%f", &itemB);
printf("Enter sales value of item C:$");//Enter value of item C
scanf_s("%f", &itemC);
CWT += cworker(itemA, itemB, itemC);//calling commision worker function, inputing values of Items A,B and C. Returning total wage of commision worker and adding it to total of all commision worker wages.
CWC += 1;// Adding +1 to commision workers count.
}
break;
case '4':// Peiceworker case
{
printf("Enter the number of item 1 produced:");//Enter number of item 1 made
scanf_s("%d", &itemAP);
printf("Enter the number of item 2 produced:");//Enter number of item 2 made
scanf_s("%d", &itemBP);
printf("Enter the number of item 3 produced:");//Enter number of item 3 made
scanf_s("%d", &itemCP);
PWT += pworker(itemAP, itemBP, itemCP);//calling peiceworker function, inputing production of items 1,2 and 3. Returning total wage of peiceworker and adding total peiceworker wage to total of all the piece worker wages.
PWC += 1;// adding +1 to peiceworker count
}
break;
case 'Q':// quit case. display totals
{
printf("Manager: \tEmployees: %d\tTotal wages:$%0.2f\n", MC, MTS);// Display manager totals
printf("Hourly: \tEmployees: %d\tTotal wages:$%0.2f\n", WC, WTS);// Display hourly totals
printf("Commision: \tEmployees: %d\tTotal wages:$%0.2f\n", CWC, CWT);// Display commision totals
printf("Pieceworker: \tEmployees: %d\tTotal wages:$%0.2f\n", PWC, PWT);// Display peiceworker totals
printf("\n");
getchar();
paycode = EOF;
}
break;
default:// Letting user know all other charecters are invalid
{
printf("Incorrect paycode entered. Please enter paycode '1' for manager, '2' for \nhourly worker, '3' for commision worker, '4' for pieceworker, or 'Q' to quit and see totals.\n");
printf("\n");
}
break;
}
getchar();
}
return 0;
}
//////////////////////////// User defined Function area --- Definitions //////////////////////////////////////////
float manager(float s)// manager user defined function
{
float MS = s;
printf("Manage salary of $%.2f\n", MS);
printf("\n");
return (MS);// returing salary for manager
}
float hworker(float w, float h)// Hourly worker user defined function. Float w is wage, float h is hours
{
float NT = 0; //normal time hours
float OT = 0; // over time hours
float wage = 0; //total wage
float Nwage = 0; //normal time wage
float Owage = 0; // Over time wage
if (h <= 40)// dessision if over 40 hours have been worked. which mathematical formular to use
{
NT = h;
OT = 0;
}
else
{
OT = h - 40;
NT = h - OT;
}
Nwage = (NT*w);
Owage = (OT*w*1.5);
wage = Nwage + Owage;
printf("Wages are $%.2f <$%.2f regular and $%.2f overtime>\n", wage, Nwage, Owage);
printf("\n");
return (wage);// returning wage for hourly worker
}
float cworker(float A, float B, float C)//Commision worker user defined function
{
float AC = A*0.057;//Item A commision
float BC = B*0.064;//Item B commision
float CC = C*0.072;//Item C commision
float BS = 250;// Base salary
float CT = AC + BC + CC;//Total commisions
float TP = BS + AC + BC + CC;//Total pay
printf("Commision wage is $%.2f <$%.2f base + $%.2f commisions <$%.2f Item A, $%.2f Item B, $%.2f Item C\n", TP, BS, CT, AC, BC, CC);
printf("\n");
return (TP);//returning total wage and commision for commision worker
}
float pworker(float D, float E, float F)//Peiceworker user defined function
{
float APC = D*22.5;//Item 1 peicerworker commision
float BPC = E*24.5;//Item 2 peicerworker commision
float CPC = F * 26;//Item 3 peicerworker commision
float PTC = APC + BPC + CPC;//Peiceworker total commision
printf("Peiceworker wage : $%.2f <Items 1 $%.2f, Items 2 $%.2f, Items 3 $%.2f\n", PTC, APC, BPC, CPC);
printf("\n");
return (PTC);//returing total wage for specific peiceworker
}
I have written a program to calculate the wage for 4 classes of workers. One on salary, hourly rate, sales Commission, and the final on amount of product made. Each having the corresponding switch input of 1,2,3,4 and Q to display total pay off all workers within each working class. After Q and totals are display the program closes. Now if any other characters are entered the program displays a code and says invalid input please enter 1,2,3,4,Q.... ect. This works when the wrong singular character is entered although If i enter say '11' into switch the second 1 carry into the first statement in the body of case '1' in switch. I logical understand this because double 1 still satisfy s the switch of '1' to case '1' sort of except there are two 1s so it carry's the second into the first input in the switch statement. does anyone have any idea how i can stop this so that it says it is eg 11, or 22, or 3333 ect is invalid entry please try again using 1,2,3,4,Q.
anyways heres the code i have written an im satisfied with it all apart from the above issue
// Including required standard librarys
#include <stdio.h>
//Declaring functions
float manager(float s);
float hworker(float w, float h);
float cworker(float A, float B, float C);
float pworker(float D, float E, float F);
// Main funtion
int main(void)
{
// declaring variables
char paycode=0; // Variable for paycode
float MTMPS = 0; // Manager salary temperary
float MTS = 0; // Total salary of all managers
int MC = 0; // Manager count
float WE = 0; //hourly wage temperary value
float HWT = 0; //Hours worked for employee temperary value
int WC = 0; //Wage worker count
float WTS = 0; //worker total salary
float itemA = 0;// Item A value temperary
float itemB = 0;// Item B value temperary
float itemC = 0;// Item C value temperary
int CWC = 0;//Comision worker count
float CWT = 0;//Commision worker total pay
int itemAP = 0;// Number of item A produced
int itemBP = 0;// Number of item A produced
int itemCP = 0;// Number of item A produced
int PWC = 0;//peiceworker count
float PWT = 0;//peiceworker total pay
while (paycode != EOF) // While statment to keep program runing until EOF is returned
{
printf("Enter employee paycode<1,2,3,4,Q>:");
paycode = getchar();// Asking for paycode value to enter into switch
switch (paycode)// Determin Employee type
{
case '1':// Manager case
{
printf("Enter weekly salary for manager:$");
scanf_s("%f", &MTMPS);// Asking for manager salary
MTS += manager(MTMPS);//calling manager function and inputing the manager salary value. returning manager salary and adding it to total salary of all managers
MC += 1;//increasing manager count by 1
}
break;
case '2': //Hour worker case
{
printf("Enter hourly wage of employee:$");
scanf_s("%f", &WE);// asking for input of hourly wage
printf("Enter hours worked by employee:");
scanf_s("%f", &HWT);// asking for input of hours worked
WTS += hworker(WE, HWT);//calling hourly worker function and inputing hours worked and hourly wage. Returning total wage for worker and adding it to total of all hourly wage workers
WC += 1;// increasing hourly wage worker count by 1
}
break;
case '3':// Commision worker case
{
printf("Enter sales value of item A:$");//Enter value of item A
scanf_s("%f", &itemA);
printf("Enter sales value of item B:$");//Enter value of item B
scanf_s("%f", &itemB);
printf("Enter sales value of item C:$");//Enter value of item C
scanf_s("%f", &itemC);
CWT += cworker(itemA, itemB, itemC);//calling commision worker function, inputing values of Items A,B and C. Returning total wage of commision worker and adding it to total of all commision worker wages.
CWC += 1;// Adding +1 to commision workers count.
}
break;
case '4':// Peiceworker case
{
printf("Enter the number of item 1 produced:");//Enter number of item 1 made
scanf_s("%d", &itemAP);
printf("Enter the number of item 2 produced:");//Enter number of item 2 made
scanf_s("%d", &itemBP);
printf("Enter the number of item 3 produced:");//Enter number of item 3 made
scanf_s("%d", &itemCP);
PWT += pworker(itemAP, itemBP, itemCP);//calling peiceworker function, inputing production of items 1,2 and 3. Returning total wage of peiceworker and adding total peiceworker wage to total of all the piece worker wages.
PWC += 1;// adding +1 to peiceworker count
}
break;
case 'Q':// quit case. display totals
{
printf("Manager: \tEmployees: %d\tTotal wages:$%0.2f\n", MC, MTS);// Display manager totals
printf("Hourly: \tEmployees: %d\tTotal wages:$%0.2f\n", WC, WTS);// Display hourly totals
printf("Commision: \tEmployees: %d\tTotal wages:$%0.2f\n", CWC, CWT);// Display commision totals
printf("Pieceworker: \tEmployees: %d\tTotal wages:$%0.2f\n", PWC, PWT);// Display peiceworker totals
printf("\n");
getchar();
paycode = EOF;
}
break;
default:// Letting user know all other charecters are invalid
{
printf("Incorrect paycode entered. Please enter paycode '1' for manager, '2' for \nhourly worker, '3' for commision worker, '4' for pieceworker, or 'Q' to quit and see totals.\n");
printf("\n");
}
break;
}
getchar();
}
return 0;
}
//////////////////////////// User defined Function area --- Definitions //////////////////////////////////////////
float manager(float s)// manager user defined function
{
float MS = s;
printf("Manage salary of $%.2f\n", MS);
printf("\n");
return (MS);// returing salary for manager
}
float hworker(float w, float h)// Hourly worker user defined function. Float w is wage, float h is hours
{
float NT = 0; //normal time hours
float OT = 0; // over time hours
float wage = 0; //total wage
float Nwage = 0; //normal time wage
float Owage = 0; // Over time wage
if (h <= 40)// dessision if over 40 hours have been worked. which mathematical formular to use
{
NT = h;
OT = 0;
}
else
{
OT = h - 40;
NT = h - OT;
}
Nwage = (NT*w);
Owage = (OT*w*1.5);
wage = Nwage + Owage;
printf("Wages are $%.2f <$%.2f regular and $%.2f overtime>\n", wage, Nwage, Owage);
printf("\n");
return (wage);// returning wage for hourly worker
}
float cworker(float A, float B, float C)//Commision worker user defined function
{
float AC = A*0.057;//Item A commision
float BC = B*0.064;//Item B commision
float CC = C*0.072;//Item C commision
float BS = 250;// Base salary
float CT = AC + BC + CC;//Total commisions
float TP = BS + AC + BC + CC;//Total pay
printf("Commision wage is $%.2f <$%.2f base + $%.2f commisions <$%.2f Item A, $%.2f Item B, $%.2f Item C\n", TP, BS, CT, AC, BC, CC);
printf("\n");
return (TP);//returning total wage and commision for commision worker
}
float pworker(float D, float E, float F)//Peiceworker user defined function
{
float APC = D*22.5;//Item 1 peicerworker commision
float BPC = E*24.5;//Item 2 peicerworker commision
float CPC = F * 26;//Item 3 peicerworker commision
float PTC = APC + BPC + CPC;//Peiceworker total commision
printf("Peiceworker wage : $%.2f <Items 1 $%.2f, Items 2 $%.2f, Items 3 $%.2f\n", PTC, APC, BPC, CPC);
printf("\n");
return (PTC);//returing total wage for specific peiceworker
}