Thread: problem with functions

  1. #1
    Registered User
    Join Date
    Feb 2006
    Location
    Philadelphia, PA
    Posts
    27

    problem with functions

    Hello,

    I am trying to make a program that prints out a square with width defined by the user. The program compiles, and asks user for width, but after that nothing happens. What am I doing wrong?

    Code:
    #include<stdio.h>
    
    /*Prompts user to enter width*/
    int getwidth(int width);
    
    /*Prints out a square*/
    char printsquare (void);
    
    int
    main(void)
    {
            int width;
            char printsquare;
    
            width = getwidth(width);
    
            printsquare;
    
            return(0);
    
    }
    
    /*Function that prompts user to enter width */
    
            int getwidth (int width)
    {
            printf("Enter width for the house, it must be odd number bigger than 5> ");
            scanf("%d", &width);
            return width;
    }
    
    /*Function that prints out a square */
            
            char printsquare(void)
            
    {
    /* prints out top of the square */
     
            int width;
    
            int count = 0;
            while(count < width)
            {
                    printf("%c", '*');
                    count=count+1;
            }
            printf("\n");
    
    
    /* prints out left and right walls of the square*/
            
            int count1 = 0;
            while(count1 < width-2)   
            {
                    printf("%c", '*');
              int count2 = 0;
              while(count2 < width-2) {
                    printf("%c", ' ');
              count2=count2+1;
                    }
                    printf("%c", '*');
                    printf("\n");
                    count1=count1+1;
            }
            
    /*prints out bottom of the square*/
    
            int count3 = 0;
            while(count3 < width)
            {
                    printf("%c", '*');
                    count3=count3+1;
            }
            printf("\n");
              
                    
    }
    I can't get what's wrong with this code. Do you see what's wrong with it?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You need parenthesis on your function call. Also you might as well get rid of that ambiguous character variable in main.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Feb 2006
    Location
    Philadelphia, PA
    Posts
    27
    what do you mean by "ambiguous character in main"?

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I mean you have a character variable in main that's of no use at all.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Feb 2006
    Location
    Philadelphia, PA
    Posts
    27
    hmm, i wish i knew which one you are talking about.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    ...
    ...
    ... the only character variable in main.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Feb 2006
    Location
    Philadelphia, PA
    Posts
    27
    i give up.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I'm beginning to think you didn't write the code you posted in your original post. Besides... forgot about the character variable. It makes no difference to your program running. As I said, you need parenthesis after your printsquare function call.

    Code:
    printsquare()
    You did it right with your other function, I don't know why you wouldn't do it there.
    Sent from my iPadŽ

  9. #9
    old man
    Join Date
    Dec 2005
    Posts
    90
    Also you want to pass "width" to printsquare()

    edit:

    You pass it to getwidth() but that isn't useful at all. If you declare "width" in getwidth() and return it, that's good enough; then you can just use this

    Code:
    printsquare (getwidth());
    to call your function.

    Explanation: C uses pass by value, and when you pass "width" to getwidth() you're passing the value of an uninitialized variable, which serves no purpose. You do get a useful value from getwidth(), but it's also just a copy.

    You can name all the function scope variables you want "width" but they are not the same variable.

    Also printsquare() should be declared to return void.
    Last edited by eerok; 02-27-2006 at 11:54 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with functions
    By saliya in forum C Programming
    Replies: 1
    Last Post: 11-05-2007, 10:36 PM
  2. Problem with system(), execl(), etc. functions
    By nickkrym in forum C Programming
    Replies: 5
    Last Post: 05-12-2007, 08:04 AM
  3. Problem: Functions
    By Dmitri in forum C Programming
    Replies: 21
    Last Post: 11-06-2005, 10:40 AM
  4. Problem with pointers and functions
    By Kheila in forum C++ Programming
    Replies: 5
    Last Post: 10-13-2005, 12:40 PM
  5. Problem with functions
    By lizardking3 in forum C++ Programming
    Replies: 4
    Last Post: 09-22-2003, 04:34 PM