Thread: Check C Programming Code

  1. #1
    Registered User meeshvl's Avatar
    Join Date
    Sep 2014
    Posts
    3

    Question Check C Programming Code

    Please help, after I compile and run and pick no. 1 and y, it won't calculate the price? Please help. Also it won't let me select the seats.
    Here's the code:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    
    void printarray(const int[6][10]);
    float price();
    float seat();
    int i,j;
    int array[50][50]={1,1,1,2,1,3,1,4,1,5,1,6,1,7,1,8,1,9,1,10,2,1,2,2,2,3,2,4,2,5,2,6,2,7,2,8,2,9,2,10,3,1,3,2,3,3,3,4,3,5,3,6,3,7,3,8,3,9,3,10,4,1,4,2,4,3,4,4,4,5,4,6,4,7,4,8,4,9,4,10,5,1,5,2,5,3,5,4,5,5,5,6,5,7,5,8,5,9,5,10,6,1,6,2,6,3,6,4,6,5,6,6,6,7,6,8,6,9,6,10};
    float padult=12.00, pkids=8.00, total, adult, kids, tadult, tkids;
    char choice1, y, n;
    char decision1;
    char decision2;
    
    
    
    
    int main()
    {
        printf("\n\t\t\t WELCOME \n\t\t\t\t\tTO\n\t\t\t THE CINEMAS \n");
        printf("\n\t\t Please choose your movie: \n");
    
    
        int choice;
        printf("\n\t\t1. Finding Nemo ");
        printf("\n\t\t2. Transformers ");
        printf("\n\t\t3. Don't Break My Heart");
        printf("\n\t\t4. Meet The Robinson");
        printf("\n\t\t5. Good Night!!");
        printf("\n\t\t6. Cancel");
        printf(" \n\n\n Please choose 1 of the option: ");
        scanf("%d", &choice);
        
        switch(choice) {
                       case 1:
                               printf("\nTitle: Finding Nemo\n");
                            printf("Description:\n");
                            printf("About a fish name Nemo caught by a human. And his father tries to save him by travelling around the world. ");
                            printf("\nNemo is being caught and  unconciously being brought to Sydney, Australia..." );
                            printf("\nWill his father who never leave their home save Nemo... Will his father survive the travelling around the world.. ");
                            printf("\nGenre: Cartoon\n");
                            printf("Do you want to watch this movie:[y][n]\n");
                            scanf("%c",&choice1);
                            scanf("%c",&choice1);
                            if(choice1 == 'y'){
                                price();
                                break;
                            }
                            
                            else{
                                main();
                            }
                            break;
                            
                            
                       case 2:
                               printf("\nTitle: Transformers\n");
                            printf("Description:\n");
                            printf("Story starts when 5 robots that call themselves Autobots arrive on Earth. They tried to protect human from Decepticon which are evil.\nThey fought the Decepticon clan with all their might. Will the earth be saved by the Autobots...\nOr will the Earth fall to the hand of the Decepticon and human race extinct...");
                            printf("\nGenre: Scifi\n");
                            printf("Do you want to watch this movie:[y][n]\n");
                            scanf("%c",&choice1);
                            scanf("%c",&choice1);
                            
                            if(choice1 == 'y'){
                                price();
                                break;
                            }
                            
                            else{
                                main();
                            }
                            break;
                                        
                            
                       case 3:
                               printf("\nTitle: Don't Break My Heart...\n ");
                            printf("Description:\n");
                            printf("Don't Break My Heart is a story about a young girl named Elizabeth that doesn't believe in love anymore. At the age of 16, she's being played by the most handsome boy in her school.\nThe boy broke up with her in front of the school assembly making her embarrased to go to school after that situation. Luckily, a boy named Samuel like her but never reveal his true feelings.\nHe than help the girl to realise the happiness of love. Will the girl realise the true feelings of Samuel...");
                            printf("\nGenre: Romance\n");
                            printf("Do you want to watch this movie:[y][n]\n");
                            scanf("%c",&choice1);
                            scanf("%c",&choice1);
                            if(choice1 == 'y'){
                                price();
                                break;
                            }
                            
                            else{
                                main();
                            }
                            break;
                            
                            
                       case 4:
                            printf("\nTitle: Meet The Robinson\n");
                            printf("Description:\n");
                            printf("Meet The Robinson is a Disney cartoon that tells us about a lonely orphan is being attacked by his enemy from the future. He is than help by his own son from the future.\nHe has to help his son so his enemy does not change the past. He help his son defeat his true enemy by time travelling.\nHis enemy who's actually his childhood friend realise that he is being used. He then switches sides and defeated their true enemy.\nThis movie shows you how friendship help us defeat anything eventhough they are far away.");
                            printf("\nGenre: Cartoon\n");
                            printf("Do you want to watch this movie:[y][n]\n");
                            scanf("%c",&choice1);
                            scanf("%c",&choice1);
                            if(choice1 == 'y'){
                                price();
                                break;
                            }
                            
                            else{
                                main();
                            }
                            break;
                            
                            
                       case 5:
                            printf("\nTitle: Good Night!!\n");
                            printf("Description:\n");
                            printf("Good Night!! is a horror story about a ghost who live in peoples dream. Once he entered the dream, it will come out until the dreamer died.\nYou cannot get out of the dream unless you are awake before the final sign. If you get to see this ghost what will you do?\nFind out how they defeat the ghost inside the dream..");
                            printf("\nGenre: Horror\n");
                            printf("Do you want to watch this movie:[y][n]\n");
                            scanf("%c",&choice1);
                            scanf("%c",&choice1);
                            if(choice1 == 'y'){
                                price();
                                break;
                            }
                            
                            else{
                                main();
                            }
                            break;
                            
                            
                            case 6:
                                printf("\nThank you!!\nGood bye\n");
       
    
    
       default:
               printf("Invalid choice...\n");
               printf("\n\n Do you wish to continue?[y][n]: ");
               scanf("%c", &decision1);
               
               if(decision1 == 'y'){
                            main();
                            }
                            
               else {
                    printf("\n\n Thank You!!\nGoodbye\n");
                    break;
                    }
       break;
    }             
       system("pause");
       return 0;
    }
    
    
    
    
    float price(){
          printf("\n\tPrice:  Adult: RM12.00\n\tChildren: RM8.00\n");
          printf("How many adult: \n");
          scanf("%d", &adult);
          printf("How many kids: \n");
          scanf("%d", &kids);
          printf("You have %d adult and %d kids\n", adult, kids);
          total=(adult*padult)+(kids*pkids);
          printf("Select seats: \n");
          float seats();
          printf("Total= RM .2%f\n", total);
          return total;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why all the global variables? You need to learn to properly pass the variables to and from your functions as parameters.

    Next you need to increase your compiler warning level to let your compiler help you locate the source of the problems, and never ignore warnings.

    Here are the warnings I received when I compiled your code. All these warnings must be fixed.

    main.c|10|warning: missing braces around initializer [-Wmissing-braces]|
    main.c|10|warning: (near initialization for ‘array[0]’) [-Wmissing-braces]|
    main.c||In function ‘price’:|
    main.c|164|warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘float *’ [-Wformat=]|
    main.c|166|warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘float *’ [-Wformat=]|
    main.c|167|warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat=]|
    main.c|167|warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘double’ [-Wformat=]|
    ||=== Build finished: 0 errors, 6 warnings (0 minutes, 0 seconds) ===|
    Jim

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    In addition, you should use a loop instead of calling "main()" recursively.

    [edit] Posted here too -> arrays - Check C Programming Code - Stack Overflow

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    FYI: Beginners should use " %c" instead of "%c" most of the times with scanf; The leading space tells scanf to skip over newlines and spaces. Note: Adding a space in front of other format specifiers is NOT normally needed.

    Code:
    scanf("%c",&choice1);
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User meeshvl's Avatar
    Join Date
    Sep 2014
    Posts
    3

    Exclamation

    Quote Originally Posted by jimblumberg View Post
    Why all the global variables? You need to learn to properly pass the variables to and from your functions as parameters.

    Next you need to increase your compiler warning level to let your compiler help you locate the source of the problems, and never ignore warnings.

    Here are the warnings I received when I compiled your code. All these warnings must be fixed.



    Jim
    Mind fixing them for me, I have no idea what the warnings are saying.

  6. #6
    Registered User meeshvl's Avatar
    Join Date
    Sep 2014
    Posts
    3
    Quote Originally Posted by Matticus View Post
    In addition, you should use a loop instead of calling "main()" recursively.

    [edit] Posted here too -> arrays - Check C Programming Code - Stack Overflow
    Easier said than done. You do it then if it's so easy. cries

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by meeshvl
    Mind fixing them for me, I have no idea what the warnings are saying.
    Concentrate on the latter few first. They are telling you to check the code in the function named price, on lines 164, 166 and 167. In particular, look at the format string and see if the format specifications match the corresponding arguments.

    Quote Originally Posted by meeshvl
    Easier said than done. You do it then if it's so easy. cries
    It is quite easy for me to do, but that's because I've done similiar things before, many times. So, keep at it and you'll find that it will be so easy.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by meeshvl View Post
    Mind fixing them for me, I have no idea what the warnings are saying.
    What good will come from someone fixing your problems for you? Aren't you supposed to be learning this yourself?

    The warnings are saying that you are asking scanf to look for one type of input, but you're then telling it to store that input in a variable of the wrong type.

    For example, in line 164, you're using %d, which means a decimal integer, but then you're telling it to store that decimal integer in a variable of type float. If you want to use %d, use an int variable. If you want to use float, use %f.

    The other subsequent warnings are for similar issues.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can some one check this code for me?!
    By Gareth Morgan in forum C Programming
    Replies: 5
    Last Post: 11-27-2012, 03:55 PM
  2. Code Check
    By Dae in forum C++ Programming
    Replies: 12
    Last Post: 01-08-2009, 07:01 PM
  3. pLease check my programming~~
    By iedchan in forum C++ Programming
    Replies: 2
    Last Post: 04-15-2007, 10:23 AM
  4. Code Check
    By Ajsan in forum C++ Programming
    Replies: 0
    Last Post: 05-10-2004, 09:01 PM
  5. My programming website (check this out)
    By Leeman_s in forum C++ Programming
    Replies: 0
    Last Post: 11-12-2001, 08:29 PM

Tags for this Thread