Thread: please check my code, dont know whats wrong

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    3

    please check my code, dont know whats wrong

    Right now, I'm in the middle of a project and wrote a code for it. When i run the code below on its own it runs fine

    #include <stdio.h>
    #define DIM 512
    #define MAXCHAR 30

    Code:
    int main()
    {
        FILE *myfile = NULL;
        char bknum[DIM], customer[MAXCHAR], movie[MAXCHAR], date[DIM], time[DIM];
        char guest[DIM], house[DIM], ticket[MAXCHAR], fee[DIM];
        
        myfile = fopen("project.txt", "a+");
        if (myfile == NULL)
        {
            printf("File open failed!! Exiting!!\n");
            return 1;
        }
        
        printf("Please enter Booking Number:\n");
        fgets(bknum, DIM, stdin);
        printf("Please enter Customer Name:\n");
        fgets(customer, MAXCHAR, stdin);
        printf("Please enter Movie Name:\n");
        fgets(movie, MAXCHAR, stdin);
        printf("Please enter the Movie Date:\n");
        fgets(date, DIM, stdin);
        printf("Please enter Movie Time\n");
        fgets(time, DIM, stdin);
        printf("Please enter Number of Guests:\n");
        fgets(guest, DIM, stdin);
        printf("Please enter House Number:\n");
        fgets(house, DIM, stdin);
        printf("Please enter Ticket Types:\n");
        fgets(ticket, MAXCHAR, stdin);
        printf("Please enter Total Fee:\n");
        fgets(fee, DIM, stdin);
        
        fprintf(myfile, "\n%s", bknum);
        fprintf(myfile, "%s", customer);
        fprintf(myfile, "%s", movie);
        fprintf(myfile, "%s", date);
        fprintf(myfile, "%s", time);
        fprintf(myfile, "%s", guest);
        fprintf(myfile, "%s", house);
        fprintf(myfile, "%s", ticket);
        fprintf(myfile, "%s", fee);
        
        fclose (myfile);
        
        
        return 0;
        
    }
    But when I put it back on its back bone (main, where its supposed to be), it has some bugs. The main code is below,

    Code:
    #include <stdio.h>
    #define DIM 512
    #define MAXCHAR 30
    int main()
    {
        void op1();
        
        int option;
        
        printf("  *** Welcome to HK Grand SPACE Movie Ticketing Management System 2017 ***\n");
        printf("  *** This system is developed by CCIT4020 Class No.CL-?? Group No.?? ***\n");
        printf("\n--<Basic Functions>--\n");
        printf("1. Add New Movie Ticketing Record(s) \n");
        printf("2. Display ALL Movie Ticketing Records \n");
        printf("3. Modify Movie Ticketing Record(s) \n");
        printf("4. Search Movie Ticketing Record(s) \n");
        printf("5. Delete Movie Ticketing Record(s) \n");
        
        printf("What is your Option <1-5>?");
        scanf("%d",&option);
        
        switch (option)
        {
            case 1:
                op1();
                break;
                
            case 2:
                
                break;
            
            case 3:
                
                break;
                
            case 4:
                
                break;
                
            case 5:
                
                break;
        }
    }
    
    
    void op1()
    {
        FILE *myfile = NULL;
        char bknum[DIM], customer[MAXCHAR], movie[MAXCHAR], date[DIM], time[DIM];
        char guest[DIM], house[DIM], ticket[MAXCHAR], fee[DIM];
        
        myfile = fopen("project.txt", "a+");
        if (myfile == NULL)
        {
            printf("File open failed!! Exiting!!\n");
        }
        
        printf("Please enter Booking Number:\n");
        fgets(bknum, DIM, stdin);
        printf("Please enter Customer Name:\n");
        fgets(customer, MAXCHAR, stdin);
        printf("Please enter Movie Name:\n");
        fgets(movie, MAXCHAR, stdin);
        printf("Please enter the Movie Date:\n");
        fgets(date, DIM, stdin);
        printf("Please enter Movie Time\n");
        fgets(time, DIM, stdin);
        printf("Please enter Number of Guests:\n");
        fgets(guest, DIM, stdin);
        printf("Please enter House Number:\n");
        fgets(house, DIM, stdin);
        printf("Please enter Ticket Types:\n");
        fgets(ticket, MAXCHAR, stdin);
        printf("Please enter Total Fee:\n");
        fgets(fee, DIM, stdin);
        
        fprintf(myfile, "\n%s", bknum);
        fprintf(myfile, "%s", customer);
        fprintf(myfile, "%s", movie);
        fprintf(myfile, "%s", date);
        fprintf(myfile, "%s", time);
        fprintf(myfile, "%s", guest);
        fprintf(myfile, "%s", house);
        fprintf(myfile, "%s", ticket);
        fprintf(myfile, "%s", fee);
        
        fclose (myfile);
        
        
    }
    The thing I'm trying to do is to accept a few pieces of information, then putting those information into project.txt. On its own, the code runs fine, no bugs. But when I run the main part, it will ask me to choose an option (op1), then it will call the code "void op1()", to run it (that is how its supposed to be), the problem comes after, the user inputs are supposed to come one at a time, for some reason "enter a booking number" & " enter customers name" show up together.

    Please help me try to fix this and let me know the problem as well. Thank a lot

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    Stepping through your code, if you look at bknum, it is getting the newline character from the option entry. You need to skip over that I think. Usually when user input gets skipped this is what is happening. To find this error I just put the variable in the watch section of the debugger and stepped through the code one line at a time.

    To be more specific about what is happening. (I may be confusing streams and buffers, sorry about that.)
    Code:
    scanf("%d", option);
    waits until the user enters data into the stdin stream and terminates this entry by hitting enter. Then the %d part of scanf pulls out an integer from that stream and momentarily ignores the rest. At this point you could have more scanf statements if you want to to further pull data out of the stream. So I suppose scanf or for that matter fgets doesn't necessarily wait for the user to enter data. It only waits if the stream is empty. If you want it to wait, you have to make sure the stream is empty.

    Another thing though, your code was at first a little confusing to me where you put the op1 prototype inside main. Generally people put their prototypes above it.
    Last edited by jack jordan; 11-02-2017 at 04:28 AM.

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    3
    sorry if im coming out a bit rude here.
    this is what i understood so far,
    what you are saying is that, it only waits if the stream is empty. then how do it empty it?
    Another thing i dont quite understand what you mean by "it is getting a newline character form the option entry"

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    I do not even see a return for your main function type int. I wish compilers would catch that error.
    if you have not already done so.
    Code:
    void op1()
    {
        FILE *myfile = NULL;
        char bknum[DIM], customer[MAXCHAR], movie[MAXCHAR], date[DIM], time[DIM];
        char guest[DIM], house[DIM], ticket[MAXCHAR], fee[DIM];
         
        myfile = fopen("project.txt", "a+");
        if (myfile == NULL)
        {
            printf("File open failed!! Exiting!!\n");
        }
         
        printf("Please enter Booking Number:\n");
        
        scanf("%s", bknum);
        
        //    fgets(bknum, DIM, stdin);
        printf("Please enter Customer Name:\n");
        
            scanf("%s", customer);
       // fgets(customer, MAXCHAR, stdin);
        printf("Please enter Movie Name:\n");
        scanf("%s", movie);
        //fgets(movie, MAXCHAR, stdin);
        printf("Please enter the Movie Date:\n");
       scanf("%s", date);
       // fgets(date, DIM, stdin);
        printf("Please enter Movie Time\n");
        scanf("%s",time);
        //fgets(time, DIM, stdin);
        printf("Please enter Number of Guests:\n");
        scanf("%s", guest);
        //fgets(guest, DIM, stdin);
        
        printf("Please enter House Number:\n");
        scanf("%s", house);
        //fgets(house, DIM, stdin);
        printf("Please enter Ticket Types:\n");
        scanf("%s",ticket );
        //fgets(ticket, MAXCHAR, stdin);
        printf("Please enter Total Fee:\n");
        //fgets(fee, DIM, stdin);
         scanf("%s", fee);
         
        fprintf(myfile, "\n%s\n", bknum);
        fprintf(myfile, "%s\n", customer);
        fprintf(myfile, "%s\n", movie);
        fprintf(myfile, "%s\n", date);
        fprintf(myfile, "%s\n", time);
        fprintf(myfile, "%s\n", guest);
        fprintf(myfile, "%s\n", house);
        fprintf(myfile, "%s\n", ticket);
        fprintf(myfile, "%s\n", fee);
         
        fclose (myfile);
         
         
    }
    results
    Code:
    2222
    sally
    My_Movie
    19-32-3012
    5:23AM
    9899
    32
    private
    3.00
    changed them all to scanf and put \n end lines in your fprintf 's to get it to go to new line. done.

    you can still put size specifiers in your scanf so not to over run buffers.
    Last edited by userxbw; 11-02-2017 at 07:00 AM.

  5. #5
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    Okay, let me try again.
    Code:
    scanf("%d", option);
    is the first line which waits for data from the stdin stream, so it waits for the user to enter data from the keyboard. If the user presses the key 1 and the enter key, that is not one character entered into stdin, but two. What gets sent to stdin is "1\n". The above scanf line takes out the "1" because it is an int, but it leaves "\n" in the stream because it is not an int. The next scanf or fgets you use will first look at what is already in stdin before waiting for more data from the keyboard. Since stdin is still not empty, these type of commands will pull out the "\n" from the buffer. Since \n is used as a terminator for these commands, they will think that they have done what they are supposed to do in just getting "\n" and continuing on to the next line.

    The code would work differently if you used
    Code:
    char option[DIM];
    scanf("%s", option);
    because unlike %d, %s gets both the integer AND the newline character out of the stream. You might have to keep in mind though that what you have is an ASCII 1, which is equivalent to decimal 49 as well as a newline character.

    There are many other options you might try as well. You could use fgets to put all keyboard data into your own buffer and then pull out the useful information yourself. You could fflush(stdin), though I've read that is supposed to be bad as it is intended for output streams. You could do something like
    Code:
    char garbage = getchar()
    to pull out the newline, or some equivalent scanf or fgets.

    I suppose the option I would choose myself is to manage my own buffer. It seems a more sound idea than to expect garbage.
    Code:
    #include <stdlib.h>
    char sentence[DIM];
    char* stringPtr = NULL;
    fgets(sentence, DIM, stdin);
    long option = strtol(sentence, stringPtr, 0);
    although that is only if you want to do more with the buffer after. If you know the first part of the buffer is the only thing you want, you don't need to use a pointer and can just go with:
    Code:
    #include <stdlib.h>
    char sentence[DIM];
    fgets(sentence, DIM, stdin);
    long option = strtol(sentence, NULL, 0);
    Just to be clear about what is happening in the above code, fgets will put the entire stdin buffer into sentence which makes stdin remain empty while you still have the data to work with. strtol gets the first part of sentence out, converts it into a long, and stores that into option.
    Last edited by jack jordan; 11-02-2017 at 11:46 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > because unlike %d, %s gets both the integer AND the newline character out of the stream.
    Are you sure?

    \n counts as white-space, which %s ignores.
    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.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The code would work differently if you used
    What? The scanf() with a "%s" specifier will stop processing the string at the first whitespace character leaving that whitespace character in the input buffer.

  8. #8
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    \n counts as white-space, which %s ignores.
    I guess I was wrong. Thanks. Just doing a test.
    Code:
    printf("What is your Option <1-5>?");
    char option[DIM];
    scanf("%s", &option);
    char remain0;
    scanf("%c", &remain0);
    char remain1;
    scanf("%c", &remain1);
    shows that option gets "1" with no newline character, remain0 gets a newline char, and remain1 waits for more data.

  9. #9
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by userxbw View Post
    I do not even see a return for your main function type int. I wish compilers would catch that error.
    It's not an error... it's defined behaviour that code execution reaching the closing brace of main() is equivalent to return 0;

  10. #10
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Hodor View Post
    It's not an error... it's defined behaviour that code execution reaching the closing brace of main() is equivalent to return 0;
    still it is nice to see it, or gezz I wonder why they backdoor it so it would default to that instead

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BST check function..whats wrong?
    By roy.42 in forum C Programming
    Replies: 6
    Last Post: 08-18-2007, 05:28 AM
  2. dont know whats wrong
    By otchster in forum C Programming
    Replies: 2
    Last Post: 11-02-2005, 11:14 AM
  3. Anyone check whats wrong with my code?
    By jr2007 in forum C++ Programming
    Replies: 6
    Last Post: 11-02-2003, 09:36 AM
  4. Code: Tell me whats wrong !
    By slx47 in forum C++ Programming
    Replies: 3
    Last Post: 05-09-2002, 04:39 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM

Tags for this Thread