Thread: check code in function.

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    38

    check code in function.

    Hey ppl. im new to c programming and i just learnt function from my lecturer. im having trouble to find the mistake from my following codes.
    It is my assignment, to create candy crush but is in number crush style.

    do help me check my error. thanks



    Code:
    #include <stdio.h>
    #include <time.h>
    #include <math.h>
    #include <stdlib.h>
    
    #define rows 9
    #define columns 9
    
    void GetUsername();
    void Function(int myArray[rows][columns]);
    
    
    
    int main()
    
    {
    
            printf("\n################WELCOME###############\n");
            printf("\n################NUMBER CRUSH##################\n");
            printf("\n");
                    
                    
            return 0;
            
    }
            
            
    void GetUsername()
    
    {     
    
    char username[30];
        
            printf("Please enter your username to login\n");
            printf("\n");
            printf("\nUsername:");
            scanf("%s", &username);
            printf("\n");
            printf("Good day, %s and best of luck in crushing the numbers", username);
            printf("\n\n");
            
            
            
        return;
        
    }
    
    
    
        
    void Function(int myArray[rows][columns])
        
     
    {
    int x,y;
    int n;
    int myArray[rows][columns];
    srand(time(NULL));
        
                for(x=0; x<rows; x++)
                {
                
            
                    printf("\n");
            
                        for(y=0; y<columns; y++)
                        {
                    
                    
                        n=rand()%9+1;
                        printf("%6i\t", n);
                        
                              /*if(myArray[rows][columns-1] || myArray[rows-1][columns])
                                
                                    continue;
                                    
                                else
                                    break;*/
                        }
                
                
                printf("\n\n");
                
                  }
            
    
    
    
    
    
            Function(myArray);
        
            getch();
                        
            return 0;
    }                    
    
    
    
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2013
    Posts
    38
    i edited some errors already. however, i didnt get myArray out in the program. Help me! thanks




    Code:
    #include <stdio.h>
    #include <time.h>
    #include <math.h>
    #include <stdlib.h>
    
    #define rows 9
    #define columns 9
    
    void GetUsername();
    void Function(int myArray[rows][columns]);
    
    
    
    int main()
    
    {
    
            printf("\n################WELCOME###############\n");
            printf("\n################NUMBER CRUSH##################\n");
            printf("\n");
                    
                    
            return 0;
            
    }
            
            
    void GetUsername()
    
    {     
    
    char username[30];
        
            printf("Please enter your username to login\n");
            printf("\n");
            printf("\nUsername:");
            scanf("%s", &username);
            printf("\n");
            printf("Good day, %s and best of luck in crushing the numbers", username);
            printf("\n\n");
            
            
            
    
    }
    
    
    
        
    void Function(int myArray[rows][columns])
        
     
    {
    int x,y;
    int n;
    
    
    
    srand(time(NULL));
        
                for(x=0; x<rows; x++)
                {
                
            
                    printf("\n");
            
                        for(y=0; y<columns; y++)
                        {
                    
                    
                        n=rand()%9+1;
                        printf("%6i\t", n);
                        
                              /*if(myArray[rows][columns-1] || myArray[rows-1][columns])
                                
                                    continue;
                                    
                                else
                                    break;*/
                        }
                
                
                printf("\n\n");
                
                  }
            
    
    
            Function(myArray);
    
    
            
            return;
    }

  3. #3
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Your void Function function is never ending recursion, you need a base case to stop the recursion if that is your plan. Also, username is a string, in your scanf you just need to use the name of the array and drop the '&'. The name of an array( username ) is like saying &username[0].

  4. #4
    Registered User
    Join Date
    Oct 2013
    Posts
    38
    What's my error in it?
    check code in function.-untitled-jpg

    Code:
    
    
    #include <stdio.h>
    #include <time.h>
    #include <math.h>
    #include <stdlib.h>
    
    
    #define rows 9
    #define columns 9
    
    
    void GetUsername(char username[]);
    void Function(int myArray[rows][columns]);
    char Move(myArray[rows][columns]);
    
    
    
    
    
    
    
    
    int main()
    {
        char username[30]; /* u nid to have this here */
        int myArray[rows][columns]; /* u nid to have this here */
    
    
        printf("\n################WELCOME###############\n");
        printf("\n################NUMBER CRUSH##################\n");
        printf("\n");
        
        GetUsername(username);     /* pass username to function */
        Function(myArray); 
        Move(myArray);      /* pass myArray to function */    
                    
                    
        return 0;
            
    }
            
            
    void GetUsername(char username[30]) /*after u pass username the function ACCEPTS 
    username */
    {     
        printf("Please enter your username to login\n");
        printf("\n");
        printf("\nUsername:");
        scanf("%s", username);  /* for array, dont need to use &username for scanf */
        printf("\n");
        printf("Good day, %s and best of luck in crushing the numbers", username);
        printf("\n\n");
        
        
            
    }
    
    
    
    
    
    
        
    void Function(int myArray[rows][columns])
    {
        int x,y;
        int steps;
        int n;
            
        srand(time(NULL));
        
        for(x=0; x<rows; x++)
        {
            printf("\n");
            
            for(y=0; y<columns; y++)
            {
                n=rand()%9+1;
                printf("%6i\t", n);
            
                              /*if(myArray[rows][columns-1] || myArray[rows-1][columns])
                                
                                    continue;
                                    
                                else
                                    break;*/
            }
            
                printf("\n\n");
                
            }
          
          
          if(steps>0)
          printf("\n%i Step taken", steps);
          printf("\n\n");
          printf("Press 2 - Down\n");
          printf("Press 4 - Left\n");
          printf("Press 6 - Right\n");
          printf("Press 8 - Up\n");
          printf("Press Q to give up\n");
          
          }
          
    char move(myArray[rows][columns])
    {
        char move;
    
    
        do
        {
            fflush(stdin);
            move = getche();
                
            if(move == '2')
                down(myArray);    /*execute down function*/
                
            if(move == '4')    
                left(myArray);    /*execute left function*/
                
            if(move == '6')    
                right(myArray);    /*execute right function*/
                
            if(move == '8')    
                up(myArray);    /*execute up function*/
                            
            if(move == 'q')    
                quit();    /*execute quit in game function*/
                
            if(move == 'Q')
                quit();    /*execute quit in game function*/
                
        }while(move != '2' && move != '4' && move != '6' && move != '8');
    }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You have to give that array a type (look at the two lines above it).

  6. #6
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    What's that cat on your task bar?

  7. #7
    Registered User
    Join Date
    Oct 2013
    Posts
    38
    That's a quincy icon, the software i used for programming my first year. haha

  8. #8
    Registered User
    Join Date
    Oct 2013
    Posts
    38
    Oh ya, how careless i am. Thanks for that man! do i need to declare the left, right, up and down?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Xian Yao View Post
    Oh ya, how careless i am. Thanks for that man! do i need to declare the left, right, up and down?
    If you expect those to be function calls, then yes you need to write (and declare) the functions.

  10. #10
    Registered User
    Join Date
    Oct 2013
    Posts
    38
    Noted. Thanks man!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help! Check my code over.
    By omGeeK in forum C Programming
    Replies: 6
    Last Post: 11-04-2010, 11:34 PM
  2. check my c code.
    By rfm in forum C Programming
    Replies: 4
    Last Post: 09-24-2009, 05:47 AM
  3. Code Check
    By Dae in forum C++ Programming
    Replies: 12
    Last Post: 01-08-2009, 07:01 PM
  4. can someone check this code
    By xpflyer2002 in forum C Programming
    Replies: 5
    Last Post: 09-16-2002, 03:22 AM
  5. Can anyone check this code
    By a_learner in forum C Programming
    Replies: 5
    Last Post: 10-06-2001, 02:41 PM