Thread: Help With Function Call

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    3

    Help With Function Call

    Hey guys. First post on this forum, but I read most of the "first post" stickys so I bring this to you in the correct format. I usually just lurk looking for answers. New at C programming.

    Anyways, this issue is driving me insane. The full code is below, but when i try to call the 'remove()' function it says I have too few arguments. What am I missing??

    Let me know if you need anything else.


    Code:
    #include <stdio.h>
    
    //List of variables & function prototypes
    int menu();
    int add();
    int remove();
    int print();
    int sort();
    int minMax();
    int average();
    int menuChoice;
    
    int grades[14] = {100,50,50,100};
    int x;
    int y;
    char z;
    int currentTask;
    //End list of variables & func. prototypes
    
    
    //Beginning of main function
    main()
    {
        do
        {
            menu();
            switch(menuChoice)
            {
                case 1:
                    add();
                    break;
                case 2:
                    remove();
                    break;
            }
        }while(menuChoice != 7);
        
        printf("\n\nHave a nice day!\nGoodbye");
    }
    //End of main function
    
    
    //Function to call the menu
    int menu()
    {
        printf("\n\nWelcome to grades information system.\n");
        printf("Select one of the options:\n");
        printf("1) Add Grades\n");
        printf("2) Remove Grades\n");
        printf("3) Print Grades\n");
        printf("4) Sort Grades\n");
        printf("5) Find Max/Min Grades\n");
        printf("6) Calculate Average\n");
        printf("7) Quit\n");
        printf("\n\nEnter Your Choice:  ");
        scanf("%d", &menuChoice);
        fflush(stdin);
    }
    //End Menu Function
    
    
    //Function for adding grades
    int add()
    {
        printf("\nYou have chosen to Add Grades.\n");
        printf("\nYour current grades are:\n");
            for(x = 0; grades[x] > 0; x++)
            {
                if(grades[x] > 0)
                {
                printf("%d ", grades[x]);
                }
            }
            
        printf("\nFor a total of %d grades.\n", y);
        printf("\nEnter up to a total of 15 grades. Press enter after each entry.\n");
        printf("Enter 0 to exit: \n");
    
            do
            {
                x = x + 1;
                scanf("%d", &grades[x]);
                fflush(stdin);
            }    while(grades[x] > 0);
    
        printf("\nYou now have a total of %d grades.\n\n", x-1);
        printf("Sending you back to the home screen..\n\n");
        sleep(3);
    }
    //End function for adding grades
    
    //Function for removing grades
    int remove()
    {
        printf("\nYou have chosen to Remove Grades.\n");
        printf("\nYour current grades are:\n");
            for(x = 0; grades[x] > 0; x++)
            {
                if(grades[x] > 0)
                {
                printf("%d ", grades[x]);
                }
            }
            
        printf("\nFor a total of %d grades.\n", y);
        printf("\nEnter the grade that you want to remove. Press Enter after each input.\n");
        printf("Enter 0 to exit: \n");
    
            do
            {
                scanf("%d", &x);
                fflush(stdin);
                
                for(y = 0; grades[y] != 0; y++)
                {
                    if(grades[y] == x)
                    {
                        grades[y] = 0;
                    }
                    
                }
                            
            }while(x > 0);
            
        printf("\nYou now have a total of %d grades.\n\n", x-1);
        printf("Sending you back to the home screen..\n\n");
        sleep(3);    
    }

  2. #2
    Registered User
    Join Date
    Oct 2014
    Posts
    3
    Wow the code layout on here is different than the compiler that I am using. On here it shows remove() in pink so I looked up "remove function" and apparently this is already something. I just changed the name of it and it is working fine. Looks like I should make it a good practice to not have such 'basic' names.

    Thanks to anybody who has viewed this. Not sure if moderators will delete it.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143

    Function declarations which are not prototypes

    I am glad you found your root cause and you were able to correct it.

    There is one more lesson possible in your example. Most of your function declarations including the one for remove() are declarations but not prototypes. Your declaration
    Code:
    int remove();
    says that remove() is a function which returns an int and takes any number and type of parameters. This does not conflict with the prototype in stdio.h.

    The function prototype
    Code:
    int remove(void);
    would declare a function remove() which returns an int and takes no parameters. If you had used this prototype, I am pretty sure you compiler would be required to give a diagnostic since it does not match the declaration in stdio.h.

  4. #4
    Registered User
    Join Date
    Oct 2014
    Posts
    3
    I appreciate the feedback. I went through and played around with this a little bit. I didn't really understand too much about the input/output for functions tbh. I am taking a class on this at university now but I have a poor teacher so I am trying to gather information from other sources.

    I have another question if I can tag it along to prevent having to create a new thread. For the "remove grades" function I put comment marks around the specific problem that I'm having. Whenever i try to remove a grade it always removes the first one. Sorry for the layout of the variables, I have been playing with it for hours. When it goes into that inner do while loop it shouldn't enter the if statement but it always seem to.

    For example; I will run the program, press 2 to remove grades, enter 90 to remove it, enter 0 to exit. But it only removes the first grade.

    Thanks in advance.

    Code:
    #include <stdio.h>
    
    //List of variables & function prototypes
    int menu(void);
    int addGrades();
    int removeGrades(void);
    int printGrades(void);
    int sortGrades(void);
    int minMaxGrades(void);
    int averageGrades(void);
    int menuChoice;
    
    int grades[14] = {60,50,50,20,75,90,0,0,0,0,0,0,0,0};
    int x;
    int y;
    int z;
    int currentTask;
    //End list of variables & func. prototypes
    
    
    //Beginning of main function
    main()
    {
        do
        {
            menu();
            switch(menuChoice)
            {
                case 1:
                addGrades();
                break;
                case 2:
                removeGrades();
                break;
                case 3:
                printGrades();
                break;
                case 4:
                sortGrades();
                break;
                case 5:
                minMaxGrades();
                break;
                case 6:
                averageGrades();
                break;
            }
            
            int i;
            for(i = 0; i<15;i++)
            {
                printf("%d,", grades[i]);
            }
        }while(menuChoice != 7);
        
        printf("\n\nHave a nice day!\nGoodbye");
    }
    //End of main function
    
    
    //Function to call the menu
    int menu()
    {
        printf("\n\nWelcome to grades information system.\n");
        printf("Select one of the options:\n");
        printf("1) Add Grades\n");
        printf("2) Remove Grades\n");
        printf("3) Print Grades\n");
        printf("4) Sort Grades\n");
        printf("5) Find Max/Min Grades\n");
        printf("6) Calculate Average\n");
        printf("7) Quit\n");
        printf("\n\nEnter Your Choice:  ");
        scanf("%d", &menuChoice);
        fflush(stdin);
    }
    //End Menu Function
    
    
    
    //Function for adding grades
    int addGrades()
    {
        printf("\nYou have chosen to Add Grades.\n");
        printf("\nYour current grades are:\n");
            for(x = 0; x < 15; x++) 
            {
                
                if(grades[x] > 0)
                {
                printf("%d ", grades[x]);
                }
            }
            
        printf("\n\nEnter up to a total of 15 grades. Press enter after each entry.\n");
        printf("Enter 0 to exit: \n");
        
        int k = 1;
        int a = 0;
            do
            {
                if(grades[a] == 0)
                {
                    scanf("%d", &k);
                    grades[a] = k;
                    fflush(stdin);                
                }
                a = a + 1;
                
                if(a>15)
                {
                    break;
                }
                
            }while(k != 0);
    
        printf("\n\nSending you back to the home screen..\n\n");
        sleep(3);
    }
    //End function for adding grades
    
    
    
    //Function for removing grades
    int removeGrades()
    {
        
        printf("\nYou have chosen to Remove Grades.\n");
        printf("\nYour current grades are:\n");
            for(x = 0; x < 15; x++)
            {
                if(grades[x] > 0)
                {
                printf("%d ", grades[x]);
                }
            }
            
        printf("\n\nEnter the grade that you want to remove. Press Enter after each input.\n");
        printf("Enter 0 to exit: \n");
    
        int c = 0;
        int p;
    /////////////////////////////////This is where I'm having the issue
            do
            {
                scanf("%d", &p);
                fflush(stdin);
                do
                {
                    if (grades[c] == p);
                    {
                        grades[c] = 0;
                        break;
                    }                
                    c = c + 1;
                    
                }while(c < 15);    
                        
            }while(p > 0);
    //////////////////////////////////This is where I'm having the issue        
        printf("\nYour updated grades are:\n");
            for(x = 0; x < 15; x++)
            {
                if(grades[x] > 0)
                {
                printf("%d ", grades[x]);
                }
            }
            
        printf("Sending you back to the home screen..\n\n");
        sleep(3);    
    }
    //End function for removing grades
    
    
    
    //Function for printing grades
    int printGrades()
    {
        int i = 1;
        printf("\nYour current grades are:\n");
            for(x = 0; x < 15; x++)
            {
                if(grades[x] > 0)
                {
                printf("%d) %d\n", i, grades[x]);
                i = i + 1;
                
                }
            }
            
        printf("\n\nSending you back to the home screen..\n\n");
        sleep(3);    
    }
    //End function for printing grades
    
    
    
    //Function for sorting grades
    int sortGrades()
    {
        int i;
        int j;
        int k;
        //Starts sorting all of the numbers
        printf("\nYour current grades are:\n");
            for(i = 0;i < 15; i++)
            {
                for(j = 0;j < 15 - i; j++)
                {
                    if(grades[j] < grades[j+1])
                    {
                        k = grades[j];
                        grades[j] = grades[j+1];
                        grades [j+1] = k;
                    }
                }
            }
            //Starts printing all of the numbers above 0
            i = 1;
            for(x = 0; x < 15; x++)
            {
                if(grades[x] > 0)
                {
                printf("%d) %d\n", i, grades[x]);
                i = i + 1;
                }
            }
            
        printf("\n\nSending you back to the home screen..\n\n");
        sleep(3);    
    }
    //End function for sorting grades
    
    
    
    //Begin function to give min/max
    int minMaxGrades()
    {
        int max = grades[0];
        int i;
        for(i = 0; i < 15; i++)
        {    
            if(max < grades[i])
            {
                max = grades[i];
            }
        }
        printf("\n\nYour max grade is %d", max);
        
        int min = grades[0];
        for(i = 0; i < 15; i++)
        {    
            if(min > grades[i] && grades[i] != 0)
            {
                min = grades[i];
            }
        }    
        printf("\nYour min grade is %d", min);    
        
        printf("\n\nSending you back to the home screen..\n\n");
        sleep(3);
    }
    //End function to give min/max
    
    
    
    //Function to calculate average grade
    int averageGrades()
    {
        int i;
        float sum = 0;
        float divide = 0;
        
        for(i = 0; i < 15; i++)
        {
            if(grades[i] > 0)
            {
                sum = sum + grades[i];
                divide = divide + 1;
            }
            
        }    
        printf("\n\nYour average is %.1f", sum/divide);
        
        printf("\n\nSending you back to the home screen..\n\n");
        sleep(3);    
    }
    //End function to calculate average grade

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I had errors when I tried compiling your code: "undefined reference to sleep"

    "sleep()" is not a standard function. It also doesn't appear to serve much purpose in your code, so I just commented it out.

    Also, you shouldn't use "fflush()" to flush the input buffer. This is technically undefined behavior according to the standard.

    FAQ > Why fflush(stdin) is wrong - Cprogramming.com
    FAQ > Flush the input buffer - Cprogramming.com

    That leaves us with some warnings to be checked:

    Code:
    /*
    main.c|23|warning: return type defaults to 'int'|
    main.c||In function 'removeGrades':|
    main.c|150|warning: suggest braces around empty body in an 'if' statement|
    main.c||In function 'averageGrades':|
    main.c|288|warning: control reaches end of non-void function|
    main.c||In function 'minMaxGrades':|
    main.c|263|warning: control reaches end of non-void function|
    main.c||In function 'sortGrades':|
    main.c|232|warning: control reaches end of non-void function|
    main.c||In function 'printGrades':|
    main.c|194|warning: control reaches end of non-void function|
    main.c||In function 'removeGrades':|
    main.c|172|warning: control reaches end of non-void function|
    main.c||In function 'addGrades':|
    main.c|119|warning: control reaches end of non-void function|
    main.c||In function 'menu':|
    main.c|76|warning: control reaches end of non-void function|
    ||=== Build finished: 0 errors, 9 warnings ===|
    */


    The warning of "suggest braces around empty body in an 'if' statement" looks suspicious. It appears that you do have a body after that "if()" statement. Let's look more closely at that line:

    Code:
    if (grades[c] == p);
    {
        // ...
    }
    Oh no! You have a semi-colon after the "if". In C, a semi-colon by itself is considered a valid statement, which does nothing (hence it is referred to as a "null statement"). So your compiler actually sees this:

    Code:
    if (grades[c] == p)
        ;  // do nothing
    {
        // this code will always be executed since it is not part of the "if"
    }
    This appears to be the cause of the problem you're describing.



    The warning "control reaches end of non-void function" means that your function is supposed to return a value, but is not returning a value at all. This is technically undefined behavior. Either explicitly return a value, or if no values need to be returned, declare that function as void.

    This is true of several of your functions.



    You should also avoid using global variables.

    Global Variables Are Bad

    For instance, your "menu()" function reads an integer and stores it in the global variable "menuChoice". Instead, it should return the integer it reads, and you should assign that return value to a variable local in "main()":

    Code:
    // in "main()"
    
    int menuChoice;
    
    menuChoice = menu();
    There are other things that need fixing, but this should be enough for you to go on for now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function call Overhead and Function Call Stack
    By Alam Khan in forum C++ Programming
    Replies: 2
    Last Post: 04-26-2014, 08:28 AM
  2. Function Prototype, Function Call, and Function definition
    By dmcarpenter in forum C Programming
    Replies: 9
    Last Post: 04-09-2013, 03:29 AM
  3. Replies: 3
    Last Post: 09-22-2011, 09:59 PM
  4. Replies: 8
    Last Post: 07-08-2011, 01:16 PM
  5. Replies: 5
    Last Post: 10-17-2006, 08:54 AM