I'm trying to create a character array where each line is a different word, however the "\n" in my do-while loop isn't working. I wonder why?

72 views Asked by At

I have a college assignment in which I must create two matrices. The first works correctly, but this one is not recording each word on a different line. I already put NULL, "\0" and "\n" but none had effect. Also, there is this message in the VSCode Editor: "comparison between pointer and integer (line 64, column 30".

I tried to create a matrix with a word in each line, this matrix will be used for word searches in the first matrix (which fortunately is working fine). To do that I created a do-while loop inside a for loop and in the scanf I put "\n" trying to make the program jump to the next line after the user press ENTER. So guys, I need your help to understand what is the problem with my code...

`

#include <stdio.h>
#include <stdlib.h>
#define CHAR 20
int main(){
    int L, C, P, p=0;
    scanf("%d %d %d ", &L, &C, &P);
    char letras[MAX][MAX]; /*a matriz letras*/
    char palavras[P][CHAR];
    for(int p=0; p<P; p++){
        int c=0;
        do{
            scanf("%c", &palavras[p][c]);
            c++;
        }while(palavras[p][c]!= '\n');
    }
     return 0;
}

`

enter image description here

1

There are 1 answers

0
ikegami On

"\n" returns a pointer to the first of two characters. You want '\n' (the Line Feed character).