0

trying to learn C i got stuck with this: I'm trying to write a function squared which should print on screen a filled square with size and filling character choosen by the user. So i wrote this:

#include <stdio.h>

int squared (int side, char fillCharacter);

int main(){
    int side;
    char fillCharacter;

    printf("Enter the side ");
    scanf("%d", &side);

    printf("Enter the character ");
    scanf("%c", &fillCharacter);

    printf("%d%c",squared(side, fillCharacter));
}

int squared (int side, char fillCharacter){
    for (int row=1 ; row <=  side; row++){
        for (int i =1; i <= side; i++){
        printf("%c", fillCharacter);

        }
    puts("");

    }
}

But sadly the output of this doesn't even scan for the character and just print a blank space

Enter the side 2
Enter the character 

I tried change things but i made it worse so if someone could give me a hint i really would appreciate it. Thanks.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
  • 2
    Your function doesn't return anything and you put a call of it inside a printf which is expecting an int and a char. – Bob__ Jun 11 '17 at 09:42
  • 1
    please try this: `scanf( " %c", &fillCharacter );` (put a space before %c) – Mike Nakis Jun 11 '17 at 09:45
  • 1
    To enter an integer, you type a value (say `2`), and then hit the enter key. `%c` picks up that enter key as a character (and it is a whitespace character). – Peter Jun 11 '17 at 09:45
  • Stop using `scanf()`. The user expects their input to take effect when they hit , one line at a time. That's also what the terminal does in effect. So do the same, and read full lines with `fgets()` or `getline()` (you can then `sscanf()` those). http://c-faq.com/stdio/gets_flush1.html – ilkkachu Jun 11 '17 at 10:02
  • @ikkachu thank you for your explanation, i'm just using scanf() because it's on the book i'm reading in order to learn C; i'm halfway so maybe they will introduce fgets() etc later. But thanks. – entropic.chaos Jun 11 '17 at 10:57
  • @Peter thank you so much now i got it why it "didn't even take the second entry". – entropic.chaos Jun 11 '17 at 11:01
  • @MikeNakis you are right , it works but can you explain me why please, i mean maybe i get it but just to be sure it's better if you tell me. – entropic.chaos Jun 11 '17 at 11:08
  • The two questions of which this is a duplicate have answers that explain fairly well what's happening and why this fixes it. – Mike Nakis Jun 11 '17 at 11:09
  • @Bob__ Well i tried with adding return by: return for (int row=1 ; row <= side; row++){ for (int i =1; i <= side; i++){ printf("%c", fillCharacter);but it just "returned" this: error: expected expression before ‘for’ return for (int row=1 ; row <= side; row++){ ^ – entropic.chaos Jun 11 '17 at 11:10
  • You missed the point. Either your function [returns nothing](https://ideone.com/EhEAuc) or it [returns a meaningful value](https://ideone.com/ZXnNvv). `for` doesn't return anything. – Bob__ Jun 11 '17 at 12:09
  • @Bob__ very helpful thank you, i appreciate it. – entropic.chaos Jun 11 '17 at 13:26

1 Answers1

0

This is linked to this problem. This answer provides a fix but you could just fix it by reversing the calls to scanf: first the character and then the number.

Axnyff
  • 9,213
  • 4
  • 33
  • 37