0

Here is the code from my program that has a issue:

    #include "stdio.h"
    int main(void)
    {
    int funcnum;

    printf("Welcome \n");
    printf("Please enter a number\n");
    scanf("%i",&funcnum);

    switch(funcnum)  //funcnum is the variable you are checking for a match
    {          //Open curly!
        case 1:  // is funcnum==1?
            printf("You entered 1. This is now the Turkey Time function.\n"); // if funcnum==1, this will happen.
            {
              //DECLARE the "cookTime" function.(Above, outside the MAIN function)
              //It will return a float, and is expecting two floats.
              float cookTime (float);

              //Below is a "global" variable -meaning available to all functions. These are declared outside of any function.
              float cTim;

              float weight;

              printf("Welcome to the turkey timer...\n");

              printf("Please enter a turkey weight \n");
              scanf("%f",&weight);

              cookTime (weight); //Calling (jumping to) the cookTime function and sending it "weight" variable.  

              printf("Cooking time is %.1f minutes.\n\n",cTim); //printing the returned value cTim.
              printf("\tThank you for choosing the MaiCorp Timing System, don't forget the gravy! \n");

              //DEFINE the  function. Note -no semicolon. (just like in main's definition above!)
              float cookTime (float w)
              {   
                cTim = w*15;
                return cTim; //We are sending cTim back to where we left Main.
              }
            }
            break; //break makes the statement end and jump out of the curlies.
        case 2:  // is funcnum==2?
            printf("You entered 2. This is now the Area function.\n");
            {
              //DECLARE the "area" function.(Above, outside the MAIN function)
              //Looking at the declaration we can see that this function will return an int, and is expecting two int's.
              int area (int, int);

              //Here we declare a global variable.  Meaning a variable that is available to all functions. These are declared outside of any function.
              int ans;

              int len,wid;

              printf("Welcome to the rectangle area calculator...\n");
              printf("Please enter a length\n");
              scanf("%i",&len);
              printf("Please enter a width\n");
              scanf("%i",&wid);


              area (len,wid);    //Calling the "area" function, sending it the len and wid integers..

              printf("Area is %i.\n",ans); //printing the returned value "ans"

              //DEFINE the area function. Note -no semicolon. (just like in main's definition above!)

              int area (int L, int W)
              {
                ans = L*W;
                return ans; 
              }
            }
            break;
        default:    //default catches all non matches.
            printf("You did not enter 1 or 2, meaning that you are not running a function this time.\n");
            break;
    }  //close curly!
    return 0;
}

When I run this program, the gcc version 4.6.3 compiler gives this:

main.c: In function 'main':
main.c:35:21: error: static declaration of 'cookTime' follows non-
static declaration
           float cookTime (float w)
                 ^~~~~~~~
main.c:17:21: note: previous declaration of 'cookTime' was here
           float cookTime (float);
                 ^~~~~~~~
main.c:67:19: error: static declaration of 'area' follows non-static 
declaration
           int area (int L, int W)
               ^~~~
main.c:47:19: note: previous declaration of 'area' was here
           int area (int, int);
               ^~~~

exit status 1

This program is written in C in case anyone needs to know the programming language that the program is written in. I have tried to fix the program by putting in "{}"'s and other code but it came to be of no use (meaning that the error did not resolve). It would be great if a reputable programmer can assist me with this issue.

  • Why do you have `(Above, outside the MAIN function)` inside the main function? โ€“ M.M Sep 26 '17 at 04:43

2 Answers2

1

It looks like you are declaring a function within the main...? That is not only bad practice, but judging by the code there it looks like it's illegal. Unless you put static in front of it.

If you want to use the function, do NOT put it's return type before using the function. Instead of:

int area( int L, int W);

use

area(int L, int W);

Really weird to define a function within the main. Like I said, I don't think it's allowed, but if you REALLY want to do it, I would suggest putting static in front of the function.

Better yet, make a Name_goes_here.h file and put the functions in there. Then,

#include "Name_goes_here.h"

and use the functions like I told you. (except instead of int L, int W, replace it with pre-declared variables L and W without int in front of it.)

  • You have a good answer, but your advise on "*leave it `int main()` rather than the void stuff*" is simply wrong. [**See What should main() return in C and C++?**](http://stackoverflow.com/questions/204476/) Here is the standard reference, See: [**C11 Standard (draft n1570) ยง5.1.2.2.1 Program startup**](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf). โ€“ David C. Rankin Sep 26 '17 at 05:25
  • Thanks, I changed it โ€“ Courtney Maroney Sep 26 '17 at 17:44
0

the following proposed code:

  1. cleanly compiles.
  2. properly prototypes the sub functions.
  3. properly declares the sub functions.
  4. eliminates most commentary that is just a repeat of the code.
  5. Uses appropriate vertical spacing (between code blocks, between functions, between code activities).
  6. fails to check the returned value from system functions (I.E. scanf()).
  7. Follows the axiom: only one statement per line and (at most) one variable declaration per statement.
  8. respects the right boundary of a printed output of the listing.
  9. properly writes a 'float' literal

and now the proposed code:

#include "stdio.h"


// prototypes
int   area (int, int);
float cookTime (float);


int main(void)
{
    int funcnum;

    printf("Welcome \n");
    printf("Please enter a number\n");
    scanf("%i",&funcnum);

    switch(funcnum)  
    {
        case 1:  
            printf("You entered 1."
                   " This is now the Turkey Time function.\n"); 

            float weight;

            printf("Welcome to the turkey timer...\n");

            printf("Please enter a turkey weight \n");
            scanf("%f",&weight);

            float cTim = cookTime (weight);

            printf("Cooking time is %.1f minutes.\n\n",cTim);
            printf("\tThank you for choosing the MaiCorp Timing System, don't forget the gravy! \n");
            break;

        case 2:  
            printf("You entered 2."
                   " This is now the Area function.\n");

            int len;
            int wid;

            printf("Welcome to the rectangle area calculator...\n");

            printf("Please enter a length\n");
            scanf("%i",&len);

            printf("Please enter a width\n");
            scanf("%i",&wid);

            int ans = area (len,wid);

            printf("Area is %i.\n",ans);
            break;

        default:    
            printf("You did not enter 1 or 2,"
                   " meaning that you are not running"
                   " a function this time.\n");
            break;
    }

    return 0;
} // end function: main


int area (int L, int W)
{
    int ans = L*W;
    return ans;
} // end function: area


float cookTime (float w)
{
    float cTim = w*15.0f;   // << note the proper 'float' literal
    return cTim;
} // end function: cookTime
user3629249
  • 15,593
  • 1
  • 16
  • 17