Newbie- having trouble with functions

This is a discussion on Newbie- having trouble with functions within the C Programming forums, part of the General Programming Boards category; Can anyone help a rookie new to C who is working hard to figure out and get comfortable with functions?? ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    11

    Newbie- having trouble with functions

    Can anyone help a rookie new to C who is working hard to figure out and get comfortable with functions?? Below is my code for a program that is to prompt user for input to draw a shape by invoking the corresponding function. Right now I cannot get it to compile. I am getting an error "C:\Documents and Settings\Owner\My Documents\project2.c(70) : error C2143: syntax error : missing ';' before 'type' " [reference line is: int circle (int shape) ].
    Could someone please take a look at my code? I am still working to understand functions and even all the concepts of C. I have not been able to locate the source of the error and am also afraid even if I can that I may have used incorrect data types, called the functions incorrectly, etc. Any and all suggestions and assistance would be appreciated. Thanks,

    Code:
    /*This program will draw a shape based on a value entered*/
    /*by the user until the sentinel value is entered to terminate*/
    /* the program.  */
    #include <stdio.h>
    int circle (int shape);
    int triangle(int shape);
    int rectangle (int shape);
    int line(int shape);
    
    int main()
    {
    	
    int shape=0;  /* Initialize variable */
    
    printf("This program will draw a shape on your screen based on \n"); /*Prompt user for correct input*/
    printf("your selection from the following menu: \n \n");
    printf("1. Draw a circle. \n");
    printf("2. Draw a triangle. \n");
    printf("3. Draw a rectangle. \n");
    printf("4. Print one blank line. \n");
    printf("5. Exit program.\n \n");
    printf("Enter your choice: \n");
    scanf("%d", &shape); /* Read value user enters from keyboard */
    fflush(stdin);
      
      while (shape != 5) {  /*While loop to continue program until sentinel value is entered*/
    	  {
       printf("This program will draw a shape on your screen based on \n"); /*Prompt user for correct input*/
       printf("your selection from the following menu: \n \n"); /*Reprompts user for input each time loop is ran*/
       printf("1. Draw a circle. \n");
       printf("2. Draw a triangle. \n");
       printf("3. Draw a rectangle. \n");
       printf("4. Print one blank line.");
       printf("5. Exit program.");
       printf("Enter your choice: \n");
    
       scanf("%d", &shape); /* Read value user enters from keyboard */
       fflush(stdin);
    	  
       switch (shape)   /* switch nested in while to draw shape corresponding to the integer input*/
    		                 /*by a call issued to correct function */ 
    	   {
       case 1:
    	   printf("Circle printed below as requested: \n \n ", circle);
    	   break;
    
       case 2:
    	   printf("Triangle printed below as requested: \n \n", triangle);
    	   break;
    
       case 3:
    	   printf("Rectangle printed below as requested: \n \n", rectangle);
    	   break;
    
       case 4: 
    	   printf("Blank line printed below as requested: \n \n", line);
    	   break;
    
       default:
    	   printf("Incorrect value entered, please reenter. \n \n");
    	   break;
    	   }
    	  }
    	  
    printf("You have terminated the program.  Thank you for using the shapes program!\n");
    return 0;
    }	
          	
    	 int circle (int shape)
    	    {	
    		 printf("     * * *      \n");
    		 printf("    *      *     \n");
    		 printf("    *      *     \n");
    		 printf("     * * *      \n");
    		 return shape;
    	    }
    
    	 int triangle (int shape)	 
    	    { 
    		 printf("           *        \n");
    		 printf("       *      *     \n");
    		 printf("     *         *     \n");
    		 printf("    ********    \n");
    		 return shape;
    	    }
    
    	 int square (int shape)	 
    	    { 
    		 printf("    ********    \n");
    		 printf("    *            *    \n");
    		 printf("    *            *    \n");
    		 printf("    *            *    \n");
    		 printf("    ********    \n");
    		 return shape;
    	    }
    
    	int line(int shape)
    	    {
    		 printf("\n");
    		 printf("You have printed one blank line.");
    		 return shape;
    	    }

  2. #2
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    Your problem is simple. When you opened your while loop block you used two opening brackets, {. For instance:
    Code:
    while (shape != 5) {     /*While loop to continue program until sentinel value is entered*/
          {
    Also in your example, the function rectangle was not included. If it does not exist, you may recieve an error. Hope this helps.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Registered User computerfreaks's Avatar
    Join Date
    Jan 2005
    Posts
    30
    Correct me if I am wrong, but there are a couple of other problems with the code:

    The functions being called to print the shape have been delclared to take an integer parameter, but there isn't a parameter being called....

    also, it think the switch statements should be calling the functions like this:

    Code:
    case 'a': printf("Printing rectangle: \n\n");
                  rectangle();
    It doesn't appear that your shapes currently require a parameter to call them...
    Last edited by computerfreaks; 02-22-2005 at 08:45 AM.

  4. #4
    Registered User computerfreaks's Avatar
    Join Date
    Jan 2005
    Posts
    30
    obviously that should hav been

    case 1:,

    not

    case 'a': !!!

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You know you can edit your posts right?

    That aside...
    Code:
    fflush(stdin);
    The above is wrong. The origional poster should go read the FAQ on the topic, and never use it again. Also, you really should learn how to pay attention to your compiler's warning and error messages. They're there for a reason. If they're not there, turn them on.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User computerfreaks's Avatar
    Join Date
    Jan 2005
    Posts
    30
    Wasn't really thinking!! will remember that......

    on the note of fflush(stdin).... I was reading my book "SAMS: Teach yourself C in 21 Days" only to discover that the writer uses it all the time!!

    That really surprised me, no wonder so many use it incorrectly.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    37
    Vtc also has a C programming course, where they advise to use it. Also 'The art of computer programming" by Lawlor advises to use it.
    I only learnt not to use it here...

    I wonder where it came from

  8. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by computerfreaks
    Wasn't really thinking!! will remember that......

    on the note of fflush(stdin).... I was reading my book "SAMS: Teach yourself C in 21 Days" only to discover that the writer uses it all the time!!

    That really surprised me, no wonder so many use it incorrectly.
    They will tell you to use feof() to control a loop, as well as give plenty of examples of global variables (not that global variables are evil, but in many of the examples they were not necessary). There could be other little things there too, but I can't remember...

    ~/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 02:34 PM
  2. Replies: 7
    Last Post: 05-25-2006, 12:51 PM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  4. Newbie asking about overloading functions
    By GLR in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2001, 09:54 AM
  5. trouble with functions with &
    By diaperdandy in forum C++ Programming
    Replies: 4
    Last Post: 12-25-2001, 09:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21