Advertisement
Guest User

studentFunction.c

a guest
Jan 8th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.51 KB | None | 0 0
  1. #include "studentFunction.h"
  2.  
  3. void startMenu(){
  4.     printf("1. Login\n");
  5.     printf("2. Create account\n");
  6.     printf("0. Exit\n\n");
  7. }
  8.  
  9. void createUser(struct User* userData){
  10.     printf("\n");
  11.     printf("New Username: ");
  12.     scanf("%s", userData->username);
  13.    
  14.     printf("New password: ");
  15.     scanf("%s", userData->password);
  16.    
  17.     printf("\nYour username is: %s \nYour password is: %s \n\n", userData->username, userData->password);
  18.    
  19.     printf("Your account has been created, welcome!\n");
  20. }
  21.  
  22. int login(struct User* userData, int amountOfUsers){
  23.     char username[80];
  24.     char password[80];
  25.     int loginLoop = 1;
  26.     int check = 0;
  27.     int position;
  28.    
  29.     for(int i = 0; i < amountOfUsers; i++){
  30.         printf("%s\n%s\n", userData[i].username, userData[i].password);
  31.     }
  32.    
  33.         do{
  34.             printf("Username: ");
  35.             scanf("%s", username);
  36.                        
  37.             printf("Password: ");
  38.             scanf("%s", password);
  39.            
  40.             for(int i = 0; i < amountOfUsers; i++){
  41.                
  42.                 if(strcmp(username, userData[i].username) == 0 && strcmp(password, userData[i].password) == 0){
  43.                         loginLoop = 1;
  44.                         check = 1;
  45.                         position = i;
  46.                         i = amountOfUsers;
  47.                         printf("\nLogin: Success\n");
  48.                 }
  49.                        
  50.                 else{
  51.                     printf("Login failed, try again!\n\n");
  52.                 }
  53.                
  54.             }
  55.                    
  56.            
  57.                
  58.                
  59.         }while(loginLoop == 0);
  60.    
  61.     return position;
  62. }
  63.  
  64. void mainMenu(struct User* userData, int userID, int amountOfUsers){
  65.    
  66.     char sentence[1000];
  67.     char newFileName[1000];
  68.     char fileContext[1000];
  69.    
  70.    
  71.     int loop = 1;
  72.     int option;
  73.     char inputChar;
  74.     char ReadFileName[80];
  75.    
  76.     char newPassword[80];
  77.    
  78.     struct User pUser;
  79.    
  80.    
  81.    
  82.    
  83.     do{
  84.         printf("\nMENU\n");
  85.         printf("---------------\n");
  86.         printf("1. Input text\n");
  87.         printf("2. Save inputed text to a new file\n");
  88.         printf("3. Add inputed text to an already existing file\n");
  89.         printf("4. Read from file and present the context of the file\n");
  90.         printf("5. Change password\n");
  91.         printf("6. Logout\n");
  92.         printf("7. Exit Program\n\n");
  93.                    
  94.         printf("Choose option (1 to 7): ");
  95.         scanf("%d", &option);
  96.         getchar();
  97.        
  98.         FILE *pFileText;
  99.         pFileText= fopen("text.txt", "a");
  100.        
  101.         switch (option){
  102.             case 1:
  103.                
  104.                 if(pFileText == NULL){
  105.                     printf("error opening file");
  106.                 }
  107.  
  108.                 printf("Enter text: ");
  109.                 fgets(sentence, 1000, stdin);
  110.  
  111.                 printf("\n%s\n", sentence); //remove later
  112.  
  113.                 printf("Would you like to save in existing file? (y/n): ");
  114.                 scanf("%c", &inputChar);
  115.  
  116.                 if(inputChar == 'y'){
  117.                     fputs(sentence, pFileText);
  118.                     fclose(pFileText);
  119.                 }
  120.        
  121.             break;
  122.            
  123.             case 2:
  124.                 printf("\n%s\n", sentence); // remove later on
  125.                
  126.                 printf("Name for new file: ");
  127.                 scanf("%s", newFileName);
  128.                 FILE * pNewFile;
  129.                 pNewFile = fopen(newFileName, "w");
  130.                
  131.                 if(!pNewFile){
  132.                         perror("Could not find file!");
  133.                 }
  134.                
  135.                 fputs(sentence, pNewFile);
  136.                 fclose(pNewFile);
  137.             break;
  138.            
  139.             case 3:            
  140.                 printf("Name of already existing file: ");
  141.                 scanf("%s", pUser.fileName);
  142.                 getchar();
  143.                
  144.                
  145.                 FILE * pExistingFile;
  146.                 pExistingFile = fopen(pUser.fileName, "a+");
  147.                
  148.                 if(!pExistingFile){
  149.                         perror("Could not find file!");
  150.                 }
  151.                 else{
  152.                     fputs(sentence, pExistingFile);
  153.                     fclose(pExistingFile);
  154.                     printf("\nSuccess! Text inputed in to existing file\n");
  155.                 }
  156.                
  157.             break;
  158.            
  159.             case 4:
  160.                 printf("Name of the file which you would like its content?: ");
  161.                 scanf("%s", ReadFileName);
  162.                
  163.                 FILE * readFile;
  164.                 readFile = fopen(ReadFileName, "r");
  165.                
  166.                 if(!readFile){
  167.                         perror("Could not find file!");
  168.                 }
  169.                
  170.                 printf("\n\n");
  171.                
  172.                 while(fgets(fileContext,sizeof(fileContext),readFile) != NULL){
  173.                     fputs(fileContext,stdout);
  174.                 }
  175.                
  176.                 printf("\n");
  177.                 fclose(readFile);
  178.             break;
  179.            
  180.             case 5:
  181.                 printf("Write your new password: ");
  182.                 scanf("%s", newPassword);
  183.                
  184.                 printf("Hello");
  185.                 printf("%s", userData[userID].password);
  186.                
  187.                 strcpy(userData[userID].password, newPassword);
  188.                
  189.  
  190.                    
  191.                 printf("%s", userData[userID].password);
  192.                
  193.  
  194.  
  195.                 FILE* outFile;
  196.                 outFile = fopen("User.txt", "r+");
  197.                    
  198.                 fseek(outFile, 3, SEEK_SET);
  199.  
  200.                 for (int i = 0; i < amountOfUsers; i++){
  201.                         fprintf(outFile, "%s\t%s\n", userData[i].username, userData[i].password);
  202.                 }
  203.                
  204.                 fclose(outFile);
  205.                
  206.             break;
  207.            
  208.             case 6:
  209.                 printf("Logout");
  210.             break;
  211.            
  212.             case 7:
  213.                 printf("Bye!");
  214.                 exit(0);
  215.             break;
  216.            
  217.             default:
  218.                 printf("input error");
  219.             break;
  220.         }
  221.     }while(loop != 0);
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement