Thread: Some Problem With My Program

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    3

    Some Problem With My Program

    Problem: I created a program for booking tickets. It works fine when its for single user. But when i make it for multi user, "user[100]". Funny characters starts to pop up when i key in the seats numbers. Example Smiling face, heart shape etc. When i open the source file user.txt those funny characters is printed as C in the file. Causing my program to be very unstable. Below is my code for booking.
    -
    Code:
    int choice();
    int i,m,n,row,column,x=5,y=5;
    
    #define FILENAME "user.txt"
    
    void show_main_menu();
    void logo();
    void book_ticket();
    void edit_booking();
    void cancel_booking();
    void view_available();
    void exitt();
    void thank_you();
    void look_up();
    
    struct data
    {
        char useless[1];
        char seat[5][10];
        char name[30];
        char nirc_number[10];
        char contact_number[10];
    }user[100], data, sales_data[100], temp;
    Code:
    /*#############################################################################*/
    /*                                Book Ticket Function                         */
    /*#############################################################################*/
    void book_ticket()
    {					 
        
        FILE *fptr;
        int quit=1;
        textcolor(YELLOW);
        fptr=fopen("user.txt","r");
        fread(&user[100],sizeof(user[100]),1,fptr);
        fclose(fptr);
        while(column!=99)   
        {
                    system("cls");
                    user[100].seat[row-1][column-1]='X';
                    
                    printf("\t\t\t \t      SCREEN           \n\n");
                    printf("\t\t             1  2  3  4  5  6  7  8  9  10\n");
                    
                    printf("\n\t\t\t1   ");        
                    for(i=0;i<10;i++)
                    {
                        if(user[100].seat[0][i]>0)
                        printf(" %c ",user[100].seat[0][i]);
                    }
                    printf("\n\n");
                    
                    printf("\t\t\t2   ");
                    for(i=0;i<10;i++)
                    {if(user[100].seat[0][i]>0)
                        printf(" %c ",user[100].seat[1][i]);
                    }
                    printf("\n\n");
                  
                    printf("\t\t\t3   ");
                    for(i=0;i<10;i++)
                    {if(user[100].seat[0][i]>0)
                        printf(" %c ",user[100].seat[2][i]);
                    }
                    printf("\n\n");
                    
                    printf("\t\t\t4   ");
                    for(i=0;i<10;i++)
                    {if(user[100].seat[0][i]>0)
                        printf(" %c ",user[100].seat[3][i]);
                    }
                    printf("\n\n");
                   
                    printf("\t\t\t5   ");
                    for(i=0;i<10;i++)
                    {if(user[100].seat[0][i]>0)
                        printf(" %c ",user[100].seat[4][i]);
                    }
                    
                    printf("\n\n\n\n\n");
                    printf("\t\t\t   Enter Row Column(99,99 to quit): "); //choose to for 'X'  
                    scanf("%d,%d",&row, &column);     
                    if(row==99)
                            break;                  
        }
        clrscr();
        logo();
    	printf("\n\n\n\n\t");
    	gets(user[100].useless);
        printf("\n\tPlease Enter Your Name:");
    	gets(user[100].name);
    	printf("\n\tPlease Enter Owner IC Number:");
    	gets(user[100].nirc_number);
    	printf("\n\tPlease Enter Your Contact Number:");
    	gets(user[100].contact_number);
    	fopen("user.txt", "ab+");
        fwrite(&user[100],sizeof(user[100]),1,fptr);
        fclose(fptr);    
    	printf("\n\n\n\n");
    	printf("\n\tThank You For Providing Your Details");
    	printf("\n\tPress Anykey To Continue");
    	getch();
        thank_you();       
    }
    -

    If i was to change the "user[100]" to "user" it works fine. But i need it to be multi users. I have totally no idea how i can solve this problem. Appreciate help from members in this forum.
    Can upload my whole program too if necessary. Heres the screenshot.
    http://www.geocities.com/eradicate85/Program.JPG
    Last edited by Americano; 10-17-2003 at 11:22 AM.

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Do you know how to use arrays?

    In your code, user[100] represents one, and only one user. You can get access to the other 99 elements by changing the index eg. user[0], user[34], user[87]...

    //Edit
    I just woke up, so i didn't even notice that user[100] doesn't even belong to your array, only user[0] - user[99]
    //
    Last edited by The Dog; 10-17-2003 at 01:35 PM.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Your definition of user[100]; defines 100 users, but they are numbered 0 to 99. Therefore, user[100] itself does not exist. That's why you're getting weird stuff, you're reading memory you shouldn't.

    You need to index into the users the same way you did with seat[][]. User number one would be user[0], the seat for user number 3 would be user[2].seat[0][i] or whatever.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    3
    Sorry for my insufficient programming knowledge, erm.. i don't quite get what you guys are saying. Would u guys mind giving me a small example or so to make me understand better?

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Code:
    struct STUDENT
    {
        char name[30];              // Student's name
        int  grade;                 // Student's current grade
        int  test_scores[10];       // Student's test scores
    } student[100];    // define 100 students, 0-99
    
    
    // To print out the information for each student:
    
    for (k=0; k < 100; k++)     // Set up loop for each student
    {
        printf("%s  %2d  ", student[k].name, student[k].grade);
        for (j=0; j<10; j++)    // loop thru each test score
        {
            printf("%2d ", student[k].test_scores[j]);
        }
        printf("\n");
    }
    The outer loop references each student. The inner loop each test for the current student.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    3
    Code:
    / *#################################################
    ############################*/
    /*                                Book Ticket Function                         */
    / *#################################################
    ############################*/
    void book_ticket()
    {					 
        
        FILE *fptr;
        int quit=1;
        textcolor(YELLOW);
        fptr=fopen("user.txt","r");
        fread(&user[i],sizeof(user[i]),1,fptr);
        fclose(fptr);
        while(column!=99)   
        {
                    system("cls");
                    user[i].seat[row-1][column-1]='X';
                    
                    printf("\t\t\t \t      SCREEN           \n\n");
                    printf("\t\t             1  2  3  4  5  6  7  8  9  10\n");
                    
                    printf("\n\t\t\t1   ");        
                    for(i=0;i<10;i++)
                    {
                        if(user[i].seat[0][i]>0)
                        printf(" %c ",user[i].seat[0][i]);
                    }
                    printf("\n\n");
                    
                    printf("\t\t\t2   ");
                    for(i=0;i<10;i++)
                    {if(user[i].seat[0][i]>0)
                        printf(" %c ",user[i].seat[1][i]);
                    }
                    printf("\n\n");
                  
                    printf("\t\t\t3   ");
                    for(i=0;i<10;i++)
                    {if(user[i].seat[0][i]>0)
                        printf(" %c ",user[i].seat[2][i]);
                    }
                    printf("\n\n");
                    
                    printf("\t\t\t4   ");
                    for(i=0;i<10;i++)
                    {if(user[i].seat[0][i]>0)
                        printf(" %c ",user[i].seat[3][i]);
                    }
                    printf("\n\n");
                   
                    printf("\t\t\t5   ");
                    for(i=0;i<10;i++)
                    {if(user[i].seat[0][i]>0)
                        printf(" %c ",user[i].seat[4][i]);
                    }
                    
                    printf("\n\n\n\n\n");
                    printf("\t\t\t   Enter Row Column(99,99 to quit): "); //choose to for 'X'  
                    scanf("%d,%d",&row, &column);     
                    if(row==99)
                            break;                  
        }
        clrscr();
        logo();
    	printf("\n\n\n\n\t");
    	gets(user[j].useless);
        printf("\n\tPlease Enter Your Name:");
    	gets(user[i].name);
    	printf("\n\tPlease Enter Owner IC Number:");
    	gets(user[i].nirc_number);
    	printf("\n\tPlease Enter Your Contact Number:");
    	gets(user[i].contact_number);
    	fopen("user.txt", "ab+");
        fwrite(&user[i],sizeof(user[i]),1,fptr);
        fclose(fptr);    
    	printf("\n\n\n\n");
    	printf("\n\tThank You For Providing Your Details");
    	printf("\n\tPress Anykey To Continue");
    	getch();
        thank_you();       
    }
    I edit it some but it still doesn't work. Whats wrong? I very bad at arrays.. sorry if i looks very stupid..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 11:10 AM
  2. Program Termination Problem
    By dacbo in forum C Programming
    Replies: 3
    Last Post: 01-23-2006, 02:34 AM
  3. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  4. Replies: 20
    Last Post: 06-12-2005, 11:53 PM