Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am working on this cinema reservation project !! 2 problems i m facing here is with data saving.

1) : when i close the program and try to register a new user the login information of previous registered user deletes Even though i am opening file in "a+" mode. but if i directly log in with the already registered user it works. the thing is i am not able to register more than 1 user at a time.

2) : When i close the program and logged in again the information of user reservation deletes. i want to save the user's reservation information too.

How can I solve this ?

What I have tried:

struct login
    {
        char fname[100];
        char lname[100];
        char username[20];
        char password[20];
    };
    #include <stdio.h>
    #define RVALUE 5
    #define CVALUE 10
    int i, j;
    void DisplaySeats(void);
    void ReserveSeats(void);
    void ChooseSeat(void);
    void CancelSeat(void);
    void CheckCancelSeat(void);
    void menu(void);
    int Seats[RVALUE][CVALUE];

    void registration();
    void login();
    int main()
    {
        int c;
        int choice, menu;
        printf("Welcome to our small Cinema!!!\n");
        printf("\n");
        //DisplaySeats();
        printf("\n1 : register!!!\n");
        printf("2 : login!!!\n");
        printf("3 : quit\n");
        printf("Enter your choice : \n");
        scanf("%d",&c);
        switch (c)
        {
        case 1:
            registration();
            break;
        case 2:
            login();
            break;
        case 3:
            printf("Thankyou for Choosing our small Cinema !! \n");
            getch();
            exit(1);
        default :
            system("CLS");
            printf("Enter a valid number !!\n");
            main();
        }

        getch();

    }
    void registration()
    {
        FILE *log;
        log = fopen("login.txt", "a+");
        struct login l;
        printf("\nEnter first name : ");
        scanf("%s", &l.fname);
        printf("\nEnter last name : ");
        scanf("%s", &l.lname);
        printf("\nEnter your Username : ");
        scanf("%s", &l.username);
        printf("\nEnter your password : ");
        scanf("%s", &l.password);
        fwrite(&l, sizeof(l), 1, log);
        fclose(log);
        printf("\nYou are successfully registered!!");
        printf("\nYour UserId is %s and your password is %s", l.username, l.password);
        printf("\nNow login with your username and password!!");
        printf("\nPress any key to continue ...");
        getch();
        system("CLS");
        main();
    }
    void login()
    {
        char username[100];
        char password[100];
        FILE *log;
        struct login l;
        log = fopen("login.txt", "r");
        if (log == NULL)
        {
            printf("FILE NOT FOUND!!!");
            exit(1);
        }
        printf("\nUserID : ");
        scanf("%s", &username);
        printf("\nPassword : ");
        scanf("%s", &password);
        while (fread(&l, sizeof(l), 1, log));
        {
            if (strcmp(username, l.username) == 0 && strcmp(password, l.password)==0)
            {
                system("CLS");
                printf("\nYou are successfully logged in !!\n");
                menu();

            }



            else
            {
                    printf("\nYour UserID or password is incorrect !!\n");
                    printf("Press any key to continue ...\n");
                    getch();
                    system("CLS");
                    login();
            }


        }
        fclose(log);

    }
    void ChooseSeat(void)
    {

        int row, col,k;
        printf("Which row do you want to choose? : ");
        scanf("%d", &row);
        printf("Which seat do you want to select? : ");
        scanf("%d", &col);
        if (row > RVALUE || col > CVALUE)
        {
            printf("Wrong Entry !! Try again\n");
            ChooseSeat();
        }
        else if (Seats[row - 1][col - 1] != 0)
        {
            printf("Seat is already reserved try again !!\n");
            ChooseSeat();
        }
        else
        {
            Seats[row - 1][col - 1] = 1;
            printf("Congratulations!! Reservation Completed!!!\n");
            DisplaySeats();
            printf("\nPress any key to go to main menu!!");
            getch();
            system("CLS");
            main();
        }


    }

    void ReserveSeats(void)
    {
        int NoOfSeats,i;
        printf("How many seats do you want to reserve?\n");
        scanf("%d", &NoOfSeats);
        DisplaySeats();
        for (i = 1; i <= NoOfSeats; i++)
        {
            ChooseSeat();
        }


    }
    void DisplaySeats(void)
    {
        printf("\t \t Seats\n");
        printf("\t 1 2 3 4 5 6 7 8 9 10\n");

        for (i = 0; i < RVALUE; i++)
        {
            printf("Rows %d : ", i + 1);
            for (j = 0; j < CVALUE; j++)
                printf("%d ", Seats[i][j]);
            printf("\n");
        }
        printf("\n");

    }
    void CheckCancelSeat(void)
    {
        while (1)
        {
            for (i = 0; i < RVALUE; i++)
            {
                for (j = 0; j < CVALUE; j++)
                {
                    if (Seats[i][j] == 0)
                    {
                        printf("There is no reserved seat available!!\n");
                        break;
                    }
                    else
                    {
                        CancelSeat();
                    }
                    break;
                }
                break;
            }
            system("CLS");
            break;


        }
    }
    void CancelSeat(void)
    {
        int r, c;
        printf("\nWhich row do you want to cancell ? : ");
        scanf("%d", &r);
        printf("\nWhich column do you want to cancell ? : \n");
        scanf("%d", &c);

        if (Seats[r - 1][c - 1] != 0)
        {
            Seats[r - 1][c - 1] = 0;
            printf("Your Seat is Cancelled !!\n");
            system("CLS");
        }
        else
        {
            printf("Wrong Entry !!\n");
            CancelSeat();
        }
    }
    void menu(void)
    {
        int choice;
            DisplaySeats();

            printf("1 : reserve a seat\n");
            printf("2 : cancell a seat\n");
            printf("3 : Main Menu\n");
            printf("Enter your choice : \n");
            scanf("%d", &choice);
            system("CLS");
            switch (choice)
                {
                    case 1:
                        ReserveSeats();
                        break;
                    case 2:
                        CheckCancelSeat();
                        break;

                    case 3:
                        main();
                        break;
                    default:
                        printf("Please enter a valid choice!!");
                        menu();
                        break;
                }
    }
