Thread: creating functions

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    22

    creating functions

    i want to make the menu in this program a function so it will be at the bottom and you simply call it without it being in the main. I tried but when i did it didnt like the way i returned it to the switch...
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int genrand()
    {
        int random;
        random = 1+rand() % 999 ;
        while(random==0)random= 1+rand() % 999 ;
        return random;
    }
    void main()
    {
        srand (time(NULL));
        int a,ary[50],random;
        for(random=0;random<50;random++)
        {
            ary[random]='\0';
        }
        while(1)
        {
            printf("\n***************************************");
            printf("\n                MENU                   ");
            printf("\n1. Fill array with random numbers      ");
            printf("\n2. Print the array                     ");
            printf("\n3. Search the array                    ");
            printf("\n4. Sort the array                      ");
            printf("\n5. Quit                                ");
            printf("\n***************************************\n");
            scanf("%d",&a);
            switch(a)
            {
                case 1:
                {
                    for(random=0;random<50;random++)
                    {
                        ary[random]=genrand();
                    }
                    break;
                }
                case 2:
                {
                    for(random=0;random<50;random++)
                    {
                        printf("%d   ",ary[random]);
                    }
                    break;
                }
                case 3:
                {
                    int n,c=0;
                    printf("\nEnter search number: ");
                    scanf("%d",&n);
                    for(random=0;random<50;random++)
                    {
                        if(ary[random]==n){
                            printf("\nNumber is at position(s): %d",(random+1));
                            c=1;
                        }
                    }
                    if(c==0)printf("\nNumber doesn't exist.");
                    break;
                }
                case 4:
                {
                    int i;
                    int x;
                    int y;
    
    
                    for(i=0;i<50;i++)
                    {
                        for(x=0;x<49;x++)
                        {
                            if(ary[x]>=ary[x+1])
                            {
                                y=ary[x];
                                ary[x]=ary[x+1];
                                ary[x+1]=y;
                            }
                        }
                    }
                    break;
                }
                case 5:
                {
                    exit(0);
                }
                default:
                {
                    printf("\nINVALID CHOICE: ");
                }
            }
        }
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    So post the version where you tried to make the menu a function, the version you were having the problem with.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    22
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int genrand()
    
    
    
    
    {
        int menu(void);
        int random;
        random = 1+rand() % 999 ;
        while(random==0)random= 1+rand() % 999 ;
        return random;
    }
    void main()
    {
        srand (time(NULL));
    
    
        int a;
        int ary[50];
        int random;
    
    
        for(random=0;random<50;random++)
        {
            ary[random]='\0';
        }
        while(1)
        {
    
    
            menu()=switch(a)
            {
                case 1:
                {
                    for(random=0;random<50;random++)
                    {
                        ary[random]=genrand();
                    }
                    break;
                }
                case 2:
                {
                    for(random=0;random<50;random++)
                    {
                        printf("%d   ",ary[random]);
                    }
                    break;
                }
                case 3:
                {
                    int x;
                    int y=0;
                    printf("\nEnter search number: ");
                    scanf("%d",&x);
                    for(random=0;random<50;random++)
                    {
                        if(ary[random]==x){
                            printf("\nNumber is at position(s): %d",(random+1));
                            y=1;
                        }
                    }
                    if(y==0)printf("\nNumber doesn't exist.");
                    break;
                }
                case 4:
                {
                    int i;
                    int x;
                    int y;
    
    
                    for(i=0;i<50;i++)
                    {
                        for(x=0;x<49;x++)
                        {
                            if(ary[x]>=ary[x+1])
                            {
                                y=ary[x];
                                ary[x]=ary[x+1];
                                ary[x+1]=y;
                            }
                        }
                    }
                    break;
                }
                case 5:
                {
                    exit(0);
                }
                default:
                {
                    printf("\nINVALID CHOICE: ");
                }
            }
        }
    }
    
    
    int menu(void)
    {
        int a;
    
    
        printf("\n***************************************");
        printf("\n                MENU                   ");
        printf("\n1. Fill array with random numbers      ");
        printf("\n2. Print the array                     ");
        printf("\n3. Search the array                    ");
        printf("\n4. Sort the array                      ");
        printf("\n5. Quit                                ");
        printf("\n***************************************\n");
        scanf("%d",&a);
        return a;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > void main()
    Replace this with int main(), and add a return 0; at the end.

    > int menu(void);
    Move this up a few lines (so it's outside the function)

    > menu()=switch(a)
    Try say
    a = menu();
    switch( a );


    or
    switch( menu() )
    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.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    22
    ok thanks,
    i originally had the int menu(void) up a few lines out of the function but it says "declaration for parameter 'menu' but no such parameter but when i have it where within the function, it runs.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    22
    anything else that i could improve upon in this code? trying to be a "good" programmer not just one that can put stuff together and hope it works

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Make each case a separate function.
    Each function receives the array (and the array length).

    Code:
        srand (time(NULL));
        int a;
    Be sure you're compiling this as C.
    In traditional C, you're not allowed to put statements before declarations.

    C++ (and newer compilers supporting C99) allow this.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating Functions & passing information to other functions
    By RyanLeonard in forum C Programming
    Replies: 4
    Last Post: 10-28-2010, 12:17 PM
  2. creating and using functions
    By moy18 in forum C Programming
    Replies: 2
    Last Post: 11-01-2007, 04:26 AM
  3. creating and using functions
    By moy18 in forum C Programming
    Replies: 2
    Last Post: 10-31-2007, 09:09 PM
  4. Creating Functions
    By Rune Hunter in forum C# Programming
    Replies: 5
    Last Post: 03-03-2005, 07:22 PM
  5. Help with creating functions
    By OmnipotentCow in forum C Programming
    Replies: 8
    Last Post: 06-27-2003, 10:54 PM