Advertisement
Guest User

Assignment

a guest
Jul 14th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.63 KB | None | 0 0
  1. #include<stdio.h>
  2. #include <conio.h>
  3.  
  4. //Salary function to calculate Employee's Salary
  5. void Salary(int s)
  6. {
  7.     if(s<40) //Checks if the number of hours is less than 40 hours
  8.     {
  9.         printf("There is no Salary!\n"); //Print that there's no salary
  10.     }
  11.     else if((s>=40)&&(s<50)) //Checks if the number of hours ranges between 40 & 50 hours
  12.     {
  13.         s*=50; //Calculates his salary by multiplying the number of hours by 50 (s=s*50)
  14.         printf("Your salary is %d\n",s); //Prints the salary after calculating it
  15.     }
  16.     else if((s>=50)&&(s<60)) //Checking if the number of hours ranges between 50 & 60
  17.     {
  18.         s*=60; //Calculates the salary by multiplying the number of hours by 60 for each hour (s=s*60)
  19.         printf("Your salary is %d\n",s); //Prints the salary after calculating it
  20.     }
  21.     else if(s>=60) //Checks if the number of hours is greater thanm 60 (100 L.E per hour)
  22.     {
  23.         s*=100; //Calculates the salary by multiplying the number of hours by 100 for each hour (s=s*100)
  24.         printf("Your salary is %d\n",s); //Prints the salary after calculating it
  25.     }
  26. }
  27.  
  28.  
  29. //Password function used to check if the password is the correct one for each user or not
  30. int Password(char name,int defaultpw,int recpw) //Function declaration where name is the employee name, defaultpw is the default password for each employee, recpw is the received password entered by the user while logging in
  31. {
  32.        if(defaultpw==recpw) //Checking if the entered password is the same as the default employee password
  33.         {
  34.             printf("The password is correct! Welcome %c\n",name); //Prints that the password is correct
  35.            return 1; //return 1 to the main function
  36.         }else
  37.         {
  38.             printf("Try again! Please enter the password:\n"); //When the password is not correct, it gives another chance
  39.             scanf("%d",&recpw); //Scanf to recieve the password for the second time
  40.            if(defaultpw==recpw) //checks if the entered password is correct or not
  41.        {
  42.             printf("The password is correct! Welcome %c\n",name); //If it's correct it prints that "The password is correct"
  43.                return 1; //It also returns 1 to the main function
  44.        }else
  45.         {
  46.            printf("Try again! Please enter the password:\n"); //If the password is incorrect for the second time, it gives him one last chance
  47.            scanf("%d",&recpw); //Accepts the password for the third/last time from the user
  48.        if(defaultpw==recpw) //Checks if the entered password is correct or not
  49.        {
  50.            printf("The password is correct! Welcome %c\n",name); //Prints that the password is correct incase it's correct
  51.            return 1; //Returns 1 to the main function
  52.        }else
  53.        {
  54.            printf("Wrong Entry!"); //Prints Wrong entry if the password is wrong for the last chance
  55.            return 0; //return 0 to the main function
  56.        }
  57.            }
  58.         }
  59. }
  60.  
  61.  
  62. void main(void)
  63. {
  64.     int pw1,pw2,pw3,NewPw1=0,NewPw2=0,NewPw3=0,pass,ret,choice,hrs; //declaring variables where pw1 and NewPw1 belongs to employee a and so on
  65.     char Employee; //used to check the employee's name
  66.  
  67.     printf("Please enter your username: \n"); //asking the user to enter the username
  68.     Employee=getch(); //receiving the username using getch(); function
  69.     printf("Please enter your password: \n"); //Asking the user to enter his password
  70.     scanf("%d",&pass); //saving the password in the variable pass
  71.     if(Employee=='A') //checking if the empployee is A
  72.     {
  73.         pw1=352; //setting his default password as 352
  74.         if(NewPw1==0) //checking if the password is changed (If it's changed then it won't be equal to 0) so having it =0 means it didn't change
  75.         {
  76.             ret=Password(Employee,pw1,pass); //calling Password function to check if the password entered is correct for employee A or not and get a return if the password is correct the the return will be 1 while if it's wrong then it'll be 0
  77.            
  78.         }else
  79.         {
  80.             ret=Password(Employee,NewPw1,pass); //calling password function to check if the password entered is equal to the new password and get a return
  81.         }
  82.     }else if(Employee=='B') //Checking if the employee is B
  83.     {
  84.         pw2=245; //Setting his default password as 254
  85.         if(NewPw2==0) //checking if the password is changed (If it's changed then it won't be equal to 0) so having it =0 means it didn't change
  86.         {
  87.             ret=Password(Employee,pw2,pass); //calling Password function to check if the password entered is correct for employee B or not and get a return if the password is correct the the return will be 1 while if it's wrong then it'll be 0
  88.            
  89.         }else
  90.         {
  91.             ret=Password(Employee,NewPw2,pass); //calling password function to check if the password entered is equal to the new password and get a return
  92.         }
  93.     }else if(Employee=='C') //Checking if the employee is C
  94.     {
  95.         pw3=367; //Setting his default password as 367
  96.         if(NewPw3==0) //checking if the password is changed (If it's changed then it won't be equal to 0) so having it =0 means it didn't change
  97.         {
  98.             ret=Password(Employee,pw3,pass); //calling Password function to check if the password entered is correct for employee C or not and get a return if the password is correct the the return will be 1 while if it's wrong then it'll be 0
  99.            
  100.         }else
  101.         {
  102.             ret=Password(Employee,NewPw3,pass); //calling password function to check if the password entered is equal to the new password and get a return
  103.         }
  104.     }else
  105.     {
  106.         printf("Wrong Username!\n"); //Print "Wrong username" message if the entered username is wrong
  107.     }
  108.  
  109.     if(ret==1) //It is used to check if the return from the Password function is "1" it means that the password is correct so it displays the menu
  110.     {
  111.         printf("1-Calculate your salary \n 2-Change your password\n"); //Displays the menu
  112.         scanf("%d",&choice); //accept the user's choice from the menu
  113.         if(choice==1) //If the user chose number one from menu, it will check the salary
  114.         {
  115.             printf("Please enter the number of hours: \n"); //Asking the user to enter the amount of hours
  116.             scanf("%d",&hrs); //Accept the number of hours from the employee
  117.             Salary(hrs); //Calling salary function and passing the number of hours to it so it calculate the salary.
  118.         }else if(choice==2) //If the user chose number 2 from the menu, it will change his password
  119.         {
  120.             printf("Please enter your new password: \n"); //Asking the user to enter the new password
  121.             if(Employee=='A') //checking if the user is Employee A
  122.             {
  123.                 scanf("%d",&NewPw1); //Updating the NewPw1 variable with the new value
  124.                 printf("Dear A, You have changed your password succesfully!\n\n"); //Print msg saying that password changed successfully
  125.             }else if(Employee=='B') //checking if the user is Employee B
  126.             {
  127.                 scanf("%d",&NewPw2); //Updating the NewPw2 variable with the new value
  128.                 printf("Dear B, You have changed your password succesfully!\n\n"); //Print msg saying that password changed successfully
  129.             }else if(Employee=='C') //checking if the user is Employee C
  130.             {
  131.                 scanf("%d",&NewPw3); //Updating the NewPw3 variable with the new value
  132.                 printf("Dear C, You have changed your password succesfully!\n\n"); //Print msg saying that password changed successfully
  133.             }
  134.         }
  135.  
  136.          printf("Please enter your username: \n"); //Asking the user to enter his username
  137.         Employee=getch(); //Receiving the username and saving it in a variable
  138.         printf("Please enter your password: \n"); //asking the user to enter his password
  139.         scanf("%d",&pass); //recieving his password and saving it in a variable
  140.        
  141.         //Repeating the same checking once again to check if the password is changed or not then pass the right password with the user name and the entered password to the password function to check if the entered password for the username is correct or not.
  142.     if(Employee=='A') //Checking if the user is Employee A
  143.     {
  144.         pw1=352; //Setting his default password
  145.         if(NewPw1==0) //Checking if the password is changed or not
  146.         {
  147.             ret=Password(Employee,pw1,pass); //Calling the password function
  148.         }else
  149.         {
  150.             ret=Password(Employee,NewPw1,pass); //Calling the password function
  151.         }
  152.     }else if(Employee=='B') //Checking if the user is employee B
  153.     {
  154.         pw2==245; //Setting his default password
  155.         if(NewPw2==0) //Checking if the password is changed or not
  156.         {
  157.             ret=Password(Employee,pw2,pass); //Calling the password function
  158.         }else
  159.         {
  160.             ret=Password(Employee,NewPw2,pass); //calling the password function
  161.         }
  162.     }else if(Employee=='C') //Checking if the user is employee C
  163.     {
  164.         pw3=367; //Setting his default password
  165.         if(NewPw3==0) //checking if the password is correct or not
  166.         {
  167.             ret=Password(Employee,pw3,pass); //calling the password function
  168.         }else
  169.         {
  170.             ret=Password(Employee,NewPw3,pass); //calling the password function
  171.         }
  172.     }else
  173.     {
  174.         printf("Wrong Username!\n"); //Printing Wrong Username message
  175.     }
  176.  
  177.         if(ret==1) //It is used to check if the return from the Password function is "1" it means that the password is correct so it displays the menu
  178.     {
  179.         printf("1-Calculate your salary\n2-Change your password\n"); //Displaing the menu for the user
  180.         scanf("%d",&choice); //Accepting choice
  181.         if(choice==1) //checking choice if it's 1
  182.         {
  183.             printf("Please enter the number of hours: \n"); //asking him to enter the amount of hours
  184.             scanf("%d",&hrs); //Saving the number of hours
  185.             Salary(hrs); //Calling salary function to calculate the employee's salary
  186.         }else if(choice==2) //checking if choice is 2 from the menu so user can change his password
  187.         {
  188.             printf("Please enter your new password: \n"); //asking the user to enter his new password
  189.             if(Employee=='A') //checking if the user is A Employee
  190.             {
  191.                 scanf("%d",&NewPw1); //recieve the password and save it in the NewPw1
  192.                 printf("Dear A, You have changed your password succesfully!\n"); //Print message that the password has changed
  193.             }else if(Employee=='B') //Checking if the user is employee B
  194.             {
  195.                 scanf("%d",&NewPw2); //receive the password and save it in NewPw2
  196.                 printf("Dear B, You have changed your password succesfully!\n"); //Print message that the password has changed
  197.             }else if(Employee=='C') //checking if the user is B employee is C
  198.             {
  199.                 scanf("%d",&NewPw3); //recieve the password and save it in NewPw3
  200.                 printf("Dear C, You have changed your password succesfully!\n"); //Print message that the password has changed
  201.             }
  202.         }
  203.  
  204.             //Repeating the same thing again for the 3rd and last time which ask the user to enter his username and password then display the menu and so on.
  205.            
  206.          printf("Please enter your username: \n");
  207.         Employee=getch();
  208.         printf("Please enter your password: \n");
  209.         scanf("%d",&pass);
  210.     if(Employee=='A')
  211.     {
  212.         pw1=352;
  213.         if(NewPw1==0)
  214.         {
  215.             ret=Password(Employee,pw1,pass);
  216.         }else
  217.         {
  218.             ret=Password(Employee,NewPw1,pass);
  219.         }
  220.     }else if(Employee=='B')
  221.     {
  222.         pw2==245;
  223.         if(NewPw2==0)
  224.         {
  225.             ret=Password(Employee,pw2,pass);
  226.         }else
  227.         {
  228.             ret=Password(Employee,NewPw2,pass);
  229.         }
  230.     }else if(Employee=='C')
  231.     {
  232.         pw3=367;
  233.         if(NewPw3==0)
  234.         {
  235.             ret=Password(Employee,pw3,pass);
  236.         }else
  237.         {
  238.             ret=Password(Employee,NewPw3,pass);
  239.         }
  240.     }else
  241.     {
  242.         printf("Wrong Username!\n");
  243.     }
  244.  
  245.         if(ret==1)
  246.     {
  247.         printf("1-Calculate your salary\n2-Change your password\n");
  248.         scanf("%d",&choice);
  249.         if(choice==1)
  250.         {
  251.             printf("Please enter the number of hours: \n");
  252.             scanf("%d",&hrs);
  253.             Salary(hrs);
  254.         }else if(choice==2)
  255.         {
  256.             printf("Please enter your new password: \n");
  257.             if(Employee=='A')
  258.             {
  259.                 scanf("%d",&NewPw1);
  260.                 printf("Dear A, You have changed your password succesfully!\n");
  261.             }else if(Employee=='B')
  262.             {
  263.                 scanf("%d",&NewPw2);
  264.                 printf("Dear B, You have changed your password succesfully!\n");
  265.             }else if(Employee=='C')
  266.             {
  267.                 scanf("%d",&NewPw3);
  268.                 printf("Dear C, You have changed your password succesfully!\n");
  269.             }
  270.         }
  271.  
  272.  
  273.  
  274.     }else
  275.         {
  276.             printf("Try again later!\n"); //If he failed to enter the correct password for 3 times he gets error messsage and program stop
  277.         }
  278.  
  279.     }else
  280.         {
  281.             printf("Try again later!\n"); //If he failed to enter the correct answer for 3 times he gets error message and program stop
  282.         }
  283.  
  284.  
  285. }else
  286.     {
  287.         printf("Try again later!\n"); //If he failed to enter the correct password he gets error message and program stop
  288.     } //If he failed to enter the correct password he gets error message and program stop
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement