Thread: Function with two parameters, not sure what to return

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    17

    Function with two parameters, not sure what to return

    I am writing a function called triagle that accepts two parameters 1) the length of a isosceles right triangle and 2) the letter to print. It should print like this

    LLLLL
    LLLL
    LLL
    LL
    L

    Here is what I have so far. I am not sure what I need to call to get the main to read in my function.
    Code:
    #include <stdio.h>
    
    int triangle(int length, int letter)
    { 
    	int i, j;
    	for(i = 1; i <= length; i++);
    	{	
    		for(j = 1; j <=length; j++);
    	}
    	return j;
    
    }
    
    int main (void)
    {
    	int length;
    	int letter;
    
    	printf("enter the length of a isoceles triangle: \n");
    	scanf("%d", &length);
    
    	printf("enter a letter: \n");
    	scanf("%d", &letter);
    
    	printf("%d\n", triangle(length, letter));
    
    	return 0;
    }
    sorry if the spacing and alignment are off

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    I think you would do better to put your printf() statement in your triangle function, and make your triangle function not return any value, by declaring it as returning void.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    Any idea why this wont work?
    Code:
    #include <stdio.h>
    
    int main(void)
    
    {	
    	int i, j, size;
    	char letter;
    
    	printf("Please enter a size:\n");
    	scanf("%d", &size);
    
    	printf("Please enter a letter:\n");
    	scanf("%c", &letter);
    
    	for (i = 1; i <= size; i++)
    	{
    		for (j = i; j <= size; j++)
    			printf("%d", letter);
    
    		printf("\n");
    	}
    	return 0;
    }

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Let me guess, your output consists of a bunch of 10?

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    it clears before I can enter a letter

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    no your right it does print all 10s

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    So I replaced the %d with a %c and it dumps out before I can enter a letter

  8. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    I am not 100% sure of what you mean by "it clears". If you mean the console disappears, then this might be of some help to you. As for the magical tens, start reading from here.
    Last edited by kermit; 09-27-2010 at 08:06 PM.

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    So I replaced scanf("%c", &letter); with letter = getchar(); and had the same result. This seems so simple, why wont it work?

  10. #10
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    The trouble is with the first call to scanf(). Remember that scanf() will only get what you tell it to, and no more. If you tell it to get an integer, that is all it will get, and will ignore anything else that is in the input buffer. With your first call, you ask scanf() to get an int (%d), But in order to enter the int, you type some integer number, and then press enter. Enter inserts a '\n' (newline) character, When scanf reads what you entered, it sees the number, and then the newline. Since you asked it to get a number, it stores that into the variable, and leaves the newline in the input buffer (because a '\n' is a character, and not an integer. So then, there is a character ('\n') in the buffer still. Guess what getchar() looks for? Characters! So if a '\n' is still in the buffer, that satisfies the call, and getchar() returns, storing '\n' into the variable letter. Try this:

    Code:
    printf("Please enter a size:\n");
    	scanf("%d", &size);
            getchar(); /* This will discard the '\n' left by scanf() */
    
    	printf("Please enter a letter:\n");
    	scanf("%c", &letter);
    Now this is just a way to get around the troubles of using scanf(). If you want to continue using C, you will eventually learn other ways of getting user input that are less problematic. If you are interested in going beyond the elementary stuff, just let me know, and I can point you to some resources. It is more work, initially, to learn, but less problematic in the long run.
    Last edited by kermit; 09-27-2010 at 08:30 PM.

  11. #11
    Registered User
    Join Date
    Sep 2010
    Posts
    17
    I get it now, thanks a bunch for the help Kermit. I got my program to run and its working perfectly!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM