Thread: Auto generate ID number for array

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    1

    Auto generate ID number for array

    Hello to all

    Please I need some help for homework of my daughter

    She have a task to create a movie library and the first command is to generate for each records the ID number automatically

    She write a code as following

    Code:
    int menu()
    {
    int choice;
    
    
    printf("Input:\n");
    printf("1. Insert a new movie\n");
    printf("2. Delete a movie based on movie's ID .\n");
    printf("3. Search a movie based on movie's title.\n");
    printf("4. Search a movie based on directo's surname.\n");
    printf("5. Display movies.\n");
    printf("6. Exit.\n");
    
    
    
    
    scanf("%d", &choice);
    
    
    return choice;
    }
    
    
    void insert_movie(movies_m *mo)
    {
    movie_m m;
    
    
    printf("ID: ");
    fflush(stdin);
    scanf("%d", &m.id);
    
    printf("Title: ");
    fflush(stdin);
    scanf("%s", m.title);
    
    printf("Name: ");
    fflush(stdin);
    scanf("%s", m.name);
    
    printf("Surname: ");
    fflush(stdin);
    scanf("%s", m.surname);
    
    printf("Day: ");
    fflush(stdin);
    scanf("%d", &m.day);
    
    while(m.day>31 || m.day<1)
    {
        printf("Not valid Day.\nPlease try again (1-31)\n");
        printf("DAY :\n");
        fflush(stdin);
        scanf("%d",&m.day);    
    }
        
    
    printf("Month: ");
    fflush(stdin);
    scanf("%d", &m.month);
    while(m.month>12 || m.month<1)
    {
        printf("Not valid Month.\nPlease try again (1-12)\n");
        printf("Month :\n");
        fflush(stdin);
        scanf("%d",&m.month);    
    }
    
    printf("Year: ");
    fflush(stdin);
    scanf("%d", &m.year);
    while(m.year>2100 || m.year<1)
    {
        printf("Not valid Year.\nPlease try again (1-2100)\n");
        printf("Year :\n");
        fflush(stdin);
        scanf("%d",&m.year);    
    }
    Thank you in advance

    Mike

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,296
    Something like this is simple enough for homework.
    Code:
    int getNextId(void) {
      static int id = 0;
      return id++;
    }
    Being declared static, the id variable will remember the last value it had between calls to the function.

    On another note, remove all those fflush(stdin) calls.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 04-24-2015, 11:39 AM
  2. Replies: 5
    Last Post: 05-17-2012, 03:18 PM
  3. generate number
    By cheeta in forum C Programming
    Replies: 9
    Last Post: 05-03-2010, 07:49 AM
  4. auto incremeting build number
    By pheres in forum C++ Programming
    Replies: 6
    Last Post: 11-10-2008, 05:18 AM
  5. auto generate password
    By Coconut in forum C Programming
    Replies: 4
    Last Post: 09-29-2002, 03:55 AM

Tags for this Thread