Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "studentFunction.h"
- void startMenu(){
- printf("1. Login\n");
- printf("2. Create account\n");
- printf("0. Exit\n\n");
- }
- void createUser(struct User* userData){
- printf("\n");
- printf("New Username: ");
- scanf("%s", userData->username);
- printf("New password: ");
- scanf("%s", userData->password);
- printf("\nYour username is: %s \nYour password is: %s \n\n", userData->username, userData->password);
- printf("Your account has been created, welcome!\n");
- }
- int login(struct User* userData, int amountOfUsers){
- char username[80];
- char password[80];
- int loginLoop = 1;
- int check = 0;
- int position;
- for(int i = 0; i < amountOfUsers; i++){
- printf("%s\n%s\n", userData[i].username, userData[i].password);
- }
- do{
- printf("Username: ");
- scanf("%s", username);
- printf("Password: ");
- scanf("%s", password);
- for(int i = 0; i < amountOfUsers; i++){
- if(strcmp(username, userData[i].username) == 0 && strcmp(password, userData[i].password) == 0){
- loginLoop = 1;
- check = 1;
- position = i;
- i = amountOfUsers;
- printf("\nLogin: Success\n");
- }
- else{
- printf("Login failed, try again!\n\n");
- }
- }
- }while(loginLoop == 0);
- return position;
- }
- void mainMenu(struct User* userData, int userID, int amountOfUsers){
- char sentence[1000];
- char newFileName[1000];
- char fileContext[1000];
- int loop = 1;
- int option;
- char inputChar;
- char ReadFileName[80];
- char newPassword[80];
- struct User pUser;
- do{
- printf("\nMENU\n");
- printf("---------------\n");
- printf("1. Input text\n");
- printf("2. Save inputed text to a new file\n");
- printf("3. Add inputed text to an already existing file\n");
- printf("4. Read from file and present the context of the file\n");
- printf("5. Change password\n");
- printf("6. Logout\n");
- printf("7. Exit Program\n\n");
- printf("Choose option (1 to 7): ");
- scanf("%d", &option);
- getchar();
- FILE *pFileText;
- pFileText= fopen("text.txt", "a");
- switch (option){
- case 1:
- if(pFileText == NULL){
- printf("error opening file");
- }
- printf("Enter text: ");
- fgets(sentence, 1000, stdin);
- printf("\n%s\n", sentence); //remove later
- printf("Would you like to save in existing file? (y/n): ");
- scanf("%c", &inputChar);
- if(inputChar == 'y'){
- fputs(sentence, pFileText);
- fclose(pFileText);
- }
- break;
- case 2:
- printf("\n%s\n", sentence); // remove later on
- printf("Name for new file: ");
- scanf("%s", newFileName);
- FILE * pNewFile;
- pNewFile = fopen(newFileName, "w");
- if(!pNewFile){
- perror("Could not find file!");
- }
- fputs(sentence, pNewFile);
- fclose(pNewFile);
- break;
- case 3:
- printf("Name of already existing file: ");
- scanf("%s", pUser.fileName);
- getchar();
- FILE * pExistingFile;
- pExistingFile = fopen(pUser.fileName, "a+");
- if(!pExistingFile){
- perror("Could not find file!");
- }
- else{
- fputs(sentence, pExistingFile);
- fclose(pExistingFile);
- printf("\nSuccess! Text inputed in to existing file\n");
- }
- break;
- case 4:
- printf("Name of the file which you would like its content?: ");
- scanf("%s", ReadFileName);
- FILE * readFile;
- readFile = fopen(ReadFileName, "r");
- if(!readFile){
- perror("Could not find file!");
- }
- printf("\n\n");
- while(fgets(fileContext,sizeof(fileContext),readFile) != NULL){
- fputs(fileContext,stdout);
- }
- printf("\n");
- fclose(readFile);
- break;
- case 5:
- printf("Write your new password: ");
- scanf("%s", newPassword);
- printf("Hello");
- printf("%s", userData[userID].password);
- strcpy(userData[userID].password, newPassword);
- printf("%s", userData[userID].password);
- FILE* outFile;
- outFile = fopen("User.txt", "r+");
- fseek(outFile, 3, SEEK_SET);
- for (int i = 0; i < amountOfUsers; i++){
- fprintf(outFile, "%s\t%s\n", userData[i].username, userData[i].password);
- }
- fclose(outFile);
- break;
- case 6:
- printf("Logout");
- break;
- case 7:
- printf("Bye!");
- exit(0);
- break;
- default:
- printf("input error");
- break;
- }
- }while(loop != 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement