lundi 28 mai 2018

When I login with id=0 (as Administrator) I want it to show the Admin Menu (and select options from there), not to be overwritten by Student Menu

1.The problem I want to solve is in while(id){ if(id==0){} else if(id!=0){} }. Is there another way how can I solve this problem without while.
So both if and else if statements are executed here, instead of executing only one case. So, when I login with id=0 (as Administrator) I want it to show the Administrator Menu (and select options from administrator menu), not to be overwritten by Student Menu.

*/
#include <stdio.h> 
#include <stdlib.h> 
#include <conio.h> 
#include <windows.h> 
#include <string.h>

void login(int id, char password) {
   printf("\n\t\tLogin: \n");
   printf("\t\tID:\t");
   scanf("%d", & id);
   printf("\n");
   printf("\t\tPassword:\t");
   scanf("%s", & password);
   system("cls");

if (id == 0) {
    system("cls");
    showAdminMenu();
} else if (id > 0) {
    showStudentMenu();
} else {
    printf("\n\n\t\t");
    printf("ID doesn't exist!\n");
    login(id, password);
}
}

void showAdminMenu() {
   printf("ADMINISTRATOR MENU:\n");
   printf("1.Add\n");
   printf("2.Modify\n");
   printf("3.Delete\n");
   printf("4.Exit\n");
}

void showStudentMenu() {
   printf("STUDENT MENU:\n");
   printf("1.Change Password\n");
   printf("2.Register\n");
   printf("3.Report\n");
   printf("4.Exit\n");
}

int main() {
  int userChoice;
  int id;
  char password;
  printf("\n\n");

printf("\t\t1.Login\n");
printf("\t\tPress -1 to exit\n");
printf("\t\tChoice: ");
scanf("%d", & userChoice);

system("cls");

if (userChoice == 1) {
    login(id, password);
} else if (userChoice == -1) {
    exit(1);
}

FILE * fp, * ft; //file pointers
char anotherChoice, choice;

/* structure that represents a astudent*/

struct student {
    char id[15];
    char name[40];
    char password[10];
    int age;
};
struct student s; //create struct student
char studentID[15];
char studentName[40];

long int recsize;

fp = fopen("STUDENT.DAT", "rb+");
if (fp == NULL) {
    fp = fopen("STUDENT.DAT", "wb+");
    if (fp == NULL) {
        printf("File cannot open");
        exit(1);
    }
}

recsize = sizeof(s);

//function inside admin menu
void addStudent() {
    system("cls");
    fseek(fp, 0, SEEK_END);
    anotherChoice = 'y';
    while (anotherChoice == 'y') {
        system("cls");
        printf("\nID:\t");
        scanf("%s", & s.id);
        printf("\nName:\t");
        scanf("%s", & s.name);
        printf("\nPassword:\t");
        scanf("%s", & s.password);
        printf("\nAge:\t");
        scanf("%d", & s.age);

        fwrite( & s, recsize, 1, fp); //writes records in a file
        printf("\nWould you like to add another student(y/n)");
        fflush(stdin);
        anotherChoice = getche();
    }

}

//function inside student menu
void changePassword() {
    system("cls");
    printf("Type a new password:\t");
    scanf("%s", & s.password);
    fseek(fp, -recsize, SEEK_CUR);
    fwrite( & s, recsize, 1, fp);
    printf("\nPassword successfully changed!");

}

while (id) {     //

    if (id == 0) {   //shows admin menu
        system("cls");
        printf("ADMINISTRATOR MENU:\n");
        printf("1.Add\n");
        printf("2.Modify\n");
        printf("3.Delete\n");
        printf("4.Exit\n");
        printf("Choice: ");
        fflush(stdin);
        char choiceFromAdminMenu;
        choiceFromAdminMenu = getch();

        switch (choiceFromAdminMenu) {
        case '1':
            addStudent();
            break;
        case '2':
        //modifyStudent();
        //break;
        case '3':
        //deleteStudent();
        //break;
        case '4':
        //exit();
        //break;

        default:
            printf("\nWrong!Please enter a valid option");
            getch();
        }
    } else if (id != 0) {        //show student menu
        system("cls");   
        printf("STUDENT MENU:\n");  
        printf("1.Change Password\n");
        printf("2.Register\n");
        printf("3.Report\n");
        printf("4.Exit\n");
        printf("Choice: ");
        fflush(stdin);
        char choiceFromStudentMenu;
        choiceFromStudentMenu = getch();

        switch (choiceFromStudentMenu) {
        case '1':
            changePassword();
            break;
        case '2':
        //register();
        //break;
        case '3':
        //report();
        //break;
        case '4':
        //exit(0);
        //break;

        default:
            printf("\nWrong!Please enter a valid option");
            getch();
        }

    }
}

return 0;
}

Aucun commentaire:

Enregistrer un commentaire