1
Considering Mxn as 2x2, I need to receive in each Mxn position a character.
for example:
r d
g h
for(i=0; i<m; i++){
for(j=0; j<n; j++){
printf("Digite um caractere para a posicao %d x %d da matriz: ",i,j);
scanf("%c", &caractere[i][j]);
}
}
printf("%c",caractere[0][0]);
printf("%c",caractere[0][1]);
printf("%c",caractere[1][0]);
printf("%c",caractere[1][1]);
The point is:
If I put to receive inside the goes’s with scanf("%s", &caractere[i][j]);
it looks like this:
(I can type each letter in each position, but when it comes to saving, it takes the last two and overwrites the first two. - trying to type tgds, it saves dsds).
Digite um caractere para a posicao 0 x 0 da matriz: t
Digite um caractere para a posicao 0 x 1 da matriz: g
Digite um caractere para a posicao 1 x 0 da matriz: d
Digite um caractere para a posicao 1 x 1 da matriz: s
dsds
If I put to receive with scanf("%c", &caractere[i][j]);
it looks like this:
(I can’t type the two letters, it already puts the two printf’s together - as below.)
Digite um caractere para a posicao 0 x 0 da matriz: Digite um caractere para a posicao 0 x 1 da matriz: re
Digite um caractere para a posicao 1 x 0 da matriz: Digite um caractere para a posicao 1 x 1 da matriz: e
e
How can I receive a character array in c? Would it really be two for? Why can’t I put in every position correctly?
*------------
put this way, he apparently picks one by one:
int main(int argc, char *argv[]) {
int m = 0, n = 0, i, j;
char caractere[m][n];
m = 2;
n = 2;
for(i=0; i<m; i++){
for(j=0; j<n; j++){
printf("Digite um caractere para a posicao %d x %d da matriz: ",i,j);
scanf(" %c", &caractere[i][j]);
}
}
printf("\n%c",caractere[0][0]);
printf("\t%c",caractere[0][1]);
printf("\n%c",caractere[1][0]);
printf("\t%c\n",caractere[1][1]);
return 0;
}
however, look at the return: (continue, always taking the last 2 inserted.)
I haven’t played with C in a while, but try to do this in your scanf
scanf("%c", &(caractere[i][j]));
is right to use twofor
is a for for each dimension of the array– JMSlasher
as explanation the reference operator & is being applied only to
caractere[i]
and then he picks up the index j within the reference... so I suggested switching to include the parentheses like & applies to thecaractere[i][j]
– JMSlasher
Are you typing a character into a ENTER? If so, the ENTER character is being read by the second scanf. Try to inform
rdgh<ENTER>
or clear the input buffer after each read,– anonimo
@Jmslasher I tried to do this way
scanf("%c", &(caractere[i][j]));
, but still, he reads the scanf twice in a row, as the example I posted in the question (image).– Rebeca Nonato
@anonimo even without reading enter, in question when I use %s, it duplicates the last two entries. It would not be because of enter.
– Rebeca Nonato
But if you are reading character then don’t use the format
%s
which is to read string. Use the%c
. Another way to read the antrda with each character followed by a ENTER is to consume this ENTER with a space before %c:scanf(" %c", &caractere[i][j]);
.– anonimo
See test at: https://ideone.com/7VkVDP
– anonimo
@anonimo, face understood. I did exactly this way. But, look at the return. (I will post the question, to put the image)
– Rebeca Nonato
Caraca, my stupid. @Anthonyaccioly worked. If you want to put as an answer, I accept. Okay.
– Rebeca Nonato