Thread: my array prints out junk...Help..its a movie ticket and pop corn redemption program

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    23

    my array prints out junk...Help..its a movie ticket and pop corn redemption program

    i have initialized my rows and columns..with statements written correctly...i have no idea why it print out junk integers... the rows have exceeded the limit..It is suppose to print only 4 rows....the admin number does not print out the numbers that i scanf in....HELP!

    my array prints out junk...Help..its a movie ticket and pop corn redemption program-picture1-jpg





    #include<stdio.h>
    void main()
    {
    char letter;
    char Results[4][3],row,column;
    int count;
    int admin[5];
    int no_of_movie_tickets;
    int no_of_pop_corn_packs;
    int points;



    printf("XYZ-ABC ONLINE MOVIE TREATS REDEMPTION SYSTEM \n"); //main menu
    printf("(A) INPUT Redemption Status Check \n");
    printf("(B) Redemption Status Check \n");
    printf("(C) Display succesful redemptions \n");
    printf("(D) Quit \n");


    flushall();



    {
    for( int count=0; count<=4; count=count++) //Function for case A, allows only 5 redemptions
    {

    printf("Please input your Admin No:"); //ask user for admin no
    scanf("%d",&admin[count]);



    printf("Number of movie tickets:");
    scanf("%d",&no_of_movie_tickets); //ask user for no of movie tickets


    printf("Number of pop corn packs:");
    scanf("%d",&no_of_pop_corn_packs); //ask user for no of pop corn packs
    printf("\n");




    points = no_of_movie_tickets * 125 + no_of_pop_corn_packs * 75;
    if(points<500)
    {
    printf("Redemption Status: Successful\n");
    printf("Admin No:%d\n",admin[count]);
    printf("Number of movie tickets:%d\n",no_of_movie_tickets);
    printf("Number of pop corn packs:%d\n",no_of_pop_corn_packs);
    printf("Redemption points used:%d\n\n",points);
    }
    else
    {
    count = count - 1;
    printf("Redemption Status: Unsuccessful\n");
    }
    }
    printf("Admin No.\t\t\tTickets\t\t\tPop_corn\t\t\tPoints");
    for(int row=0;row<=4; row=row+1)
    {
    for( int column=0; column<=3; column=column+1)
    printf("%d\t\t\t%d\t\t\t%d\t\t\t%d",admin,no_of_mo vie_tickets,no_of_pop_corn_packs,points);

    }
    }

    printf("Please try again tomorrow. Thankyou\n"); // after 5 successful attemptions, prints this out
    }
    Attached Files Attached Files
    Last edited by smeraldan; 02-09-2012 at 03:43 AM.

  2. #2
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    70
    First up all, Do not attach any file in this forum. Read the Rules.
    I do not know what is the logic behind the below line.
    Anyway I found out some small things. Red color means changes, Green Color means new addition on your code
    Code:
    printf("Admin No.\t\tTickets\t\tPop_corn\t\tPoints\n");
    for(int row=0;row<=4; row=row+1)
    {
    for( int column=0; column<=3; column=column+1)
    printf("%d\t\t\t%d\t\t\t%d\t\t\t%d\n",admin[column],no_of_mo vie_tickets,no_of_pop_corn_packs,points);
    }

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Attaching files is not against the rules.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    70
    Quote Originally Posted by whiteflags View Post
    Attaching files is not against the rules.
    Then what is the purpose of Code Tags?
    Announcements - General Programming Boards

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    That has ABSOLUTELY NOTHING to do with whether or not attachments are okay.

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    23
    Quote Originally Posted by ArunS View Post
    First up all, Do not attach any file in this forum. Read the Rules.
    I do not know what is the logic behind the below line.
    Anyway I found out some small things. Red color means changes, Green Color means new addition on your code
    Code:
    printf("Admin No.\t\tTickets\t\tPop_corn\t\tPoints\n");
    for(int row=0;row<=4; row=row+1)
    {
    for( int column=0; column<=3; column=column+1)
    printf("%d\t\t\t%d\t\t\t%d\t\t\t%d\n",admin[column],no_of_mo vie_tickets,no_of_pop_corn_packs,points);
    }
    i've tried it the program block shuts after keying in all the data???

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I don't understand what you're saying.

  8. #8
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    70
    i've tried it the program block shuts after keying in all the data???
    what do you mean?

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Although I have to say, the bit below would make a lot more sense if the results where in some kind of matrix. I can see that you were thinking about that in your code, but if the data is not actually in a matrix, then you shouldn't really think about output in terms of rows and columns. You will end up printing too much stuff. You only have four data points anyway.
    Code:
    for(int row=0;row<=4; row=row+1)
    {
    for( int column=0; column<=3; column=column+1)
    printf("%d\t\t\t%d\t\t\t%d\t\t\t%d",admin,no_of_mo vie_tickets,no_of_pop_corn_packs,points);
    
    }
    If it were me I would put every variable in the point formula in a structure and call that my redemption structure. That organizes the data. Then you can either process the points one at a time or build an array of structures. When you go to print a structure, simply output each member with printf...

  10. #10
    Registered User
    Join Date
    Feb 2012
    Posts
    23
    Quote Originally Posted by whiteflags View Post
    Although I have to say, the bit below would make a lot more sense if the results where in some kind of matrix. I can see that you were thinking about that in your code, but if the data is not actually in a matrix, then you shouldn't really think about output in terms of rows and columns. You will end up printing too much stuff. You only have four data points anyway.
    Code:
    for(int row=0;row<=4; row=row+1)
    {
    for( int column=0; column<=3; column=column+1)
    printf("%d\t\t\t%d\t\t\t%d\t\t\t%d",admin,no_of_mo vie_tickets,no_of_pop_corn_packs,points);
    
    }
    If it were me I would put every variable in the point formula in a structure and call that my redemption structure. That organizes the data. Then you can either process the points one at a time or build an array of structures. When you go to print a structure, simply output each member with printf...
    what exactly do u mean by putting every variable in point formula?Do i make these 3 variable to a main variable?

  11. #11
    Registered User
    Join Date
    Feb 2012
    Posts
    23
    basically..what i meant was..when i run it( adding yr green code to admin), it was running smoothly then suddenly, the arraying cause the program to force close..and the program pops out and restarts again....

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by smeraldan View Post
    what exactly do u mean by putting every variable in point formula?Do i make these 3 variable to a main variable?
    In your code, you did this
    Code:
    points = no_of_movie_tickets * 125 + no_of_pop_corn_packs * 75;
    You got those three variables from the user. I want you to make all three of those into a struct like this
    Code:
    struct redemption
    {
       int no_of_movie_tickets;
       int no_of_pop_corn_packs;
       int admin;
    };
    
    /* in main: */
    struct redemption example;
    example.no_of_movie_tickets = 2;
    example.no_of_pop_corn_packs = 1;
    example.admin = 12345;
    Then you make four of them in an array, since you want the user to enter four; unless you want to process one redemption at a time, then you can get away with one instance and using it over and over.

    Suddenly you know exactly what to get and exactly what to print because all of the data for one redemption is together. And you don't have to worry about using a matrix like this:
    Code:
    char Results[4][3]
    which a lot of the code you wrote suggests you were trying to do to solve the problem. If you forget about writing matrix code and rewrite some things, you'll fix your problem.

  13. #13
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    70
    Have you tried only admin[column] or what exactly did changes on your code?
    Post your changed code only and also post the results

  14. #14
    Registered User
    Join Date
    Apr 2011
    Location
    dust
    Posts
    70
    Quote Originally Posted by smeraldan View Post
    basically..what i meant was..when i run it( adding yr green code to admin), it was running smoothly then suddenly, the arraying cause the program to force close..and the program pops out and restarts again....
    What is the exact output do you want?

  15. #15
    Registered User
    Join Date
    Feb 2012
    Posts
    23
    Quote Originally Posted by ArunS View Post
    What is the exact output do you want?
    i want the output to print out all the items i've keyed in, the admin no, the no_of_movie_tickets,the no_of_pop_corn and total points 1 column for each....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 10-27-2011, 11:25 AM
  2. Replies: 2
    Last Post: 09-06-2011, 01:08 AM
  3. we have corn!!
    By theor23 in forum C Programming
    Replies: 6
    Last Post: 02-03-2011, 10:49 PM
  4. Array fills with junk - help
    By Lucky Luke in forum C Programming
    Replies: 1
    Last Post: 12-13-2009, 05:13 PM
  5. Replies: 12
    Last Post: 02-28-2008, 06:19 PM