Posted
Updated 7-Dec-18 14:59pm
v4
Comments
KarstenK 8-Dec-18 12:42pm    
use a while loop in your main function to avoid the recursive call.

You program is badly organized, with useless harmful recursive calls, calls to main, global variables.
In order to get proper help you should show us the code of the main function.

[update]
Try the following program (please note, you should always check the return value of the scanf function, I've shown only some examples in the posted code)

C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ROWS 5
#define COLS 10

#define LONG_STRING 100
#define SHORT_STRING  20

struct Login
{
  char fname[LONG_STRING];
  char lname[LONG_STRING];
  char username[SHORT_STRING];
  char password[SHORT_STRING];
};

void displaySeats(int seats[ROWS][COLS]);
void reserveSeats(int seats[ROWS][COLS]);
void cancelSeat(int seats[ROWS][COLS]);

int handleMainMenu();
int handleSubMenu(int seats[ROWS][COLS]);
void registration();
int login();


enum
{
  LOGGED_OUT,
  LOGGED_IN,
  EXIT
};

int main()
{
  int user_state = LOGGED_OUT;

  int seat[ROWS][COLS] = {0};

  while  (user_state != EXIT)
  {
    if ( user_state == LOGGED_OUT)
    {// user is NOT logged in, handle main menu 
      user_state = handleMainMenu();
    }
    else
    {// user is logged in, handle sub menu
      user_state = handleSubMenu(seat);
    }
  }
  return 0;
}

int handleMainMenu()
{
  int choice;
  while ( 1 )
  {
    printf("Welcome to our small Cinema!!!\n");
    printf("\n");
    printf("\n1 : register!!!\n");
    printf("2 : login!!!\n");
    printf("3 : quit\n");
    printf("Enter your choice : \n");

    if ( scanf("%d", &choice) == 1)
    {
      switch ( choice)
      {
      case 1:
        registration();
        break;
      case 2:
        if ( login())
          return LOGGED_IN;
        break;
      case 3:
        printf("Thank you for choosing our small Cinema !! \n");
        return EXIT;
      default:
        printf("Invalid choice, please insert a valid number\n");
        break;
      }
    }
  }
} 

int handleSubMenu(int seat[ROWS][COLS])
{
  int choice;

  while ( 1 )
  {
    printf("\nReservation Menu\n");
    displaySeats(seat);

    printf("1 : reserve a seat\n");
    printf("2 : cancell a seat\n");
    printf("3 : Main Menu\n");
    printf("Enter your choice : \n");
    if (scanf("%d", &choice)==1)
    {
      switch (choice)
      {
      case 1:
        reserveSeats(seat);
        break;
      case 2:
        cancelSeat(seat);
        break;
      case 3:
        return LOGGED_OUT;
      default:
        printf("Please enter a valid choice!!");
        break;
      }
    }
  }
}

void registration()
{
  FILE *log = fopen("login.txt", "a+");
  struct Login l;
  printf("\nEnter first name : ");
  scanf("%s", (l.fname));
  printf("\nEnter last name : ");
  scanf("%s", l.lname);
  printf("\nEnter your Username : ");
  scanf("%s", l.username);
  printf("\nEnter your password : ");
  scanf("%s", l.password);
  fwrite(&l, sizeof(l), 1, log);
  fclose(log);
  printf("\nYou are successfully registered!!");
  printf("\nYour UserId is %s and your password is %s", l.username, l.password);
  printf("\nNow login with your username and password!!");
  printf("\nPress any key to continue ...");
  getchar();
}

int login()
{
  FILE *log;
  struct Login linput, lfile;
  log = fopen("login.txt", "r");
  if (log == NULL)
  {
    printf("FILE NOT FOUND!!!\n");
    return 0; // login failed
  }
  printf("\nUserID : ");
  scanf("%s", linput.username);
  printf("\nPassword : ");
  scanf("%s", linput.password);
  while ( fread(&lfile, sizeof(lfile), 1, log)  )
  {
    if (strcmp(linput.username, lfile.username) == 0
      && strcmp(linput.password, lfile.password)==0)
    {
      printf("\nYou are successfully logged in !!\n");
      fclose(log);
      return 1; // login succeeded
    }
  }
  printf("\nYour UserID or password is incorrect !!\n");
  printf("Press any key to continue ...\n");
  getchar();
  fclose(log);
  return 0; // login failed
}

void reserveSeats(int seat[ROWS][COLS])
{
  int noOfSeats, noOfReservedSeats;
  printf("How many seats do you want to reserve?\n");
  scanf("%d", &noOfSeats);
  displaySeats(seat);

  noOfReservedSeats = 0;

  while (noOfReservedSeats < noOfSeats)
  {
    int row, col;

    printf("Which row do you want to choose? : ");
    scanf("%d", &row);
    printf("Which seat do you want to select? : ");
    scanf("%d", &col);
    if (row > ROWS || col > COLS)
    {
      printf("Wrong Entry !! Try again\n");
    }
    else if (seat[row - 1][col - 1] != 0)
    {
      printf("Seat is already reserved try again !!\n");
    }
    else
    {
      seat[row - 1][col - 1] = 1;
      ++noOfReservedSeats;
    }
  }
  printf("Congratulations!! Rservation complete!!!\n");
  printf("\nPress any key to continue!!\n");
  getchar();
}

void displaySeats(int seat[ROWS][COLS])
{
  printf("        Seats\n");
  printf("        1 2 3 4 5 6 7 8 9 10\n");
  int row;
  for (row = 0; row < ROWS; ++row)
  {
    int col;
    printf("Row %d : ", row + 1);
    for (col = 0; col < COLS; ++col)
      printf("%d ", seat[row][col]);
    printf("\n");
  }
  printf("\n");
}

void cancelSeat(int seat[ROWS][COLS])
{
  displaySeats(seat);
  int row, col;
  printf("\nWhich row do you want to cancell ? : ");
  scanf("%d", &row);
  printf("\nWhich column do you want to cancell ? : \n");
  scanf("%d", &col);

  if (seat[row - 1][col - 1] != 0)
  {
    seat[row - 1][col - 1] = 0;
    printf("Your Seat is Cancelled !!\n");
  }
  else
  {
    printf("The seat was not reserved !!\n");
  }
}

[/update]
 
Share this answer
 
v2
Comments
Member 14081431 7-Dec-18 20:59pm    
@Cpallini I changed the into full code i tried so far
CPallini 8-Dec-18 18:48pm    
Have a look at my updated solution.
As always, start with the debugger. Open your login file in a good editor (not Notepad) one that "looks at" the file and spots changes. I use PSPad, but there are loads of others. What you want is to open the file in the editor, then when a different app changes it the editor should notice and ask if you want to reload. (Notepad doesn't do this, but you can use it to that check others do)

Then run your code in the debugger. Register a new user. Did the file change? If it didn't, something is looking at the wrong file. Find out what.
If it did, then close your app. Does it change back?
COpen your app again. Has the file changed?

Basically, you want to know when the file changes, and when if changes back, if it does. You can then use the debugger to want what your code is doing and find out what is changing it (or not) and why. But without that info? nobody knows!

BTW: why are you calling main from your registration and ChooseSeat functions? That really isn't a good idea, it will cause a stack overflow if you do it oto many times. You should be looking at loops instead.
 
Share this answer
 
v2
Comments
Member 14081431 7-Dec-18 12:36pm    
i didn't post whole code and i am using codeblocks not notepad

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900