Reputation: 21
#include<stdio.h>
#include<stdlib.h>
#define MSIZE 10
int main()
{
int Size, gen, i, j;
printf("Enter number of generations\t");
scanf("%d", &gen);
printf("\nEnter size of the matrix (max size is %d and min is 2)\t", MSIZE);
scanf("%d", &Size);
if (Size > MSIZE) {
printf("\nSize should not be more than %d", MSIZE);
return 1;
}
if (Size < 2) {
printf("\nSize should not be less than 2");
return 1;
}
char **m = (char**) calloc(Size, sizeof(char*));
for (i=0; i<Size; i++)
{
m[i] = (char*) calloc(Size, sizeof(char));
}
printf("Enter matrix of first generation\n");
for (i=0; i<Size; i++) {
for (j=0; j<Size; j++) {
scanf("%c", &m[i][j]);
/*to make sure*/
printf("%c ", m[i][j]);
}
printf("\n\n");
}
}
This is the first part of my program which should be about the death game for Conway. I think, the problem is in the input function, because if I fill it inside the program by myself (not by input) it will be printed right.
Upvotes: 0
Views: 69
Reputation: 11406
I think, the problem is in the input function, because if I fill it inside the program by myself (not by input) it will be printed right.
You need a space before "%c"
in scanf()
to consume a previous newline/enter:
for (i=0; i<Size; i++) {
for (j=0; j<Size; j++) {
scanf(" %c", &m[i][j]);
/*to make sure*/
printf("%c ", m[i][j]);
}
printf("\n\n");
}
When you hit enter on the previous scanf()
, a newline is placed in the input buffer. Adding a space in front of %c
tells scanf()
to skip that newline (and other whitespace).
Upvotes: 2