There is a task, to draw a square through a function, and at the same time to give it a side and from what symbols it will be built. I did everything I can not understand the reason why it does not work, why after the introduction of the party, the program immediately ends. I do from Win 7 x64, DevC ++.

#include <stdio.h> #include <stdlib.h> void even(int side, char symbol); main() { int side = 0; char symbol; printf("Vvedite storony - "); scanf("%d", &side); printf("Vvedite symbol - "); scanf("%c", &symbol); even(side, symbol); system("PAUSE"); } void even(int i, char symbol) { for (int j = 1; j <= i; j++) { for (int m = 1; m <= i; m++) { printf("%c", symbol); } printf("\n"); } return; } 

upd. the problem is in

 printf("Vvedite symbol - "); scanf("%c", &symbol); 
  • 1. Are you sure that the program does not close because you just worked to the end? Put a breakpoint on the last brace of the main function. 2. And what happens if you do not read the values ​​of variables from the console, but specify them directly in the code? 3. Your question title gives sensationalism. - VladD
  • The program works to the end, 2 - the program correctly works, then the matter is in the input of information printf ("Vvedite symbol -"); scanf ("% c", & symbol); = 3 - I'll change now so that no one would think that I have an "unhealthy sensation." - Stee1House pm
  • @steelhouse: Let me assume that you enter numbers through Enter, right? - VladD
  • eeee yes, the logic seems to have caught it, but how can I do it not through an enterter, otherwise it reads the last character, which is he in this case .. - Stee1House
  • one
    @VladD seems to understand what caused your surprise when I wrote about returning NULL from skipnl() . Naturally, the "forward" this macro does not look. If someone suddenly wants, then you can #define skipnleof () ({char b [1024], * res = NULL; fgets (b, 1024, stdin); \ int c = getchar (); if (c! = EOF) {ungetc (c, stdin); res = b;} \ res;}) and oops will not appear. Of course 1) formatting in comments is difficult; 2) such peering ahead for unbuffered IMHO will not work. - avp

1 answer 1

See what happens.

Suppose you typed 13 <Enter> x <Enter> .

In your stream stdin are such symbols: 1 3 \nx \n .

The first scanf "takes" the numbers 1 and 3 from the stream, and stops at the first non-number: \n .

The next scanf reads just one character from the stream, it is just \n .

Check: http://ideone.com/i9xXXr


Do this :

  1. Read lines from the keyboard completely, using fgets ;
  2. Pull information from them with sscanf .

Example :

 #include <stdio.h> #define INPUT_BUF_SIZE 256 int main(void) { int d; char c; char buf[INPUT_BUF_SIZE]; fgets(buf, INPUT_BUF_SIZE, stdin); // читаем целую строку sscanf(buf, "%d", &d); // достаём из неё число fgets(buf, INPUT_BUF_SIZE, stdin); // читаем целую строку sscanf(buf, "%c", &c); // достаём из неё символ printf("d = %d, c = %c = char(%d)", d, c, (int)c); return 0; } 

fgets reads the entire line (more precisely, its piece, which is loaded into the buffer), including the final \n , if there is one.

  • It is clear, however, I still can not decide, I will continue to think. upd: Thanks, not my level, I still have to learn this) Thank you very much. - Stee1House
  • one
    @steelhouse: added an example. - VladD
  • @steelhouse: please! You see, everything is simple there, spend 5 minutes, figure it out, you will explain to others later :-) - VladD
  • This is very far away) The problem is that I am doing the exercises from the book, but they have not yet reached this point, but there are already tasks of this type. This decision on the book does not resort to more interesting and advanced solutions, you can get around as I understood just by changing the data entry in places (I just got it), but as for the example, I will remember it, everything will come in handy) And at the same time I will learn new functions .. Well but I haven't gotten to stdin yet. Thanks again, very well explained) - Stee1House
  • one
    @VladD, this is a non-standard gcc extension. gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html A block enclosed in parentheses returns the value of the last enclosed expression. - dzhioev